aboutsummaryrefslogtreecommitdiffstats
path: root/contracts/core/test/tokens/weth9.ts
diff options
context:
space:
mode:
authorfragosti <francesco.agosti93@gmail.com>2018-12-04 06:48:15 +0800
committerfragosti <francesco.agosti93@gmail.com>2018-12-04 06:48:15 +0800
commit7086bd32ee23f0931be26194eb28af8178f858eb (patch)
tree53764373a8228b00d88939fdecc6f6a1ae633c9e /contracts/core/test/tokens/weth9.ts
parent50bfbda79a312651581f03614c1b4f4cbbe49cf1 (diff)
parent50df67e7511460f051f91785bb4384485077ef60 (diff)
downloaddexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar
dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.gz
dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.bz2
dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.lz
dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.xz
dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.tar.zst
dexon-sol-tools-7086bd32ee23f0931be26194eb28af8178f858eb.zip
Merge branch 'development' of https://github.com/0xProject/0x-monorepo into feature/website/instant-configurator
Diffstat (limited to 'contracts/core/test/tokens/weth9.ts')
-rw-r--r--contracts/core/test/tokens/weth9.ts138
1 files changed, 138 insertions, 0 deletions
diff --git a/contracts/core/test/tokens/weth9.ts b/contracts/core/test/tokens/weth9.ts
new file mode 100644
index 000000000..9a31dc3f2
--- /dev/null
+++ b/contracts/core/test/tokens/weth9.ts
@@ -0,0 +1,138 @@
+import { BlockchainLifecycle } from '@0x/dev-utils';
+import { BigNumber } from '@0x/utils';
+import { Web3Wrapper } from '@0x/web3-wrapper';
+import * as chai from 'chai';
+
+import { WETH9Contract } from '../../generated-wrappers/weth9';
+import { artifacts } from '../../src/artifacts';
+import { expectInsufficientFundsAsync, expectTransactionFailedWithoutReasonAsync } from '../utils/assertions';
+import { chaiSetup } from '../utils/chai_setup';
+import { constants } from '../utils/constants';
+import { provider, txDefaults, web3Wrapper } from '../utils/web3_wrapper';
+
+chaiSetup.configure();
+const expect = chai.expect;
+const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
+
+describe('EtherToken', () => {
+ let account: string;
+ const gasPrice = Web3Wrapper.toBaseUnitAmount(new BigNumber(20), 9);
+ let etherToken: WETH9Contract;
+
+ before(async () => {
+ await blockchainLifecycle.startAsync();
+ });
+ after(async () => {
+ await blockchainLifecycle.revertAsync();
+ });
+ before(async () => {
+ const accounts = await web3Wrapper.getAvailableAddressesAsync();
+ account = accounts[0];
+
+ etherToken = await WETH9Contract.deployFrom0xArtifactAsync(artifacts.WETH9, provider, {
+ gasPrice,
+ ...txDefaults,
+ });
+ });
+ 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 expectInsufficientFundsAsync(etherToken.deposit.sendTransactionAsync({ value: ethToDeposit }));
+ });
+
+ it('should convert deposited Ether to wrapped Ether tokens', async () => {
+ const initEthBalance = await web3Wrapper.getBalanceInWeiAsync(account);
+ const initEthTokenBalance = await etherToken.balanceOf.callAsync(account);
+
+ const ethToDeposit = new BigNumber(Web3Wrapper.toWei(new BigNumber(1)));
+
+ const txHash = await etherToken.deposit.sendTransactionAsync({ value: ethToDeposit });
+ const receipt = await web3Wrapper.awaitTransactionSuccessAsync(
+ txHash,
+ constants.AWAIT_TRANSACTION_MINED_MS,
+ );
+
+ const ethSpentOnGas = gasPrice.times(receipt.gasUsed);
+ const finalEthBalance = await web3Wrapper.getBalanceInWeiAsync(account);
+ const finalEthTokenBalance = await etherToken.balanceOf.callAsync(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 etherToken.balanceOf.callAsync(account);
+ const ethTokensToWithdraw = initEthTokenBalance.plus(1);
+
+ return expectTransactionFailedWithoutReasonAsync(
+ etherToken.withdraw.sendTransactionAsync(ethTokensToWithdraw),
+ );
+ });
+
+ it('should convert ether tokens to ether with sufficient balance', async () => {
+ const ethToDeposit = new BigNumber(Web3Wrapper.toWei(new BigNumber(1)));
+ await web3Wrapper.awaitTransactionSuccessAsync(
+ await etherToken.deposit.sendTransactionAsync({ value: ethToDeposit }),
+ constants.AWAIT_TRANSACTION_MINED_MS,
+ );
+ const initEthTokenBalance = await etherToken.balanceOf.callAsync(account);
+ const initEthBalance = await web3Wrapper.getBalanceInWeiAsync(account);
+ const ethTokensToWithdraw = initEthTokenBalance;
+ expect(ethTokensToWithdraw).to.not.be.bignumber.equal(0);
+ const txHash = await etherToken.withdraw.sendTransactionAsync(ethTokensToWithdraw, {
+ gas: constants.MAX_ETHERTOKEN_WITHDRAW_GAS,
+ });
+ const receipt = await web3Wrapper.awaitTransactionSuccessAsync(
+ txHash,
+ constants.AWAIT_TRANSACTION_MINED_MS,
+ );
+
+ const ethSpentOnGas = gasPrice.times(receipt.gasUsed);
+ const finalEthBalance = await web3Wrapper.getBalanceInWeiAsync(account);
+ const finalEthTokenBalance = await etherToken.balanceOf.callAsync(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 etherToken.balanceOf.callAsync(account);
+
+ const ethToDeposit = Web3Wrapper.toBaseUnitAmount(new BigNumber(1), 18);
+
+ const txHash = await web3Wrapper.sendTransactionAsync({
+ from: account,
+ to: etherToken.address,
+ value: ethToDeposit,
+ gasPrice,
+ });
+
+ const receipt = await web3Wrapper.awaitTransactionSuccessAsync(
+ txHash,
+ constants.AWAIT_TRANSACTION_MINED_MS,
+ );
+
+ const ethSpentOnGas = gasPrice.times(receipt.gasUsed);
+ const finalEthBalance = await web3Wrapper.getBalanceInWeiAsync(account);
+ const finalEthTokenBalance = await etherToken.balanceOf.callAsync(account);
+
+ expect(finalEthBalance).to.be.bignumber.equal(initEthBalance.minus(ethToDeposit.plus(ethSpentOnGas)));
+ expect(finalEthTokenBalance).to.be.bignumber.equal(initEthTokenBalance.plus(ethToDeposit));
+ });
+ });
+});