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
/** | |
* Add functionality like burn to the _stake afunction | |
* | |
*/ | |
function stake(uint256 _amount) public { | |
// Make sure staker actually is good for it | |
require(_amount < _balances[msg.sender], "DevToken: Cannot stake more than you own"); | |
_stake(_amount); | |
// Burn the amount of tokens on the sender |
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"); | |
const { assert } = require('chai'); | |
const truffleAssert = require('truffle-assertions'); | |
contract("DevToken", async accounts => { | |
it("Staking 100x2", async () => { | |
devToken = await DevToken.deployed(); | |
// Stake 100 is used to stake 100 tokens twice and see that stake is added correctly and money burned | |
let owner = accounts[0]; |
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
// Stake again on owner because we want hasStake test to assert summary | |
stakeID = await devToken.stake(stake_amount, { from: owner }); | |
// Assert on the emittedevent using truffleassert | |
// This will capture the event and inside the event callback we can use assert on the values returned | |
truffleAssert.eventEmitted( | |
stakeID, | |
"Staked", | |
(ev) => { | |
// In here we can do our assertion on the ev variable (its the event and will contain the values we emitted) | |
assert.equal(ev.amount, stake_amount, "Stake amount in event was not correct"); |
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("new stakeholder should have increased index", async () => { | |
let stake_amount = 100; | |
stakeID = await devToken.stake(stake_amount, { from: accounts[1] }); | |
// Assert on the emittedevent using truffleassert | |
// This will capture the event and inside the event callback we can use assert on the values returned | |
truffleAssert.eventEmitted( | |
stakeID, | |
"Staked", | |
(ev) => { | |
// In here we can do our assertion on the ev variable (its the event and will contain the values we emitted) |
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 withdrawStake is used to withdraw stakes from the account holder | |
*/ | |
function withdrawStake(uint256 amount, uint256 stake_index) public { | |
uint256 amount_to_mint = _withdrawStake(amount, stake_index); | |
// Return staked tokens to user | |
_mint(msg.sender, amount_to_mint); | |
} |
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 stakes | |
* and the duration the stake has been active | |
*/ | |
function calculateStakeReward(Stake memory _current_stake) internal view returns(uint256){ | |
// First calculate how long the stake has been active | |
// Use current seconds since epoch - the seconds since epoch the stake was made | |
// The output will be duration in SECONDS , | |
// We will reward the user 0.1% per Hour So thats 0.1% per 3600 seconds |
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 | |
rewardPerHour is 1000 because it is used to represent 0.001, since we only use integer numbers | |
This will give users 0.1% reward for each staked token / H | |
*/ | |
uint256 internal rewardPerHour = 1000; |
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 | |
* withdrawStake takes in an amount and a index of the stake and will remove tokens from that stake | |
* Notice index of the stake is the users stake counter, starting at 0 for the first stake | |
* Will return the amount to MINT onto the acount | |
* Will also calculateStakeReward and reset timer | |
*/ | |
function _withdrawStake(uint256 amount, uint256 index) internal returns(uint256){ | |
// Grab user_index which is the index to use to grab the Stake[] | |
uint256 user_index = stakes[msg.sender]; |
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 withdrawStake is used to withdraw stakes from the account holder | |
*/ | |
function withdrawStake(uint256 amount, uint256 stake_index) public { | |
uint256 amount_to_mint = _withdrawStake(amount, stake_index); | |
// Return staked tokens to user | |
_mint(msg.sender, amount_to_mint); | |
} |
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 | |
* StakingSummary is a struct that is used to contain all stakes performed by a certain account | |
*/ | |
struct StakingSummary{ | |
uint256 total_amount; | |
Stake[] stakes; | |
} |