Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
/**
* @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 _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
advanceTime = (time) => {
return new Promise((resolve, reject) => {
web3.currentProvider.send({
jsonrpc: '2.0',
method: 'evm_increaseTime',
params: [time],
id: new Date().getTime()
}, (err, result) => {
if (err) { return reject(err) }
return resolve(result)
it("calculate rewards", async() => {
devToken = await DevToken.deployed();
let owner = accounts[0];
// Owner has 1 stake at this time, its the index 1 with 100 Tokens staked
// So lets fast forward time by 20 Hours and see if we gain 2% reward
const newBlock = await helper.advanceTimeAndBlock(3600*20);
let reward = await devToken.calculateTotalAvailableRewards(owner);
/**
* @notice
* This is a Multidimensional array where we store all Stakes that are performed on the Contract
* The stakes for each address are stored at a certain index, the index can be found using the stakeHolders mapping
*/
Stake[][] internal stakes;
/**
* @notice
* Stakeholders is used to keep track of the INDEX for the stakers in the stakes array
*/
/**
* @notice
* Stake is used to make a stake for an sender. It will remove the amount staked from the stakers account and place those tokens inside a stake container
* StakeID
*/
function stake(uint256 _stake) public{
// Simple check so that user does not stake 0
require(_stake > 0, "Cannot stake nothing");
// Make sure staker actually is good for it
require(_stake < balanceOf(msg.sender), "Cannot stake more than you own");
/**
* @notice Wrap the ERC20 constructor with our own construtor, in this case we dont do anything but call the ERC constructor.
*/
constructor() ERC20("DevToken", "DEVTK"){
// Here we mint 100000 tokens to the account that creates the smart contract, 18 0s because ERC20 uses 18 Decimals
_mint(msg.sender, 100000);
// Initialize stakes array
stakes.push();
}
/**
* @notice
* removeStake takes in an amount and a index of the stake and will remove tokens from that stake
*/
function removeStake(uint256 _stake, uint256 index) public {
// Grab user_index which is the index to use to grab the Stake[]
uint256 user_index = stakeHolders[msg.sender];
require(stakes[user_index][index].amount >= _stake, "Cannot withdraw more than you have staked");
// Remove by subtracting the money unstaked
/**
* @notice
* calculateTotalAvailableRewards will iterate all stakes made by a account
* and summerize the total unclaimed rewards
*
*/
function calculateTotalAvailableRewards(address _staker) public view returns(uint256){
uint256 total_reward;
// Itterate all stakes from the address and calculate their reward for each stake
Stake[] memory user_stakes = stakes[stakeHolders[_staker]];
// Make sure the DevToken contract is included by requireing it.
const DevToken = artifacts.require("DevToken");
// THis is an async function, it will accept the Deployer account, the network, and eventual accounts.
module.exports = async function (deployer, network, accounts) {
// await while we deploy the DevToken
await deployer.deploy(DevToken, "DevToken", "DVTK", 18, "50000000000000000000000");
const devToken = await DevToken.deployed()
};
/**
* @notice decimals will return the number of decimal precision the Token is deployed with
*/
function decimals() external view returns (uint8) {
return _decimals;
}
/**
* @notice symbol will return the Token's symbol
*/
function symbol() external view returns (string memory){