diff options
Diffstat (limited to 'test/ether_token_wrapper_test.ts')
-rw-r--r-- | test/ether_token_wrapper_test.ts | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/test/ether_token_wrapper_test.ts b/test/ether_token_wrapper_test.ts index 60fe00925..ebce81e97 100644 --- a/test/ether_token_wrapper_test.ts +++ b/test/ether_token_wrapper_test.ts @@ -12,13 +12,18 @@ chaiSetup.configure(); const expect = chai.expect; const blockchainLifecycle = new BlockchainLifecycle(); -describe.only('EtherTokenWrapper', () => { +// Since the address depositing/withdrawing ETH/WETH also needs to pay gas costs for the transaction, +// a small amount of ETH will be used to pay this gas cost. We therefore check that the difference between +// the expected balance and actual balance (given the amount of ETH deposited), only deviates by the amount +// required to pay gas costs. +const MAX_REASONABLE_GAS_COST_IN_WEI = 62237; + +describe('EtherTokenWrapper', () => { let web3: Web3; let zeroEx: ZeroEx; let userAddresses: string[]; let addressWithETH: string; let wethContractAddress: string; - let depositETHAmount: BigNumber.BigNumber; let depositWeiAmount: BigNumber.BigNumber; let decimalPlaces: number; before(async () => { @@ -27,8 +32,7 @@ describe.only('EtherTokenWrapper', () => { userAddresses = await promisify(web3.eth.getAccounts)(); addressWithETH = userAddresses[0]; wethContractAddress = await zeroEx.etherToken.getContractAddressAsync(); - depositETHAmount = new BigNumber(5); - depositWeiAmount = (zeroEx as any)._web3Wrapper.toWei(depositETHAmount); + depositWeiAmount = (zeroEx as any)._web3Wrapper.toWei(new BigNumber(5)); decimalPlaces = 7; }); beforeEach(async () => { @@ -39,25 +43,26 @@ describe.only('EtherTokenWrapper', () => { }); describe('#depositAsync', () => { it('should successfully deposit ETH and issue Wrapped ETH tokens', async () => { - const preETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInEthAsync(addressWithETH); + const preETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInWeiAsync(addressWithETH); const preWETHBalance = await zeroEx.token.getBalanceAsync(wethContractAddress, addressWithETH); expect(preETHBalance).to.be.bignumber.gt(0); expect(preWETHBalance).to.be.bignumber.equal(0); await zeroEx.etherToken.depositAsync(depositWeiAmount, addressWithETH); - const postETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInEthAsync(addressWithETH); + const postETHBalanceInWei = await (zeroEx as any)._web3Wrapper.getBalanceInWeiAsync(addressWithETH); const postWETHBalanceInBaseUnits = await zeroEx.token.getBalanceAsync(wethContractAddress, addressWithETH); expect(postWETHBalanceInBaseUnits).to.be.bignumber.equal(depositWeiAmount); - const remainingETH = preETHBalance.minus(depositETHAmount); - return expect(postETHBalance.round(decimalPlaces)).to.be.bignumber.equal(remainingETH); + const remainingETHInWei = preETHBalance.minus(depositWeiAmount); + const gasCost = remainingETHInWei.minus(postETHBalanceInWei); + expect(gasCost).to.be.bignumber.lte(MAX_REASONABLE_GAS_COST_IN_WEI); }); it('should throw if user has insufficient ETH balance for deposit', async () => { - const preETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInEthAsync(addressWithETH); + const preETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInWeiAsync(addressWithETH); - const overETHBalance = preETHBalance.add(5); - const overETHBalanceinWei = (zeroEx as any)._web3Wrapper.toWei(overETHBalance); + const extraETHBalance = (zeroEx as any)._web3Wrapper.toWei(5, 'ether'); + const overETHBalanceinWei = preETHBalance.add(extraETHBalance); return expect( zeroEx.etherToken.depositAsync(overETHBalanceinWei, addressWithETH), @@ -66,24 +71,26 @@ describe.only('EtherTokenWrapper', () => { }); describe('#withdrawAsync', () => { it('should successfully withdraw ETH in return for Wrapped ETH tokens', async () => { - const ETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInEthAsync(addressWithETH); + const ETHBalanceInWei = await (zeroEx as any)._web3Wrapper.getBalanceInWeiAsync(addressWithETH); await zeroEx.etherToken.depositAsync(depositWeiAmount, addressWithETH); - const expectedPreETHBalance = ETHBalance.minus(depositETHAmount); - const preETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInEthAsync(addressWithETH); + const expectedPreETHBalance = ETHBalanceInWei.minus(depositWeiAmount); + const preETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInWeiAsync(addressWithETH); const preWETHBalance = await zeroEx.token.getBalanceAsync(wethContractAddress, addressWithETH); - expect(preETHBalance.round(decimalPlaces)).to.be.bignumber.equal(expectedPreETHBalance); + let gasCost = expectedPreETHBalance.minus(preETHBalance); + expect(gasCost).to.be.bignumber.lte(MAX_REASONABLE_GAS_COST_IN_WEI); expect(preWETHBalance).to.be.bignumber.equal(depositWeiAmount); await zeroEx.etherToken.withdrawAsync(depositWeiAmount, addressWithETH); - const postETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInEthAsync(addressWithETH); + const postETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInWeiAsync(addressWithETH); const postWETHBalanceInBaseUnits = await zeroEx.token.getBalanceAsync(wethContractAddress, addressWithETH); expect(postWETHBalanceInBaseUnits).to.be.bignumber.equal(0); - const expectedETHBalance = preETHBalance.add(depositETHAmount).round(decimalPlaces); - return expect(postETHBalance.round(decimalPlaces)).to.be.bignumber.equal(expectedETHBalance); + const expectedETHBalance = preETHBalance.add(depositWeiAmount).round(decimalPlaces); + gasCost = expectedETHBalance.minus(postETHBalance); + expect(gasCost).to.be.bignumber.lte(MAX_REASONABLE_GAS_COST_IN_WEI); }); it('should throw if user has insufficient WETH balance for withdrawl', async () => { const preWETHBalance = await zeroEx.token.getBalanceAsync(wethContractAddress, addressWithETH); |