|
| 1 | +const { contract, web3 } = require('@openzeppelin/test-environment'); |
| 2 | +const { expectRevert, expectEvent } = require('@openzeppelin/test-helpers'); |
| 3 | +const { expect } = require('chai'); |
| 4 | + |
| 5 | +const _require = require('app-root-path').require; |
| 6 | +const BlockchainCaller = _require('/util/blockchain_caller'); |
| 7 | +const chain = new BlockchainCaller(web3); |
| 8 | + |
| 9 | +const MockERC20 = contract.fromArtifact('MockERC20'); |
| 10 | +const TokenPool = contract.fromArtifact('TokenPool'); |
| 11 | + |
| 12 | +let token, otherToken, tokenPool, owner, anotherAccount; |
| 13 | +describe('tokenPool', function () { |
| 14 | + beforeEach('setup contracts', async function () { |
| 15 | + const accounts = await chain.getUserAccounts(); |
| 16 | + owner = web3.utils.toChecksumAddress(accounts[0]); |
| 17 | + anotherAccount = web3.utils.toChecksumAddress(accounts[8]); |
| 18 | + |
| 19 | + token = await MockERC20.new(1000); |
| 20 | + otherToken = await MockERC20.new(2000); |
| 21 | + |
| 22 | + tokenPool = await TokenPool.new(token.address); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('balance', function() { |
| 26 | + it('should return the balance of the token pool', async function(){ |
| 27 | + await token.transfer(tokenPool.address, 123); |
| 28 | + expect(await tokenPool.balance.call()).to.be.bignumber.equal('123'); |
| 29 | + await tokenPool.transfer(owner, 99); |
| 30 | + expect(await tokenPool.balance.call()).to.be.bignumber.equal('24'); |
| 31 | + await tokenPool.transfer(owner, 24); |
| 32 | + expect(await tokenPool.balance.call()).to.be.bignumber.equal('0'); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('transfer', function() { |
| 37 | + it('should let the owner transfer funds out', async function(){ |
| 38 | + await token.transfer(tokenPool.address, 1000); |
| 39 | + |
| 40 | + expect(await tokenPool.balance.call()).to.be.bignumber.equal('1000'); |
| 41 | + expect(await token.balanceOf.call(anotherAccount)).to.be.bignumber.equal('0'); |
| 42 | + |
| 43 | + await tokenPool.transfer(anotherAccount, 1000); |
| 44 | + |
| 45 | + expect(await tokenPool.balance.call()).to.be.bignumber.equal('0'); |
| 46 | + expect(await token.balanceOf.call(anotherAccount)).to.be.bignumber.equal('1000'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should NOT let other users transfer funds out', async function(){ |
| 50 | + await token.transfer(tokenPool.address, 1000); |
| 51 | + await expectRevert( |
| 52 | + tokenPool.transfer(anotherAccount, 1000, { from: anotherAccount }), |
| 53 | + 'Ownable: caller is not the owner' |
| 54 | + ); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('rescueFunds', function() { |
| 59 | + beforeEach(async function(){ |
| 60 | + await token.transfer(tokenPool.address, 1000); |
| 61 | + await otherToken.transfer(tokenPool.address, 2000); |
| 62 | + |
| 63 | + expect(await tokenPool.balance.call()).to.be.bignumber.equal('1000'); |
| 64 | + expect(await token.balanceOf.call(anotherAccount)).to.be.bignumber.equal('0'); |
| 65 | + expect(await otherToken.balanceOf.call(tokenPool.address)).to.be.bignumber.equal('2000'); |
| 66 | + expect(await otherToken.balanceOf.call(anotherAccount)).to.be.bignumber.equal('0'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should let owner users claim excess funds completely', async function(){ |
| 70 | + await tokenPool.rescueFunds(otherToken.address, anotherAccount, 2000); |
| 71 | + |
| 72 | + expect(await tokenPool.balance.call()).to.be.bignumber.equal('1000'); |
| 73 | + expect(await token.balanceOf.call(anotherAccount)).to.be.bignumber.equal('0'); |
| 74 | + expect(await otherToken.balanceOf.call(tokenPool.address)).to.be.bignumber.equal('0'); |
| 75 | + expect(await otherToken.balanceOf.call(anotherAccount)).to.be.bignumber.equal('2000'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should let owner users claim excess funds partially', async function(){ |
| 79 | + await tokenPool.rescueFunds(otherToken.address, anotherAccount, 777); |
| 80 | + |
| 81 | + expect(await tokenPool.balance.call()).to.be.bignumber.equal('1000'); |
| 82 | + expect(await token.balanceOf.call(anotherAccount)).to.be.bignumber.equal('0'); |
| 83 | + expect(await otherToken.balanceOf.call(tokenPool.address)).to.be.bignumber.equal('1223'); |
| 84 | + expect(await otherToken.balanceOf.call(anotherAccount)).to.be.bignumber.equal('777'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should NOT let owner claim more than available excess funds', async function(){ |
| 88 | + await expectRevert( |
| 89 | + tokenPool.rescueFunds(otherToken.address, anotherAccount, 2001), |
| 90 | + 'ERC20: transfer amount exceeds balance' |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should NOT let owner users claim held funds', async function(){ |
| 95 | + await expectRevert( |
| 96 | + tokenPool.rescueFunds(token.address, anotherAccount, 1000), |
| 97 | + 'TokenPool: Cannot claim token held by the contract' |
| 98 | + ); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should NOT let other users users claim excess funds', async function(){ |
| 102 | + await expectRevert( |
| 103 | + tokenPool.rescueFunds(otherToken.address, anotherAccount, 2000, { from: anotherAccount }), |
| 104 | + 'Ownable: caller is not the owner' |
| 105 | + ); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments