Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save percybolmer/ff34bf5ed5ec9612e6265a4d1b774b63 to your computer and use it in GitHub Desktop.
Save percybolmer/ff34bf5ed5ec9612e6265a4d1b774b63 to your computer and use it in GitHub Desktop.
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();
// Stake 100 is used to stake 100 tokens twice and see that stake is added correctly and money burned
let owner = accounts[0];
// Set owner, user and a stake_amount
let stake_amount = 100;
// Get init balance of user
balance = await devToken.balanceOf(owner)
// Stake the amount, notice the FROM parameter which specifes what the msg.sender address will be
try{
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");
assert.equal(ev.index, 0, "Stake index was not correct");
return true;
},
"Stake event should have triggered");
}catch(error){
assert.equal(error.reason, "", "Stakeing should have worked");
}
// Perform a second stake
stakeID = await devToken.stake(stake_amount, { from: owner});
truffleAssert.eventEmitted(
stakeID, "Staked", (ev) => {
assert.equal(ev.amount, stake_amount, "Stake amount in event was not correct");
// This stake should have index 1
assert.equal(ev.index, 1, "Second Stake index was not correct");
return true;
}, "Second stake should have worked"
)
after_balance = await devToken.balanceOf(owner);
// Assert if his balance is equal to 100000- stake*2
assert.equal(after_balance.toNumber(), balance - stake_amount*2);
// Make sure there is now one stake and that the summary says that totalamount is 100
});
it("hasStake works properly", async() => {
devToken = await DevToken.deployed();
let owner = accounts[0];
let summary = await devToken.hasStake(owner);
assert.equal(summary.total_amount, 200, "The total staking amount should be 200");
// Itterate all stakes and verify their amount aswell.
summary.stakes.forEach(function(stake) {
assert.equal(stake.amount, 100, "Stake has wrong amount of tokens");
});
assert.equal(summary.stakes.length, 2, "Wrong length of stakes in summary");
});
it("to low balance too stake", async() => {
devToken = await DevToken.deployed();
let staker = accounts[1];
// staker has no tokens so lets see him stake 0 and then more
try {
await devToken.stake(0, {from: staker});
}catch(error){
assert.equal(error.reason, "Cannot stake nothing" , "Failed to trigger 0 stake check");
}
try{
await devToken.stake(100, {from: staker});
}catch(error){
assert.equal(error.reason, "Cannot stake more than you own", "Failed to trigger balance to small check");
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment