This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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; | |
} | |
/** |