Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
async function connectToSelectedNetwork() {
// This will connect to the selected network inside MetaMask
const web3 = new Web3(Web3.givenProvider);
// Set the ABI of the Built contract so we can interact with it
const abi = await getABI();
const address = getContractAddress();
// Make a new instance of the contract by giving the address and abi
const devtoken = new web3.Contract(abi, address);
// Set the state of the app by passing the contract so we can reach it from other places
function App() {
// Adding our Devtoken state and a set function to assign it
const [devToken, setDevToken] = useState(0);
// getContractAddress returns the address of the contract
// hardcoded :)
function getContractAddress() {
return "0xaD183414719d49Fc3F8Fb0490662C4d484972d86";
}
// getABI loads the ABI of the contract
// This is an async function so we can wait for it to finish executing
async function getABI(){
// DevToken.json should be placed inside the public folder so we can reach it
let ABI = "";
await fetch('./DevToken.json', {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
// 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
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
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);
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("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("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);