Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
it("calculate stakerholder rewards", async () => {
// Transfer tokens to account 4 and use for testing
let staker = accounts[4];
let stake_amount = 100;
await devToken.transfer(staker, stake_amount + 1);
// See what rewards we get before staking
let amount = await devToken.calculateStakeReward(staker);
@percybolmer
percybolmer / Crypto-ERC20-DevToken_Stake_Struct
Last active May 9, 2021 19:12
A gist showing how we can create a struct in solidity
/**
* @notice
* A stake struct is used to represent the way we store stakes,
* A Stake will contain the users address, the amount staked and two time stamps,
* Since which is when the stake was made
* Until is the end date of the stake
*/
struct Stake{
address user;
uint256 amount;
/**
* @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
* hasStake is used to check if a account has stakes and the total amount along with all the seperate stakes
*/
function hasStake(address _staker) public view returns(StakingSummary memory){
// totalStakeAmount is used to count total staked amount of the address
uint256 totalStakeAmount;
// Keep a summary in memory since we need to calculate this
StakingSummary memory summary = StakingSummary(0, stakes[_staker]);
// Itterate all stakes and grab amount of stakes
/**
* @notice
* Events below are declared, they are used to communicate to the transaction logs about events
*/
// Staked event is triggered whenever a user stakes tokens, address is indexed to make it filterable
event Staked(address indexed user, uint256 amount, uint256 index, uint256 timestamp);
const DevToken = artifacts.require("DevToken");
contract("DevToken", async accounts => {
it("Stake 100 x2", async () => {
// Deploy the DevToken and await it, store the results inside devToken
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];
const DevToken = artifacts.require("DevToken");
const assert = require("chai").assert;
const truffleAssert = require('truffle-assertions');
contract("DevToken", async accounts => {
it("Stake 100 x2", async () => {
// Deploy the DevToken and await it, store the results inside devToken
devToken = await DevToken.deployed();
/**
* @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 {
require(stakes[msg.sender][index].amount >= _stake, "Cannot withdraw more than you have staked");
// Remove by subtracting the money unstaked
stakes[msg.sender][index].amount = stakes[msg.sender][index].amount - _stake;
// If stake is empty, 0, then remove it from the array of stakes
if(stakes[msg.sender][index].amount == 0){
it("cant withdraw bigger amount than current stake", async() => {
devToken = await DevToken.deployed();
let owner = accounts[0];
// Try withdrawing 200 from first stake
try {
await devToken.removeStake(200, 0, {from:owner});
}catch(error){
assert.equal(error.reason, "Cannot withdraw more than you have staked", "Failed to notice a too big withdrawal from stake");
/**
* @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;