diff options
-rw-r--r-- | src/contract_wrappers/ether_token_wrapper.ts | 3 | ||||
-rw-r--r-- | src/web3_wrapper.ts | 9 | ||||
-rw-r--r-- | test/ether_token_wrapper_test.ts | 43 |
3 files changed, 30 insertions, 25 deletions
diff --git a/src/contract_wrappers/ether_token_wrapper.ts b/src/contract_wrappers/ether_token_wrapper.ts index e8efbc9a6..76e7289b7 100644 --- a/src/contract_wrappers/ether_token_wrapper.ts +++ b/src/contract_wrappers/ether_token_wrapper.ts @@ -28,8 +28,7 @@ export class EtherTokenWrapper extends ContractWrapper { assert.isBigNumber('amountInWei', amountInWei); await assert.isSenderAddressAsync('depositor', depositor, this._web3Wrapper); - const ethBalance = await this._web3Wrapper.getBalanceInEthAsync(depositor); - const ethBalanceInWei = this._web3Wrapper.toWei(ethBalance); + const ethBalanceInWei = await this._web3Wrapper.getBalanceInWeiAsync(depositor); assert.assert(ethBalanceInWei.gte(amountInWei), ZeroExError.INSUFFICIENT_ETH_BALANCE_FOR_DEPOSIT); const wethContract = await this._getEtherTokenContractAsync(); diff --git a/src/web3_wrapper.ts b/src/web3_wrapper.ts index 0a310aeee..630f0bef3 100644 --- a/src/web3_wrapper.ts +++ b/src/web3_wrapper.ts @@ -38,11 +38,10 @@ export class Web3Wrapper { const balanceWei = this.web3.toWei(ethAmount, 'ether'); return balanceWei; } - public async getBalanceInEthAsync(owner: string): Promise<BigNumber.BigNumber> { - const balanceInWei = await promisify(this.web3.eth.getBalance)(owner); - let balanceEth = this.web3.fromWei(balanceInWei, 'ether'); - balanceEth = new BigNumber(balanceEth); - return balanceEth; + public async getBalanceInWeiAsync(owner: string): Promise<BigNumber.BigNumber> { + let balanceInWei = await promisify(this.web3.eth.getBalance)(owner); + balanceInWei = new BigNumber(balanceInWei); + return balanceInWei; } public async doesContractExistAtAddressAsync(address: string): Promise<boolean> { const code = await promisify(this.web3.eth.getCode)(address); 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); |