Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
/**
* 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
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];
// 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");
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)
/**
* @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);
}
/**
* @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
/**
* @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;
/**
* @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];
@percybolmer
percybolmer / gist:05dea958c597fa8ec8fd7bb695bbee9d
Created July 30, 2021 05:18
Crypto-BEP20-DevToken-WithdrawStake
/**
* @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);
}
/**
* @notice
* StakingSummary is a struct that is used to contain all stakes performed by a certain account
*/
struct StakingSummary{
uint256 total_amount;
Stake[] stakes;
}