aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/test/ether_token.ts
diff options
context:
space:
mode:
authorLeonid <logvinov.leon@gmail.com>2018-01-31 03:27:14 +0800
committerGitHub <noreply@github.com>2018-01-31 03:27:14 +0800
commit2bcb7d56394c43acd68d1823092de2ad047abc6d (patch)
tree55684889214373fcd66ca52efa079f381cfb388b /packages/contracts/test/ether_token.ts
parentd004df56e38726ed7f1b775da33d8f37aae270db (diff)
parent7ad314472d27e8a6c6ee8242f4f335d414709f4d (diff)
downloaddexon-sol-tools-2bcb7d56394c43acd68d1823092de2ad047abc6d.tar.gz
dexon-sol-tools-2bcb7d56394c43acd68d1823092de2ad047abc6d.tar.zst
dexon-sol-tools-2bcb7d56394c43acd68d1823092de2ad047abc6d.zip
Merge pull request #332 from 0xProject/feature/contracts-abi-gen
Contracts refactor. Bye, truffle!!! :clap:
Diffstat (limited to 'packages/contracts/test/ether_token.ts')
-rw-r--r--packages/contracts/test/ether_token.ts126
1 files changed, 126 insertions, 0 deletions
diff --git a/packages/contracts/test/ether_token.ts b/packages/contracts/test/ether_token.ts
new file mode 100644
index 000000000..4c70534ee
--- /dev/null
+++ b/packages/contracts/test/ether_token.ts
@@ -0,0 +1,126 @@
+import { ZeroEx, ZeroExError } from '0x.js';
+import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
+import { BigNumber, promisify } from '@0xproject/utils';
+import { Web3Wrapper } from '@0xproject/web3-wrapper';
+import * as chai from 'chai';
+
+import { constants } from '../util/constants';
+import { ContractName } from '../util/types';
+
+import { chaiSetup } from './utils/chai_setup';
+import { deployer } from './utils/deployer';
+
+chaiSetup.configure();
+const expect = chai.expect;
+const web3 = web3Factory.create();
+const web3Wrapper = new Web3Wrapper(web3.currentProvider);
+const blockchainLifecycle = new BlockchainLifecycle();
+
+describe('EtherToken', () => {
+ let account: string;
+ const gasPrice = ZeroEx.toBaseUnitAmount(new BigNumber(20), 9);
+ let zeroEx: ZeroEx;
+ let etherTokenAddress: string;
+ before(async () => {
+ const accounts = await web3Wrapper.getAvailableAddressesAsync();
+ account = accounts[0];
+
+ const etherToken = await deployer.deployAsync(ContractName.EtherToken);
+ etherTokenAddress = etherToken.address;
+ zeroEx = new ZeroEx(web3.currentProvider, {
+ gasPrice,
+ networkId: constants.TESTRPC_NETWORK_ID,
+ });
+ });
+ beforeEach(async () => {
+ await blockchainLifecycle.startAsync();
+ });
+ afterEach(async () => {
+ await blockchainLifecycle.revertAsync();
+ });
+ describe('deposit', () => {
+ it('should throw if caller attempts to deposit more Ether than caller balance', async () => {
+ const initEthBalance = await web3Wrapper.getBalanceInWeiAsync(account);
+ const ethToDeposit = initEthBalance.plus(1);
+
+ return expect(zeroEx.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account)).to.be.rejectedWith(
+ ZeroExError.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 ethToDeposit = new BigNumber(web3.toWei(1, 'ether'));
+
+ const txHash = await zeroEx.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account);
+ const receipt = await zeroEx.awaitTransactionMinedAsync(txHash);
+
+ const ethSpentOnGas = gasPrice.times(receipt.gasUsed);
+ const finalEthBalance = await web3Wrapper.getBalanceInWeiAsync(account);
+ const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account);
+
+ expect(finalEthBalance).to.be.bignumber.equal(initEthBalance.minus(ethToDeposit.plus(ethSpentOnGas)));
+ expect(finalEthTokenBalance).to.be.bignumber.equal(initEthTokenBalance.plus(ethToDeposit));
+ });
+ });
+
+ describe('withdraw', () => {
+ it('should throw if caller attempts to withdraw greater than caller balance', async () => {
+ const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account);
+ const ethTokensToWithdraw = initEthTokenBalance.plus(1);
+
+ return expect(
+ zeroEx.etherToken.withdrawAsync(etherTokenAddress, ethTokensToWithdraw, account),
+ ).to.be.rejectedWith(ZeroExError.InsufficientWEthBalanceForWithdrawal);
+ });
+
+ it('should convert ether tokens to ether with sufficient balance', async () => {
+ const ethToDeposit = new BigNumber(web3.toWei(1, 'ether'));
+ await zeroEx.etherToken.depositAsync(etherTokenAddress, ethToDeposit, account);
+ const initEthTokenBalance = await zeroEx.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);
+
+ const ethSpentOnGas = gasPrice.times(receipt.gasUsed);
+ const finalEthBalance = await web3Wrapper.getBalanceInWeiAsync(account);
+ const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account);
+
+ expect(finalEthBalance).to.be.bignumber.equal(
+ initEthBalance.plus(ethTokensToWithdraw.minus(ethSpentOnGas)),
+ );
+ expect(finalEthTokenBalance).to.be.bignumber.equal(initEthTokenBalance.minus(ethTokensToWithdraw));
+ });
+ });
+
+ 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 ethToDeposit = ZeroEx.toBaseUnitAmount(new BigNumber(1), 18);
+
+ const txHash = await web3Wrapper.sendTransactionAsync({
+ from: account,
+ to: etherTokenAddress,
+ value: ethToDeposit,
+ gasPrice,
+ });
+
+ const receipt = await zeroEx.awaitTransactionMinedAsync(txHash);
+
+ const ethSpentOnGas = gasPrice.times(receipt.gasUsed);
+ const finalEthBalance = await web3Wrapper.getBalanceInWeiAsync(account);
+ const finalEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account);
+
+ expect(finalEthBalance).to.be.bignumber.equal(initEthBalance.minus(ethToDeposit.plus(ethSpentOnGas)));
+ expect(finalEthTokenBalance).to.be.bignumber.equal(initEthTokenBalance.plus(ethToDeposit));
+ });
+ });
+});