diff options
author | Fabio Berger <me@fabioberger.com> | 2018-05-23 04:15:16 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-05-23 04:15:16 +0800 |
commit | 5a09063adbb3fe506496b3413926d550575a85a5 (patch) | |
tree | 1fc73537dfd95df95bc582aa57c0c3c213486431 | |
parent | 9de9bf651b7cede14ed718a438c48204dec5f989 (diff) | |
download | dexon-0x-contracts-5a09063adbb3fe506496b3413926d550575a85a5.tar.gz dexon-0x-contracts-5a09063adbb3fe506496b3413926d550575a85a5.tar.zst dexon-0x-contracts-5a09063adbb3fe506496b3413926d550575a85a5.zip |
Refactor etherToken test to use contract-wrappers
-rw-r--r-- | packages/contracts/test/ether_token.ts | 58 |
1 files changed, 36 insertions, 22 deletions
diff --git a/packages/contracts/test/ether_token.ts b/packages/contracts/test/ether_token.ts index 8f2a01a4d..90538caf9 100644 --- a/packages/contracts/test/ether_token.ts +++ b/packages/contracts/test/ether_token.ts @@ -1,4 +1,4 @@ -import { ContractWrappersError } from '@0xproject/contract-wrappers'; +import { ContractWrappers, ContractWrappersError } from '@0xproject/contract-wrappers'; import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { BigNumber, promisify } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; @@ -18,7 +18,7 @@ const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper); describe('EtherToken', () => { let account: string; const gasPrice = Web3Wrapper.toBaseUnitAmount(new BigNumber(20), 9); - let zeroEx: ZeroEx; + let contractWrappers: ContractWrappers; let etherTokenAddress: string; before(async () => { @@ -33,7 +33,7 @@ describe('EtherToken', () => { const etherToken = await WETH9Contract.deployFrom0xArtifactAsync(artifacts.EtherToken, provider, txDefaults); etherTokenAddress = etherToken.address; - zeroEx = new ZeroEx(provider, { + contractWrappers = new ContractWrappers(provider, { gasPrice, networkId: constants.TESTRPC_NETWORK_ID, }); @@ -49,23 +49,26 @@ describe('EtherToken', () => { const initEthBalance = await web3Wrapper.getBalanceInWeiAsync(account); const ethToDeposit = initEthBalance.plus(1); - return expect(zeroEx.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account)).to.be.rejectedWith( - ContractWrappersError.InsufficientEthBalanceForDeposit, - ); + return expect( + contractWrappers.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account), + ).to.be.rejectedWith(ContractWrappersError.InsufficientEthBalanceForDeposit); }); it('should convert deposited Ether to wrapped Ether tokens', async () => { const initEthBalance = await web3Wrapper.getBalanceInWeiAsync(account); - const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); + const initEthTokenBalance = await contractWrappers.token.getBalanceAsync(etherTokenAddress, account); const ethToDeposit = new BigNumber(Web3Wrapper.toWei(new BigNumber(1))); - const txHash = await zeroEx.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account); - const receipt = await zeroEx.awaitTransactionMinedAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS); + const txHash = await contractWrappers.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account); + const receipt = await contractWrappers.awaitTransactionMinedAsync( + txHash, + constants.AWAIT_TRANSACTION_MINED_MS, + ); const ethSpentOnGas = gasPrice.times(receipt.gasUsed); const finalEthBalance = await web3Wrapper.getBalanceInWeiAsync(account); - const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); + const finalEthTokenBalance = await contractWrappers.token.getBalanceAsync(etherTokenAddress, account); expect(finalEthBalance).to.be.bignumber.equal(initEthBalance.minus(ethToDeposit.plus(ethSpentOnGas))); expect(finalEthTokenBalance).to.be.bignumber.equal(initEthTokenBalance.plus(ethToDeposit)); @@ -74,29 +77,37 @@ describe('EtherToken', () => { describe('withdraw', () => { it('should throw if caller attempts to withdraw greater than caller balance', async () => { - const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); + const initEthTokenBalance = await contractWrappers.token.getBalanceAsync(etherTokenAddress, account); const ethTokensToWithdraw = initEthTokenBalance.plus(1); return expect( - zeroEx.etherToken.withdrawAsync(etherTokenAddress, ethTokensToWithdraw, account), + contractWrappers.etherToken.withdrawAsync(etherTokenAddress, ethTokensToWithdraw, account), ).to.be.rejectedWith(ContractWrappersError.InsufficientWEthBalanceForWithdrawal); }); it('should convert ether tokens to ether with sufficient balance', async () => { const ethToDeposit = new BigNumber(Web3Wrapper.toWei(new BigNumber(1))); - await zeroEx.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account); - const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); + await contractWrappers.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account); + const initEthTokenBalance = await contractWrappers.token.getBalanceAsync(etherTokenAddress, account); const initEthBalance = await web3Wrapper.getBalanceInWeiAsync(account); const ethTokensToWithdraw = initEthTokenBalance; expect(ethTokensToWithdraw).to.not.be.bignumber.equal(0); - const txHash = await zeroEx.etherToken.withdrawAsync(etherTokenAddress, ethTokensToWithdraw, account, { - gasLimit: constants.MAX_ETHERTOKEN_WITHDRAW_GAS, - }); - const receipt = await zeroEx.awaitTransactionMinedAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS); + const txHash = await contractWrappers.etherToken.withdrawAsync( + etherTokenAddress, + ethTokensToWithdraw, + account, + { + gasLimit: constants.MAX_ETHERTOKEN_WITHDRAW_GAS, + }, + ); + const receipt = await contractWrappers.awaitTransactionMinedAsync( + txHash, + constants.AWAIT_TRANSACTION_MINED_MS, + ); const ethSpentOnGas = gasPrice.times(receipt.gasUsed); const finalEthBalance = await web3Wrapper.getBalanceInWeiAsync(account); - const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); + const finalEthTokenBalance = await contractWrappers.token.getBalanceAsync(etherTokenAddress, account); expect(finalEthBalance).to.be.bignumber.equal( initEthBalance.plus(ethTokensToWithdraw.minus(ethSpentOnGas)), @@ -108,7 +119,7 @@ describe('EtherToken', () => { describe('fallback', () => { it('should convert sent ether to ether tokens', async () => { const initEthBalance = await web3Wrapper.getBalanceInWeiAsync(account); - const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); + const initEthTokenBalance = await contractWrappers.token.getBalanceAsync(etherTokenAddress, account); const ethToDeposit = Web3Wrapper.toBaseUnitAmount(new BigNumber(1), 18); @@ -119,11 +130,14 @@ describe('EtherToken', () => { gasPrice, }); - const receipt = await zeroEx.awaitTransactionMinedAsync(txHash, constants.AWAIT_TRANSACTION_MINED_MS); + const receipt = await contractWrappers.awaitTransactionMinedAsync( + txHash, + constants.AWAIT_TRANSACTION_MINED_MS, + ); const ethSpentOnGas = gasPrice.times(receipt.gasUsed); const finalEthBalance = await web3Wrapper.getBalanceInWeiAsync(account); - const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account); + const finalEthTokenBalance = await contractWrappers.token.getBalanceAsync(etherTokenAddress, account); expect(finalEthBalance).to.be.bignumber.equal(initEthBalance.minus(ethToDeposit.plus(ethSpentOnGas))); expect(finalEthTokenBalance).to.be.bignumber.equal(initEthTokenBalance.plus(ethToDeposit)); |