Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active May 8, 2021 04:14
Show Gist options
  • Save percybolmer/3f264deb54eb7d085b54fd8498ecf80c to your computer and use it in GitHub Desktop.
Save percybolmer/3f264deb54eb7d085b54fd8498ecf80c to your computer and use it in GitHub Desktop.
Updated version of staking tests
const DevToken = artifacts.require("DevToken");
// Create our generated error message
const STAKING_TO_MUCH_MSG = "Cannot stake more than you own"
contract("DevToken", async accounts => {
// Stake 100 is used to stake 100 tokens and see that stake is added correctly and money burned
it("Stake 100", async () => {
// Deploy the DevToken and await it, store the results inside devToken
devToken = await DevToken.deployed();
// Set owner, user and a stake_amount
let owner = accounts[0];
let stake_amount = 100
// Get init balance of user
balance = await devToken.balanceOf(owner)
// Add a Try CATCH to capture the Require error that is thrown
try {
// Stake the amount, notice the FROM parameter which specifes what the msg.sender address will be
await devToken.stake(stake_amount, { from: owner});
} catch(error){
// The returned error contains your error message inside reasons field
assert.equal(error.reason, STAKING_TO_MUCH_MSG, "Should trigger a staking error if too much is staked")
}
after_balance = await devToken.balanceOf(owner)
// Assert if his balance is equal to 100000- stake
assert.equal(after_balance.toNumber(), balance - stake_amount);
});
it("Stake overexceeding balance", async () => {
// Deploy the DevToken and await it, store the results inside devToken
devToken = await DevToken.deployed();
// Set owner, user and a stake_amount
let owner = accounts[0];
let stake_amount = 100000
// Get init balance of user
balance = await devToken.balanceOf(owner)
// Add a Try CATCH to capture the Require error that is thrown
try {
// Stake the amount, notice the FROM parameter which specifes what the msg.sender address will be
await devToken.stake(stake_amount, { from: owner});
} catch(error){
// The returned error contains your error message inside reasons field
assert.equal(error.reason, STAKING_TO_MUCH_MSG, "Should trigger a staking error if too much is staked")
}
});
it("Stakes updated after staking", async () => {
// Deploy the DevToken and await it, store the results inside devToken
devToken = await DevToken.deployed();
// Set staker, user and a stake_amount
// Transfer some tokens to the account 1 so he can stake
let staker = accounts[1];
let stake_amount = 100
await devToken.transfer(staker, stake_amount+1)
// Get init balance of user
balance = await devToken.balanceOf(staker)
// Add a Try CATCH to capture the Require error that is thrown
try {
// Stake the amount, notice the FROM parameter which specifes what the msg.sender address will be
await devToken.stake(stake_amount, { from: staker});
} catch(error){
// The returned error contains your error message inside reasons field
assert.equal(error.reason, STAKING_TO_MUCH_MSG, "Should trigger a staking error if too much is staked")
}
amount = await devToken.hasStake(staker);
assert.equal(amount.toNumber(), stake_amount, "Stake should appear when searching for it")
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment