2020-02-19 09:33:38 +00:00
const TestToken = artifacts . require ( 'TestToken' ) ;
2020-09-30 08:18:15 +00:00
const ERC20Bucket = artifacts . require ( 'ERC20Bucket' ) ;
2020-04-28 09:36:34 +00:00
const ERC20BucketFactory = artifacts . require ( 'ERC20BucketFactory' ) ;
2020-10-01 07:51:33 +00:00
const {
bucketShouldBeOwnable ,
factoryShouldCreateAnOwnableBucket ,
} = require ( "./helpers" ) ;
2020-02-18 19:23:31 +00:00
2020-02-19 11:28:05 +00:00
const TOTAL _SUPPLY = 10000 ;
const GIFT _AMOUNT = 10 ;
const REDEEM _CODE = web3 . utils . sha3 ( "hello world" ) ;
const NOW = Math . round ( new Date ( ) . getTime ( ) / 1000 ) ;
2020-04-23 12:51:05 +00:00
const START _TIME = NOW - 1 ;
2020-02-19 11:28:05 +00:00
const EXPIRATION _TIME = NOW + 60 * 60 * 24 ; // in 24 hours
2020-05-04 06:04:30 +00:00
const MAX _TX _DELAY _BLOCKS = 10 ;
2020-02-19 11:28:05 +00:00
2020-02-18 19:23:31 +00:00
async function signRedeem ( contractAddress , signer , message ) {
const result = await web3 . eth . net . getId ( ) ;
2020-02-19 13:35:11 +00:00
let chainId = parseInt ( result ) ;
2020-04-01 16:27:06 +00:00
//FIXME: in tests, getChainID in the contract returns 1 so we hardcode it here to 1.
2020-02-19 13:35:11 +00:00
chainId = 1 ;
2020-02-18 19:23:31 +00:00
2020-04-01 16:27:06 +00:00
const domain = [
2020-02-18 19:23:31 +00:00
{ name : "name" , type : "string" } ,
{ name : "version" , type : "string" } ,
{ name : "chainId" , type : "uint256" } ,
{ name : "verifyingContract" , type : "address" }
] ;
2020-04-01 16:27:06 +00:00
const redeem = [
2020-04-02 11:28:41 +00:00
{ name : "blockNumber" , type : "uint256" } ,
{ name : "blockHash" , type : "bytes32" } ,
2020-02-18 19:23:31 +00:00
{ name : "receiver" , type : "address" } ,
{ name : "code" , type : "bytes32" } ,
] ;
2020-04-01 16:27:06 +00:00
const domainData = {
2020-04-28 09:36:34 +00:00
name : "KeycardERC20Bucket" ,
2020-02-18 19:23:31 +00:00
version : "1" ,
chainId : chainId ,
verifyingContract : contractAddress
} ;
2020-04-01 16:27:06 +00:00
const data = {
2020-02-18 19:23:31 +00:00
types : {
EIP712Domain : domain ,
Redeem : redeem ,
} ,
primaryType : "Redeem" ,
domain : domainData ,
message : message
} ;
return new Promise ( ( resolve , reject ) => {
2020-09-30 08:18:15 +00:00
web3 . currentProvider . send ( {
2020-02-18 19:23:31 +00:00
jsonrpc : '2.0' ,
id : Date . now ( ) . toString ( ) . substring ( 9 ) ,
method : "eth_signTypedData" ,
params : [ signer , data ] ,
2020-02-19 13:35:11 +00:00
from : signer
2020-02-18 19:23:31 +00:00
} , ( error , res ) => {
if ( error ) {
return reject ( error ) ;
}
2020-09-30 10:30:16 +00:00
2020-02-18 19:23:31 +00:00
resolve ( res . result ) ;
} ) ;
} ) ;
}
function mineAt ( timestamp ) {
return new Promise ( ( resolve , reject ) => {
2020-09-30 08:18:15 +00:00
web3 . currentProvider . send ( {
2020-02-18 19:23:31 +00:00
jsonrpc : '2.0' ,
method : "evm_mine" ,
params : [ timestamp ] ,
id : Date . now ( ) . toString ( ) . substring ( 9 )
} , ( error , res ) => {
if ( error ) {
return reject ( error ) ;
}
resolve ( res . result ) ;
} ) ;
} ) ;
}
2020-09-30 10:11:56 +00:00
contract ( "ERC20Bucket" , function ( ) {
2020-09-30 08:18:15 +00:00
let bucketInstance ,
factoryInstance ,
2020-09-30 10:11:56 +00:00
tokenInstance ,
2020-09-30 08:18:15 +00:00
shop ,
user ,
relayer ,
keycard _1 ,
keycard _2 ;
2020-02-21 15:09:42 +00:00
2020-09-30 08:18:15 +00:00
before ( async ( ) => {
const accounts = await web3 . eth . getAccounts ( ) ;
shop = accounts [ 0 ] ;
user = accounts [ 1 ] ;
relayer = accounts [ 2 ] ;
keycard _1 = accounts [ 3 ] ;
keycard _2 = accounts [ 4 ] ;
2020-02-21 19:01:15 +00:00
2020-09-30 08:18:15 +00:00
const deployedTestToken = await TestToken . deployed ( ) ;
2020-09-30 10:11:56 +00:00
tokenInstance = new web3 . eth . Contract ( TestToken . abi , deployedTestToken . address ) ;
2020-09-30 08:18:15 +00:00
} ) ;
2020-02-18 19:23:31 +00:00
2020-10-01 07:51:33 +00:00
bucketShouldBeOwnable ( "erc20" , ( ) => [ ERC20Bucket , shop , tokenInstance , [ START _TIME , EXPIRATION _TIME , MAX _TX _DELAY _BLOCKS ] ] ) ;
factoryShouldCreateAnOwnableBucket ( "erc20" , ( ) => [ ERC20BucketFactory , ERC20Bucket , shop , tokenInstance , [ START _TIME , EXPIRATION _TIME , MAX _TX _DELAY _BLOCKS ] ] ) ;
2020-03-25 08:41:59 +00:00
it ( "deploy factory" , async ( ) => {
2020-09-30 08:18:15 +00:00
const contract = new web3 . eth . Contract ( ERC20BucketFactory . abi ) ;
const deploy = contract . deploy ( { data : ERC20BucketFactory . bytecode } ) ;
const gas = await deploy . estimateGas ( ) ;
const rec = await deploy . send ( {
from : shop ,
gas ,
2020-03-25 08:41:59 +00:00
} ) ;
2020-09-30 08:18:15 +00:00
factoryInstance = new web3 . eth . Contract ( ERC20BucketFactory . abi , rec . options . address ) ;
2020-03-25 08:41:59 +00:00
} ) ;
2020-02-21 19:01:15 +00:00
it ( "deploy bucket" , async ( ) => {
2020-09-30 08:18:15 +00:00
const instance = new web3 . eth . Contract ( ERC20Bucket . abi ) ;
const deploy = instance . deploy ( {
data : ERC20Bucket . bytecode ,
2020-09-30 10:11:56 +00:00
arguments : [ tokenInstance . options . address , START _TIME , EXPIRATION _TIME , MAX _TX _DELAY _BLOCKS ]
2020-03-25 08:41:59 +00:00
} ) ;
const gas = await deploy . estimateGas ( ) ;
2020-09-30 08:18:15 +00:00
const rec = await deploy . send ( {
from : shop ,
gas ,
} ) ;
bucketInstance = new web3 . eth . Contract ( ERC20Bucket . abi , rec . options . address ) ;
2020-02-21 19:01:15 +00:00
} ) ;
it ( "deploy bucket via factory" , async ( ) => {
2020-10-01 07:51:33 +00:00
const create = factoryInstance . methods . create ( tokenInstance . options . address , START _TIME , EXPIRATION _TIME , MAX _TX _DELAY _BLOCKS ) ;
2020-02-21 19:01:15 +00:00
const gas = await create . estimateGas ( ) ;
2020-04-24 16:59:08 +00:00
const receipt = await create . send ( {
from : shop ,
gas : gas ,
} ) ;
} ) ;
2020-04-29 10:50:25 +00:00
it ( "return correct bucket type" , async function ( ) {
2020-09-30 08:18:15 +00:00
let bucketType = await bucketInstance . methods . bucketType ( ) . call ( ) ;
2020-04-30 05:34:24 +00:00
assert . equal ( parseInt ( bucketType ) , 20 ) ;
2020-04-29 10:50:25 +00:00
} ) ;
2020-02-19 11:28:05 +00:00
it ( "shop buys 100 tokens" , async function ( ) {
2020-09-30 10:11:56 +00:00
let supply = await tokenInstance . methods . totalSupply ( ) . call ( ) ;
2020-02-19 11:28:05 +00:00
assert . equal ( parseInt ( supply ) , 0 ) ;
2020-02-18 19:23:31 +00:00
2020-09-30 10:11:56 +00:00
await tokenInstance . methods . mint ( TOTAL _SUPPLY ) . send ( {
2020-02-19 11:28:05 +00:00
from : shop ,
} ) ;
2020-02-18 19:23:31 +00:00
2020-09-30 10:11:56 +00:00
supply = await tokenInstance . methods . totalSupply ( ) . call ( ) ;
2020-02-19 11:28:05 +00:00
assert . equal ( parseInt ( supply ) , TOTAL _SUPPLY ) ;
2020-02-18 19:23:31 +00:00
2020-09-30 10:11:56 +00:00
let shopBalance = await tokenInstance . methods . balanceOf ( shop ) . call ( ) ;
2020-02-19 11:28:05 +00:00
assert . equal ( parseInt ( shopBalance ) , TOTAL _SUPPLY ) ;
} ) ;
2020-02-18 19:23:31 +00:00
2020-02-21 15:09:42 +00:00
it ( "add supply" , async function ( ) {
2020-09-30 10:11:56 +00:00
let bucketBalance = await tokenInstance . methods . balanceOf ( bucketInstance . options . address ) . call ( ) ;
2020-02-21 15:09:42 +00:00
assert . equal ( parseInt ( bucketBalance ) , 0 , ` bucket balance before is ${ bucketBalance } instead of 0 ` ) ;
2020-02-19 09:33:38 +00:00
2020-09-30 10:11:56 +00:00
let shopBalance = await tokenInstance . methods . balanceOf ( shop ) . call ( ) ;
2020-02-19 11:28:05 +00:00
assert . equal ( parseInt ( shopBalance ) , TOTAL _SUPPLY , ` shop balance before is ${ shopBalance } instead of ${ TOTAL _SUPPLY } ` ) ;
2020-02-19 09:33:38 +00:00
2020-09-30 10:11:56 +00:00
const transfer = tokenInstance . methods . transfer ( bucketInstance . options . address , TOTAL _SUPPLY ) ;
2020-02-21 15:09:42 +00:00
const transferGas = await transfer . estimateGas ( ) ;
await transfer . send ( {
2020-02-19 11:28:05 +00:00
from : shop ,
2020-02-21 15:09:42 +00:00
gas : transferGas ,
2020-02-19 11:28:05 +00:00
} ) ;
2020-02-19 09:33:38 +00:00
2020-09-30 10:11:56 +00:00
bucketBalance = await tokenInstance . methods . balanceOf ( bucketInstance . options . address ) . call ( ) ;
2020-02-21 15:09:42 +00:00
assert . equal ( parseInt ( bucketBalance ) , TOTAL _SUPPLY , ` bucket balance after is ${ bucketBalance } instead of ${ TOTAL _SUPPLY } ` ) ;
2020-02-19 09:33:38 +00:00
2020-09-30 10:11:56 +00:00
shopBalance = await tokenInstance . methods . balanceOf ( shop ) . call ( ) ;
2020-02-19 11:28:05 +00:00
assert . equal ( parseInt ( shopBalance ) , 0 , ` shop balance after is ${ shopBalance } instead of 0 ` ) ;
2020-02-19 09:33:38 +00:00
2020-09-30 08:18:15 +00:00
let totalSupply = await bucketInstance . methods . totalSupply ( ) . call ( ) ;
2020-02-19 11:28:05 +00:00
assert . equal ( parseInt ( totalSupply ) , TOTAL _SUPPLY , ` total contract supply is ${ totalSupply } instead of ${ TOTAL _SUPPLY } ` ) ;
2020-09-30 08:18:15 +00:00
let availableSupply = await bucketInstance . methods . availableSupply ( ) . call ( ) ;
2020-02-19 11:28:05 +00:00
assert . equal ( parseInt ( availableSupply ) , TOTAL _SUPPLY , ` available contract supply is ${ availableSupply } instead of ${ TOTAL _SUPPLY } ` ) ;
2020-02-18 19:23:31 +00:00
} ) ;
2020-04-28 09:36:34 +00:00
async function testCreateRedeemable ( keycard , amount ) {
2020-09-30 08:18:15 +00:00
let initialSupply = await bucketInstance . methods . totalSupply ( ) . call ( ) ;
let initialAvailableSupply = await bucketInstance . methods . availableSupply ( ) . call ( ) ;
2020-02-21 15:09:42 +00:00
2020-02-19 11:28:05 +00:00
const redeemCodeHash = web3 . utils . sha3 ( REDEEM _CODE ) ;
2020-09-30 08:18:15 +00:00
const createRedeemable = bucketInstance . methods . createRedeemable ( keycard , amount , redeemCodeHash ) ;
2020-04-28 09:36:34 +00:00
const createRedeemableGas = await createRedeemable . estimateGas ( ) ;
await createRedeemable . send ( {
2020-02-19 11:28:05 +00:00
from : shop ,
2020-04-28 09:36:34 +00:00
gas : createRedeemableGas ,
2020-02-19 11:28:05 +00:00
} ) ;
2020-02-18 19:23:31 +00:00
2020-09-30 08:18:15 +00:00
let totalSupply = await bucketInstance . methods . totalSupply ( ) . call ( ) ;
2020-02-21 15:09:42 +00:00
assert . equal ( parseInt ( totalSupply ) , parseInt ( initialSupply ) , ` totalSupply is ${ totalSupply } instead of ${ initialSupply } ` ) ;
2020-02-18 19:23:31 +00:00
2020-09-30 08:18:15 +00:00
let availableSupply = await bucketInstance . methods . availableSupply ( ) . call ( ) ;
2020-02-21 15:09:42 +00:00
assert . equal ( parseInt ( availableSupply ) , parseInt ( initialAvailableSupply ) - amount ) ;
2020-02-19 11:28:05 +00:00
}
2020-02-18 19:23:31 +00:00
2020-04-28 09:36:34 +00:00
it ( "createRedeemable should fail if amount is zero" , async function ( ) {
2020-02-19 11:28:05 +00:00
try {
2020-04-28 09:36:34 +00:00
await testCreateRedeemable ( keycard _1 , 0 ) ;
assert . fail ( "createRedeemable should have failed" ) ;
2020-02-19 11:28:05 +00:00
} catch ( e ) {
assert . match ( e . message , /invalid amount/ ) ;
}
} ) ;
2020-02-18 19:23:31 +00:00
2020-04-28 09:36:34 +00:00
it ( "createRedeemable fails if amount > totalSupply" , async function ( ) {
2020-02-19 11:28:05 +00:00
try {
2020-04-28 09:36:34 +00:00
await testCreateRedeemable ( keycard _1 , TOTAL _SUPPLY + 1 ) ;
assert . fail ( "createRedeemable should have failed" ) ;
2020-02-19 11:28:05 +00:00
} catch ( e ) {
assert . match ( e . message , /low supply/ ) ;
}
} ) ;
2020-02-18 19:23:31 +00:00
2020-04-28 09:36:34 +00:00
it ( "createRedeemable" , async function ( ) {
await testCreateRedeemable ( keycard _1 , GIFT _AMOUNT ) ;
2020-02-19 11:28:05 +00:00
} ) ;
2020-02-18 19:23:31 +00:00
2020-04-28 09:36:34 +00:00
it ( "createRedeemable should fail if keycard has already been used" , async function ( ) {
2020-02-19 11:28:05 +00:00
try {
2020-04-28 09:36:34 +00:00
await testCreateRedeemable ( keycard _1 , 1 ) ;
assert . fail ( "createRedeemable should have failed" ) ;
2020-02-19 11:28:05 +00:00
} catch ( e ) {
2020-02-21 19:01:15 +00:00
assert . match ( e . message , /recipient already used/ ) ;
2020-02-19 11:28:05 +00:00
}
} ) ;
2020-02-18 19:23:31 +00:00
2020-04-28 09:36:34 +00:00
it ( "createRedeemable amount > availableSupply" , async function ( ) {
2020-02-19 11:28:05 +00:00
try {
2020-04-28 09:36:34 +00:00
await testCreateRedeemable ( keycard _2 , TOTAL _SUPPLY - GIFT _AMOUNT + 1 ) ;
assert . fail ( "createRedeemable should have failed" ) ;
2020-02-19 11:28:05 +00:00
} catch ( e ) {
assert . match ( e . message , /low supply/ ) ;
}
} ) ;
2020-02-18 19:23:31 +00:00
2020-04-02 11:28:41 +00:00
async function testRedeem ( receiver , recipient , signer , relayer , redeemCode , blockNumber , blockHash ) {
2020-09-30 10:11:56 +00:00
let initialBucketBalance = await tokenInstance . methods . balanceOf ( bucketInstance . options . address ) . call ( ) ;
let initialUserBalance = await tokenInstance . methods . balanceOf ( user ) . call ( ) ;
2020-09-30 08:18:15 +00:00
let initialRedeemableSupply = await bucketInstance . methods . redeemableSupply ( ) . call ( ) ;
2020-02-18 19:23:31 +00:00
2020-09-30 08:18:15 +00:00
let redeemable = await bucketInstance . methods . redeemables ( recipient ) . call ( ) ;
2020-04-28 09:36:34 +00:00
const amount = parseInt ( redeemable . data ) ;
2020-02-18 19:23:31 +00:00
2020-02-19 11:28:05 +00:00
const message = {
2020-04-02 11:28:41 +00:00
blockNumber : blockNumber ,
blockHash : blockHash ,
2020-02-21 15:09:42 +00:00
receiver : receiver ,
2020-02-19 11:28:05 +00:00
code : redeemCode ,
} ;
2020-02-18 19:23:31 +00:00
2020-09-30 08:18:15 +00:00
const sig = await signRedeem ( bucketInstance . options . address , signer , message ) ;
const redeem = bucketInstance . methods . redeem ( message , sig ) ;
2020-02-19 11:28:05 +00:00
const redeemGas = await redeem . estimateGas ( ) ;
2020-04-29 10:33:14 +00:00
let receipt = await redeem . send ( {
2020-02-21 22:57:44 +00:00
from : relayer ,
2020-02-19 11:28:05 +00:00
gas : redeemGas ,
} ) ;
2020-04-29 10:33:14 +00:00
assert . equal ( receipt . events . Redeemed . returnValues . recipient , recipient ) ;
assert . equal ( receipt . events . Redeemed . returnValues . data , redeemable . data ) ;
2020-02-19 11:28:05 +00:00
2020-02-21 15:09:42 +00:00
let expectedBucketBalance = parseInt ( initialBucketBalance ) - amount ;
2020-09-30 10:11:56 +00:00
let bucketBalance = await tokenInstance . methods . balanceOf ( bucketInstance . options . address ) . call ( ) ;
2020-02-21 15:09:42 +00:00
assert . equal ( parseInt ( bucketBalance ) , expectedBucketBalance , ` bucketBalance after redeem should be ${ expectedBucketBalance } instead of ${ bucketBalance } ` ) ;
let expectedUserBalance = parseInt ( initialUserBalance + amount ) ;
2020-09-30 10:11:56 +00:00
userBalance = await tokenInstance . methods . balanceOf ( user ) . call ( ) ;
2020-02-21 15:09:42 +00:00
assert . equal ( parseInt ( userBalance ) , expectedUserBalance , ` user ` , ` userBalance after redeem should be ${ expectedUserBalance } instead of ${ userBalance } ` ) ;
let expectedRedeemableSupply = initialRedeemableSupply - amount ;
2020-09-30 08:18:15 +00:00
let redeemableSupply = await bucketInstance . methods . redeemableSupply ( ) . call ( ) ;
2020-02-21 15:09:42 +00:00
assert . equal ( parseInt ( redeemableSupply ) , expectedRedeemableSupply , ` redeemableSupply after redeem should be ${ expectedRedeemableSupply } instead of ${ redeemableSupply } ` ) ;
2020-02-19 11:28:05 +00:00
}
2020-04-23 12:51:05 +00:00
it ( "cannot redeem before start date" , async function ( ) {
const block = await web3 . eth . getBlock ( "latest" ) ;
await mineAt ( START _TIME ) ;
try {
await testRedeem ( user , keycard _1 , keycard _1 , relayer , REDEEM _CODE , block . number , block . hash ) ;
assert . fail ( "redeem should have failed" ) ;
} catch ( e ) {
assert . match ( e . message , /not yet started/ ) ;
}
} ) ;
2020-02-19 11:28:05 +00:00
it ( "cannot redeem after expiration date" , async function ( ) {
2020-04-02 11:28:41 +00:00
const block = await web3 . eth . getBlock ( "latest" ) ;
2020-02-19 11:28:05 +00:00
await mineAt ( EXPIRATION _TIME ) ;
2020-04-02 11:28:41 +00:00
2020-02-19 11:28:05 +00:00
try {
2020-04-02 11:28:41 +00:00
await testRedeem ( user , keycard _1 , keycard _1 , relayer , REDEEM _CODE , block . number , block . hash ) ;
2020-02-19 11:28:05 +00:00
assert . fail ( "redeem should have failed" ) ;
} catch ( e ) {
assert . match ( e . message , /expired/ ) ;
}
} ) ;
it ( "cannot redeem with invalid code" , async function ( ) {
2020-04-02 11:28:41 +00:00
const block = await web3 . eth . getBlock ( "latest" ) ;
2020-02-19 11:28:05 +00:00
await mineAt ( NOW ) ;
try {
2020-04-02 11:28:41 +00:00
await testRedeem ( user , keycard _1 , keycard _1 , relayer , web3 . utils . sha3 ( "bad-code" ) , block . number , block . hash ) ;
2020-02-19 11:28:05 +00:00
assert . fail ( "redeem should have failed" ) ;
} catch ( e ) {
assert . match ( e . message , /invalid code/ ) ;
}
} ) ;
2020-03-25 08:23:22 +00:00
it ( "cannot redeem with invalid recipient" , async function ( ) {
2020-04-02 11:28:41 +00:00
const block = await web3 . eth . getBlock ( "latest" ) ;
2020-02-21 15:09:42 +00:00
await mineAt ( NOW ) ;
try {
2020-04-02 11:28:41 +00:00
await testRedeem ( user , keycard _1 , keycard _2 , relayer , REDEEM _CODE , block . number , block . hash ) ;
2020-02-21 15:09:42 +00:00
assert . fail ( "redeem should have failed" ) ;
} catch ( e ) {
2020-03-25 08:23:22 +00:00
assert . match ( e . message , /not found/ ) ;
2020-02-21 15:09:42 +00:00
}
} ) ;
2020-04-02 11:28:41 +00:00
it ( "cannot redeem with a block in the future" , async function ( ) {
const block = await web3 . eth . getBlock ( "latest" ) ;
await mineAt ( NOW ) ;
try {
await testRedeem ( user , keycard _1 , keycard _1 , relayer , REDEEM _CODE , ( block . number + 2 ) , "0x0000000000000000000000000000000000000000000000000000000000000000" ) ;
} catch ( e ) {
assert . match ( e . message , /future/ ) ;
}
} ) ;
it ( "cannot redeem with an old block" , async function ( ) {
const currentBlock = await web3 . eth . getBlock ( "latest" ) ;
const block = await web3 . eth . getBlock ( currentBlock . number - 10 ) ;
await mineAt ( NOW ) ;
try {
await testRedeem ( user , keycard _1 , keycard _1 , relayer , REDEEM _CODE , block . number , block . hash ) ;
} catch ( e ) {
assert . match ( e . message , /too old/ ) ;
}
} ) ;
it ( "cannot redeem with an invalid hash" , async function ( ) {
const block = await web3 . eth . getBlock ( "latest" ) ;
await mineAt ( NOW ) ;
try {
await testRedeem ( user , keycard _1 , keycard _1 , relayer , REDEEM _CODE , block . number , "0x0000000000000000000000000000000000000000000000000000000000000000" ) ;
} catch ( e ) {
assert . match ( e . message , /invalid block hash/ ) ;
}
2020-04-23 11:42:08 +00:00
} ) ;
2020-04-02 11:28:41 +00:00
2020-02-19 11:28:05 +00:00
it ( "can redeem before expiration date" , async function ( ) {
2020-04-02 11:28:41 +00:00
const block = await web3 . eth . getBlock ( "latest" ) ;
2020-02-19 11:28:05 +00:00
await mineAt ( NOW ) ;
2020-04-02 11:28:41 +00:00
await testRedeem ( user , keycard _1 , keycard _1 , relayer , REDEEM _CODE , block . number , block . hash ) ;
2020-02-19 11:28:05 +00:00
} ) ;
async function testKill ( ) {
2020-09-30 10:11:56 +00:00
let initialShopBalance = parseInt ( await tokenInstance . methods . balanceOf ( shop ) . call ( ) ) ;
let initialBucketBalance = parseInt ( await tokenInstance . methods . balanceOf ( bucketInstance . options . address ) . call ( ) ) ;
2020-02-19 11:28:05 +00:00
2020-09-30 08:18:15 +00:00
await bucketInstance . methods . kill ( ) . send ( {
2020-02-19 11:28:05 +00:00
from : shop ,
} ) ;
2020-02-21 15:09:42 +00:00
let expectedShopBalance = initialShopBalance + initialBucketBalance ;
2020-09-30 10:11:56 +00:00
let shopBalance = await tokenInstance . methods . balanceOf ( shop ) . call ( ) ;
2020-02-21 15:09:42 +00:00
assert . equal ( parseInt ( shopBalance ) , expectedShopBalance , ` shop balance after kill is ${ shopBalance } instead of ${ expectedShopBalance } ` ) ;
2020-02-19 11:28:05 +00:00
2020-09-30 10:11:56 +00:00
let bucketBalance = await tokenInstance . methods . balanceOf ( bucketInstance . options . address ) . call ( ) ;
2020-02-21 15:09:42 +00:00
assert . equal ( parseInt ( bucketBalance ) , 0 , ` bucketBalance after kill is ${ bucketBalance } instead of 0 ` ) ;
2020-02-19 11:28:05 +00:00
}
it ( "shop cannot kill contract before expirationTime" , async function ( ) {
await mineAt ( NOW ) ;
try {
await testKill ( ) ;
assert . fail ( "redeem should have failed" ) ;
} catch ( e ) {
assert . match ( e . message , /not expired yet/ ) ;
}
} ) ;
it ( "shop can kill contract after expirationTime" , async function ( ) {
await mineAt ( EXPIRATION _TIME ) ;
await testKill ( ) ;
2020-04-23 11:42:08 +00:00
await mineAt ( NOW ) ;
2020-02-19 11:28:05 +00:00
} ) ;
2020-02-18 19:23:31 +00:00
} ) ;