Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/**
* @notice DevToken is a development token that we use to learn how to code solidity
* and what BEP-20 interface requires
*/
contract DevToken {
@percybolmer
percybolmer / Crypto-ERC20-OpenZeppeline-Transfer
Created May 7, 2021 14:09
OpenZeppelines transfer method
/**
* @dev See {IERC20-transfer}.
*
* Requirements:
*
* - `recipient` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(_msgSender(), recipient, amount);
it("transfering tokens", async() => {
devToken = await DevToken.deployed();
// Grab initial balance
let initial_balance = await devToken.balanceOf(accounts[1]);
// transfer tokens from account 0 to 1
await devToken.transfer(accounts[1], 100);
let after_balance = await devToken.balanceOf(accounts[1]);
@percybolmer
percybolmer / Crypto-ERC20-DevToken-Stake-V1.0
Last active May 8, 2021 04:13
A simple Staking method, several bugs
contract DevToken is ERC20, Ownable {
using Address for address;
/**
* @notice
* This is a mapping where we store all Stakes that are performed on the Contract
* The stakes for each address
*/
mapping(address => uint256) internal stakes;
const DevToken = artifacts.require("DevToken");
contract("DevToken", async accounts => {
// Stake 100 is used to stake 100 tokens and see that stake is added correctly and money burned
it("Stake 100", async () => {
// Deploy the DevToken and await it, store the results inside devToken
devToken = await DevToken.deployed();
// Set owner, user and a stake_amount
let owner = accounts[0];
let stake_amount = 100
/**
* @notice
* hasStake is used to check if a account has stakes and returns the amount if has staked
*/
function hasStake(address _staker) public view returns(uint256){
return stakes[_staker];
}
@percybolmer
percybolmer / Crypto-ERC20-DevToken_Test_StakingV2
Last active May 8, 2021 04:14
Updated version of staking tests
const DevToken = artifacts.require("DevToken");
// Create our generated error message
const STAKING_TO_MUCH_MSG = "Cannot stake more than you own"
contract("DevToken", async accounts => {
// Stake 100 is used to stake 100 tokens and see that stake is added correctly and money burned
it("Stake 100", async () => {
// Deploy the DevToken and await it, store the results inside devToken
devToken = await DevToken.deployed();
// Set owner, user and a stake_amount
/**
* @notice
* removeStake removes a stake from the stakes mapping and returns the amount to the owner
*/
function removeStake(uint256 _stake) public {
require(stakes[msg.sender] >= _stake, "Cannot withdraw more than you have staked");
// Remove by subtracting the money unstaked
stakes[msg.sender] = stakes[msg.sender] - _stake;
// Return staked tokens to user
it("removeStake overflow", async () => {
// Transfer tokens to account 2 and use for testing
let staker = accounts[2];
let stake_amount = 100;
await devToken.transfer(staker, stake_amount + 1);
try {
// Stake our Tokens
await devToken.stake(stake_amount, { from: staker });
/**
* @notice
* calculateStakeReward is used to calculate how much a user should be rewarded for their stake
*/
function calculateStakeReward(address _staker) public view returns(uint256){
// Since we are using uint and it does only support full integers, start by dividing by 100
return (stakes[_staker] / 100) * 5;
}
/**