Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
/**
* @notice
* A stake struct is used to represent the way we store stakes,
* A Stake will contain the users address, the amount staked and a timestamp,
* Since which is when the stake was made
*/
struct Stake{
address user;
uint256 amount;
uint256 since;
/**
* @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, stakeholders[stakes[_staker]].address_stakes);
// Itterate all stakes and grab amount of stakes
stakeholders[index].address_stakes.push(Stake(msg.sender, _amount, timestamp,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.withdrawStake(200, 0, {from:owner});
}catch(error){
assert.equal(error.reason, "Staking: Cannot withdraw more than you have staked", "Failed to notice a too big withdrawal from stake");
it("withdraw 50 from a stake", async() => {
devToken = await DevToken.deployed();
let owner = accounts[0];
let withdraw_amount = 50;
// Try withdrawing 50 from first stake
await devToken.withdrawStake(withdraw_amount, 0, {from:owner});
// Grab a new summary to see if the total amount has changed
let summary = await devToken.hasStake(owner);
it("remove stake if empty", async() => {
devToken = await DevToken.deployed();
let owner = accounts[0];
let withdraw_amount = 50;
// Try withdrawing 50 from first stake AGAIN, this should empty the first stake
await devToken.withdrawStake(withdraw_amount, 0, {from:owner});
// Grab a new summary to see if the total amount has changed
let summary = await devToken.hasStake(owner);
console.log(summary);
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 summary = await devToken.hasStake(owner);
it("reward stakes", async() => {
devToken = await DevToken.deployed();
// Use a fresh Account, Mint 1000 Tokens to it
let staker = accounts[3];
await devToken.mint(accounts[3],1000);
let initial_balance = await devToken.balanceOf(staker);
// Make a stake on 200, fast forward 20 hours, claim reward, amount should be Initial balanace +4
await devToken.stake(200, {from: staker});
await helper.advanceTimeAndBlock(3600*20);
import logo from './logo.svg';
import './App.css';
// These imports are important
import React, { useState, useEffect } from 'react';
import Web3 from 'web3-eth'
function App() {
// this will trigger whenever the App function is called, which index.js runs at startup
// this will trigger whenever the App function is called, which index.js runs at startup
useEffect(() => {
// Here we check if there is web3 support
if (typeof web3 !== 'undefined') {
window.web3 = new Web3(window.web3.currentProvider)
// Check if its MetaMask that is installed
if (window.web3.currentProvider.isMetaMask === true) {
connectMetaMask();
} else {
// Another web3 provider, add support if you want