Refactor etherToken test to use contract-wrappers

This commit is contained in:
Fabio Berger
2018-05-22 13:15:16 -07:00
parent 9de9bf651b
commit 5a09063adb

View File

@@ -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));