aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/test/ts/ether_token.ts
blob: 7e98e9befff2a941b88ce11ccb74bc08afb0ff6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {ZeroEx, ZeroExError} from '0x.js';
import {BigNumber} from 'bignumber.js';
import * as chai from 'chai';
import promisify = require('es6-promisify');
import Web3 = require('web3');

import {Artifacts} from '../../util/artifacts';

import {chaiSetup} from './utils/chai_setup';

const {EtherToken} = new Artifacts(artifacts);

chaiSetup.configure();
const expect = chai.expect;

// In order to benefit from type-safety, we re-assign the global web3 instance injected by Truffle
// with type `any` to a variable of type `Web3`.
const web3: Web3 = (global as any).web3;

contract('EtherToken', (accounts: string[]) => {
  const account = accounts[0];
  const gasPrice = ZeroEx.toBaseUnitAmount(new BigNumber(20), 9);
  let zeroEx: ZeroEx;
  let etherTokenAddress: string;
  before(async () => {
    etherTokenAddress = EtherToken.address;
    zeroEx = new ZeroEx(web3.currentProvider, {
        gasPrice,
        etherTokenContractAddress: etherTokenAddress,
    });
  });

  const sendTransactionAsync = promisify(web3.eth.sendTransaction);
  const getEthBalanceAsync = async (owner: string) => {
    const balanceStr = await promisify(web3.eth.getBalance)(owner);
    const balance = new BigNumber(balanceStr);
    return balance;
  };

  describe('deposit', () => {
    it('should throw if caller attempts to deposit more Ether than caller balance', async () => {
      const initEthBalance = await getEthBalanceAsync(account);
      const ethToDeposit = initEthBalance.plus(1);

      return expect(zeroEx.etherToken.depositAsync(ethToDeposit, account))
        .to.be.rejectedWith(ZeroExError.InsufficientEthBalanceForDeposit);
    });

    it('should convert deposited Ether to wrapped Ether tokens', async () => {
      const initEthBalance = await getEthBalanceAsync(account);
      const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account);

      const ethToDeposit = new BigNumber(web3.toWei(1, 'ether'));

      const txHash = await zeroEx.etherToken.depositAsync(ethToDeposit, account);
      const receipt = await zeroEx.awaitTransactionMinedAsync(txHash);

      const ethSpentOnGas = gasPrice.times(receipt.gasUsed);
      const finalEthBalance = await getEthBalanceAsync(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(ethTokensToWithdraw, account))
        .to.be.rejectedWith(ZeroExError.InsufficientWEthBalanceForWithdrawal);
    });

    it('should convert ether tokens to ether with sufficient balance', async () => {
      const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account);
      const initEthBalance = await getEthBalanceAsync(account);
      const ethTokensToWithdraw = initEthTokenBalance;
      expect(ethTokensToWithdraw).to.not.be.bignumber.equal(0);
      const txHash = await zeroEx.etherToken.withdrawAsync(ethTokensToWithdraw, account);
      const receipt = await zeroEx.awaitTransactionMinedAsync(txHash);

      const ethSpentOnGas = gasPrice.times(receipt.gasUsed);
      const finalEthBalance = await getEthBalanceAsync(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 getEthBalanceAsync(account);
      const initEthTokenBalance = await zeroEx.token.getBalanceAsync(etherTokenAddress, account);

      const ethToDeposit = ZeroEx.toBaseUnitAmount(new BigNumber(1), 18);

      const txHash = await sendTransactionAsync({
        from: account,
        to: etherTokenAddress,
        value: ethToDeposit,
        gasPrice,
      });

      const receipt = await zeroEx.awaitTransactionMinedAsync(txHash);

      const ethSpentOnGas = gasPrice.times(receipt.gasUsed);
      const finalEthBalance = await getEthBalanceAsync(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));
    });
  });
});