2020-03-10 14:35:27 +00:00
// /*global contract, config, it, assert, artifacts*/
2020-03-30 15:33:25 +00:00
let StakingPool = artifacts . require ( 'StakingPool' ) ;
2020-03-10 14:35:27 +00:00
const SNT = artifacts . require ( 'SNT' ) ;
2019-06-18 12:14:29 +00:00
let iuri , jonathan , richard ;
// For documentation please see https://embark.status.im/docs/contracts_testing.html
config ( {
contracts : {
2020-03-10 14:35:27 +00:00
deploy :
{
"MiniMeToken" : { "deploy" : false } ,
"MiniMeTokenFactory" : { } ,
"SNT" : {
"instanceOf" : "MiniMeToken" ,
"args" : [
"$MiniMeTokenFactory" ,
"0x0000000000000000000000000000000000000000" ,
0 ,
"TestMiniMeToken" ,
18 ,
"STT" ,
true
]
} ,
"StakingPool" : {
2020-03-30 15:33:25 +00:00
"deploy" : false ,
2020-03-10 14:35:27 +00:00
"args" : [ "$SNT" ]
}
}
2019-06-18 12:14:29 +00:00
}
} , ( _err , accounts ) => {
iuri = accounts [ 0 ] ;
jonathan = accounts [ 1 ] ;
richard = accounts [ 2 ] ;
2020-03-30 15:33:25 +00:00
pascal = accounts [ 3 ] ;
michael = accounts [ 4 ] ;
eric = accounts [ 5 ] ;
2019-06-18 12:14:29 +00:00
} ) ;
// TODO: add asserts for balances
contract ( "StakingPool" , function ( ) {
this . timeout ( 0 ) ;
2019-06-18 13:59:23 +00:00
before ( async ( ) => {
// distribute SNT
2019-06-18 19:56:50 +00:00
await SNT . methods . generateTokens ( iuri , "10000000000000000000000" ) . send ( { from : iuri } ) ;
2019-06-18 13:59:23 +00:00
await SNT . methods . transfer ( jonathan , "1000000000000000000000" ) . send ( { from : iuri } ) ;
await SNT . methods . transfer ( richard , "1000000000000000000000" ) . send ( { from : iuri } ) ;
2020-03-30 15:33:25 +00:00
await SNT . methods . generateTokens ( pascal , "1000000000000000000" ) . send ( { from : iuri } ) ;
await SNT . methods . generateTokens ( michael , "1000000000000000000" ) . send ( { from : iuri } ) ;
// Deploy Staking Pool
StakingPool = await StakingPool . deploy ( { arguments : [ SNT . options . address , 100 ] } ) . send ( ) ;
2019-06-18 13:59:23 +00:00
// approve StakingPool to transfer tokens
let balance ;
balance = await SNT . methods . balanceOf ( iuri ) . call ( ) ;
assert . strictEqual ( balance , "8000000000000000000000" ) ;
2019-06-18 19:56:50 +00:00
// TODO: we are not approving here because we want to test the approveAndCall functionality
2019-06-18 13:59:23 +00:00
balance = await SNT . methods . balanceOf ( jonathan ) . call ( ) ;
assert . strictEqual ( balance , "1000000000000000000000" ) ;
2020-03-30 15:33:25 +00:00
await SNT . methods . approve ( StakingPool . options . address , "10000000000000000000000" ) . send ( { from : jonathan } ) ;
2019-06-18 13:59:23 +00:00
balance = await SNT . methods . balanceOf ( richard ) . call ( ) ;
assert . strictEqual ( balance , "1000000000000000000000" ) ;
2020-03-30 15:33:25 +00:00
await SNT . methods . approve ( StakingPool . options . address , "10000000000000000000000" ) . send ( { from : richard } ) ;
2019-06-18 13:59:23 +00:00
} )
2019-06-18 12:14:29 +00:00
describe ( "initial state" , ( ) => {
it ( "initial exchangeRate should be 1" , async function ( ) {
let rate = await StakingPool . methods . exchangeRate ( 0 ) . call ( ) ;
assert . strictEqual ( rate , "1000000000000000000" ) ;
} ) ;
it ( "initial token supply should be 0" , async function ( ) {
let rate = await StakingPool . methods . totalSupply ( ) . call ( ) ;
assert . strictEqual ( rate , "0" ) ;
} ) ;
it ( "initial balance should be 0" , async function ( ) {
2020-03-30 15:33:25 +00:00
let balance = await SNT . methods . balanceOf ( StakingPool . options . address ) . call ( ) ;
2019-06-18 13:59:23 +00:00
assert . strictEqual ( balance , "0" ) ;
2019-06-18 12:14:29 +00:00
} ) ;
} )
describe ( "depositing before contributions" , ( ) => {
before ( "deposit 11 ETH" , async ( ) => {
2019-06-18 19:56:50 +00:00
// Deposit using approveAndCall
2020-03-30 15:33:25 +00:00
const encodedCall = StakingPool . methods . stake ( "11000000000000000000" ) . encodeABI ( ) ;
2019-06-18 19:56:50 +00:00
await SNT . methods . approveAndCall ( StakingPool . options . address , "11000000000000000000" , encodedCall ) . send ( { from : iuri } ) ;
2019-06-18 12:14:29 +00:00
} )
it ( "exchangeRate should remain 1" , async function ( ) {
let rate = await StakingPool . methods . exchangeRate ( 0 ) . call ( ) ;
assert . strictEqual ( rate , "1000000000000000000" ) ;
} ) ;
it ( "token supply should be 12" , async function ( ) {
let rate = await StakingPool . methods . totalSupply ( ) . call ( ) ;
assert . strictEqual ( rate , "11000000000000000000" ) ;
} ) ;
it ( "balance should be 12" , async function ( ) {
2020-03-30 15:33:25 +00:00
let balance = await SNT . methods . balanceOf ( StakingPool . options . address ) . call ( ) ;
2019-06-18 13:59:23 +00:00
assert . strictEqual ( balance , "11000000000000000000" ) ;
2019-06-18 12:14:29 +00:00
} ) ;
} ) ;
describe ( "2nd person depositing before contributions" , ( ) => {
before ( "deposit 5 ETH" , async ( ) => {
2020-03-30 15:33:25 +00:00
await StakingPool . methods . stake ( "5000000000000000000" ) . send ( { from : jonathan } )
2019-06-18 12:14:29 +00:00
} )
it ( "exchangeRate should remain 1" , async function ( ) {
let rate = await StakingPool . methods . exchangeRate ( 0 ) . call ( ) ;
assert . strictEqual ( rate , "1000000000000000000" ) ;
} ) ;
it ( "token supply should be 17" , async function ( ) {
let rate = await StakingPool . methods . totalSupply ( ) . call ( ) ;
assert . strictEqual ( rate , "16000000000000000000" ) ;
} ) ;
it ( "balance should be 17" , async function ( ) {
2020-03-30 15:33:25 +00:00
let balance = await SNT . methods . balanceOf ( StakingPool . options . address ) . call ( ) ;
2019-06-18 13:59:23 +00:00
assert . strictEqual ( balance , "16000000000000000000" ) ;
2019-06-18 12:14:29 +00:00
} ) ;
} ) ;
describe ( "contributions" , ( ) => {
before ( "contribute 10 ETH" , async ( ) => {
2020-03-30 15:33:25 +00:00
await SNT . methods . transfer ( StakingPool . options . address , "10000000000000000000" ) . send ( { from : iuri } ) ;
2019-06-18 12:14:29 +00:00
} )
it ( "exchangeRate should increase" , async function ( ) {
let rate = await StakingPool . methods . exchangeRate ( 0 ) . call ( ) ;
assert . strictEqual ( rate , "1625000000000000000" ) ;
} ) ;
it ( "token supply should remain at 17" , async function ( ) {
let rate = await StakingPool . methods . totalSupply ( ) . call ( ) ;
assert . strictEqual ( rate , "16000000000000000000" ) ;
} ) ;
it ( "balance should be 27" , async function ( ) {
2020-03-30 15:33:25 +00:00
let balance = await SNT . methods . balanceOf ( StakingPool . options . address ) . call ( ) ;
2019-06-18 13:59:23 +00:00
assert . strictEqual ( balance , "26000000000000000000" ) ;
2019-06-18 12:14:29 +00:00
} ) ;
} ) ;
describe ( "withdrawing 5 tokens after contributions" , ( ) => {
before ( "withdraw 5 tokens" , async ( ) => {
await StakingPool . methods . withdraw ( "5000000000000000000" ) . send ( { from : jonathan } )
} )
it ( "exchangeRate should remain the same" , async function ( ) {
let rate = await StakingPool . methods . exchangeRate ( 0 ) . call ( ) ;
assert . strictEqual ( rate , "1625000000000000000" ) ;
} ) ;
it ( "token supply should decrease to 11" , async function ( ) {
let rate = await StakingPool . methods . totalSupply ( ) . call ( ) ;
assert . strictEqual ( rate , "11000000000000000000" ) ;
} ) ;
it ( "balance should decrease by correct exchange rate" , async function ( ) {
2020-03-30 15:33:25 +00:00
let balance = await SNT . methods . balanceOf ( StakingPool . options . address ) . call ( ) ;
2019-06-18 12:14:29 +00:00
// 5000000000000000000 tokens x 1.625 rate
// => 8125000000000000000 ETH
// 26000000000000000000 - 8125000000000000000 = 17875000000000000000
2019-06-18 13:59:23 +00:00
assert . strictEqual ( balance , "17875000000000000000" ) ;
2019-06-18 12:14:29 +00:00
} ) ;
} ) ;
describe ( "depositing after contributions" , ( ) => {
before ( "deposit 8 ETH" , async ( ) => {
2020-03-30 15:33:25 +00:00
await StakingPool . methods . stake ( "8000000000000000000" ) . send ( { from : richard } )
2019-06-18 12:14:29 +00:00
} )
it ( "exchangeRate should remain the same" , async function ( ) {
let rate = await StakingPool . methods . exchangeRate ( 0 ) . call ( ) ;
assert . strictEqual ( rate , "1625000000000000000" ) ;
} ) ;
it ( "token supply should increase by correct exchange rate" , async function ( ) {
let rate = await StakingPool . methods . totalSupply ( ) . call ( ) ;
assert . strictEqual ( rate , "15923076923076923077" ) ;
} ) ;
it ( "balance should increase" , async function ( ) {
2020-03-30 15:33:25 +00:00
let balance = await SNT . methods . balanceOf ( StakingPool . options . address ) . call ( ) ;
2019-06-18 12:14:29 +00:00
// 17875000000000000000 + 8000000000000000000
2019-06-18 13:59:23 +00:00
assert . strictEqual ( balance , "25875000000000000000" ) ;
2019-06-18 12:14:29 +00:00
} ) ;
} ) ;
2020-03-30 15:33:25 +00:00
describe ( "Checking stake conditions" , ( ) => {
it ( "should not allow stakeing more than the balance had at the moment of staking pool deployment" , async ( ) => {
await SNT . methods . transfer ( pascal , "1000000000000000000" ) . send ( { from : iuri } ) ; // Pascal now has 2 eth
await SNT . methods . approve ( StakingPool . options . address , "2000000000000000000" ) . send ( { from : pascal } ) ;
await assert . reverts ( StakingPool . methods . stake ( "2000000000000000000" ) , { from : pascal } , "Returned error: VM Exception while processing transaction: revert Stake amount exceeds SNT balance at pool creation" ) ;
await StakingPool . methods . stake ( "1000000000000000000" ) . send ( { from : pascal } ) ;
} ) ;
it ( "should allow stake only after a pool user withdraws" , async ( ) => {
// Mine 100 blocks
for ( let i = 0 ; i < 100 ; i ++ ) {
await mineAtTimestamp ( 12345678 ) ;
}
await SNT . methods . approve ( StakingPool . options . address , "1000000000000000000" ) . send ( { from : michael } ) ;
await assert . reverts ( StakingPool . methods . stake ( "1000000000000000000" ) , { from : michael } , "Returned error: VM Exception while processing transaction: revert Max stake amount exceeded" ) ;
assert . strictEqual ( await StakingPool . methods . maxAmountToStake ( ) . call ( ) , "0" ) ;
await StakingPool . methods . withdraw ( "1000000000000000000" ) . send ( { from : richard } )
assert . strictEqual ( await StakingPool . methods . maxAmountToStake ( ) . call ( ) , "1000000000000000000" ) ;
await SNT . methods . transfer ( eric , "1000000000000000000" ) . send ( { from : michael } ) ;
await SNT . methods . approve ( StakingPool . options . address , "1000000000000000000" ) . send ( { from : eric } ) ;
await StakingPool . methods . stake ( "1000000000000000000" ) . send ( { from : eric } ) ;
} ) ;
} ) ;
2020-03-10 14:35:27 +00:00
} ) ;