aboutsummaryrefslogtreecommitdiffstats
path: root/packages/0x.js/test/ether_token_wrapper_test.ts
blob: 72086dff063b92be448401a05b984498983fe7a4 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils';
import { BigNumber } from '@0xproject/utils';
import * as chai from 'chai';
import 'mocha';
import * as Web3 from 'web3';

import {
    ApprovalContractEventArgs,
    BlockParamLiteral,
    BlockRange,
    DecodedLogEvent,
    DepositContractEventArgs,
    EtherTokenEvents,
    Token,
    TransferContractEventArgs,
    WithdrawalContractEventArgs,
    ZeroEx,
    ZeroExError,
} from '../src';
import { DoneCallback } from '../src/types';

import { chaiSetup } from './utils/chai_setup';
import { constants } from './utils/constants';
import { reportNodeCallbackErrors } from './utils/report_callback_errors';
import { TokenUtils } from './utils/token_utils';

chaiSetup.configure();
const expect = chai.expect;
const blockchainLifecycle = new BlockchainLifecycle();

// Since the address depositing/withdrawing ETH/WETH also needs to pay gas costs for the transaction,
// a small amount of ETH will be used to pay this gas cost. We therefore check that the difference between
// the expected balance and actual balance (given the amount of ETH deposited), only deviates by the amount
// required to pay gas costs.
const MAX_REASONABLE_GAS_COST_IN_WEI = 62517;

describe('EtherTokenWrapper', () => {
    let web3: Web3;
    let zeroEx: ZeroEx;
    let tokens: Token[];
    let userAddresses: string[];
    let addressWithETH: string;
    let wethContractAddress: string;
    let depositWeiAmount: BigNumber;
    let decimalPlaces: number;
    let addressWithoutFunds: string;
    const gasPrice = new BigNumber(1);
    const zeroExConfig = {
        gasPrice,
        networkId: constants.TESTRPC_NETWORK_ID,
    };
    const transferAmount = new BigNumber(42);
    const allowanceAmount = new BigNumber(42);
    const depositAmount = new BigNumber(42);
    const withdrawalAmount = new BigNumber(42);
    before(async () => {
        web3 = web3Factory.create();
        zeroEx = new ZeroEx(web3.currentProvider, zeroExConfig);
        tokens = await zeroEx.tokenRegistry.getTokensAsync();
        userAddresses = await zeroEx.getAvailableAddressesAsync();
        addressWithETH = userAddresses[0];
        wethContractAddress = zeroEx.etherToken.getContractAddressIfExists() as string;
        depositWeiAmount = (zeroEx as any)._web3Wrapper.toWei(new BigNumber(5));
        decimalPlaces = 7;
        addressWithoutFunds = userAddresses[1];
    });
    beforeEach(async () => {
        await blockchainLifecycle.startAsync();
    });
    afterEach(async () => {
        await blockchainLifecycle.revertAsync();
    });
    describe('#getContractAddressIfExists', async () => {
        it('should return contract address if connected to a known network', () => {
            const contractAddressIfExists = zeroEx.etherToken.getContractAddressIfExists();
            expect(contractAddressIfExists).to.not.be.undefined();
        });
        it('should throw if connected to a private network and contract addresses are not specified', () => {
            const UNKNOWN_NETWORK_NETWORK_ID = 10;
            expect(
                () =>
                    new ZeroEx(web3.currentProvider, {
                        networkId: UNKNOWN_NETWORK_NETWORK_ID,
                    } as any),
            ).to.throw();
        });
    });
    describe('#depositAsync', () => {
        it('should successfully deposit ETH and issue Wrapped ETH tokens', async () => {
            const preETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInWeiAsync(addressWithETH);
            const preWETHBalance = await zeroEx.token.getBalanceAsync(wethContractAddress, addressWithETH);
            expect(preETHBalance).to.be.bignumber.gt(0);
            expect(preWETHBalance).to.be.bignumber.equal(0);

            const txHash = await zeroEx.etherToken.depositAsync(wethContractAddress, depositWeiAmount, addressWithETH);
            await zeroEx.awaitTransactionMinedAsync(txHash);

            const postETHBalanceInWei = await (zeroEx as any)._web3Wrapper.getBalanceInWeiAsync(addressWithETH);
            const postWETHBalanceInBaseUnits = await zeroEx.token.getBalanceAsync(wethContractAddress, addressWithETH);

            expect(postWETHBalanceInBaseUnits).to.be.bignumber.equal(depositWeiAmount);
            const remainingETHInWei = preETHBalance.minus(depositWeiAmount);
            const gasCost = remainingETHInWei.minus(postETHBalanceInWei);
            expect(gasCost).to.be.bignumber.lte(MAX_REASONABLE_GAS_COST_IN_WEI);
        });
        it('should throw if user has insufficient ETH balance for deposit', async () => {
            const preETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInWeiAsync(addressWithETH);

            const extraETHBalance = (zeroEx as any)._web3Wrapper.toWei(5, 'ether');
            const overETHBalanceinWei = preETHBalance.add(extraETHBalance);

            return expect(
                zeroEx.etherToken.depositAsync(wethContractAddress, overETHBalanceinWei, addressWithETH),
            ).to.be.rejectedWith(ZeroExError.InsufficientEthBalanceForDeposit);
        });
    });
    describe('#withdrawAsync', () => {
        it('should successfully withdraw ETH in return for Wrapped ETH tokens', async () => {
            const ETHBalanceInWei = await (zeroEx as any)._web3Wrapper.getBalanceInWeiAsync(addressWithETH);

            await zeroEx.etherToken.depositAsync(wethContractAddress, depositWeiAmount, addressWithETH);

            const expectedPreETHBalance = ETHBalanceInWei.minus(depositWeiAmount);
            const preETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInWeiAsync(addressWithETH);
            const preWETHBalance = await zeroEx.token.getBalanceAsync(wethContractAddress, addressWithETH);
            let gasCost = expectedPreETHBalance.minus(preETHBalance);
            expect(gasCost).to.be.bignumber.lte(MAX_REASONABLE_GAS_COST_IN_WEI);
            expect(preWETHBalance).to.be.bignumber.equal(depositWeiAmount);

            const txHash = await zeroEx.etherToken.withdrawAsync(wethContractAddress, depositWeiAmount, addressWithETH);
            await zeroEx.awaitTransactionMinedAsync(txHash);

            const postETHBalance = await (zeroEx as any)._web3Wrapper.getBalanceInWeiAsync(addressWithETH);
            const postWETHBalanceInBaseUnits = await zeroEx.token.getBalanceAsync(wethContractAddress, addressWithETH);

            expect(postWETHBalanceInBaseUnits).to.be.bignumber.equal(0);
            const expectedETHBalance = preETHBalance.add(depositWeiAmount).round(decimalPlaces);
            gasCost = expectedETHBalance.minus(postETHBalance);
            expect(gasCost).to.be.bignumber.lte(MAX_REASONABLE_GAS_COST_IN_WEI);
        });
        it('should throw if user has insufficient WETH balance for withdrawl', async () => {
            const preWETHBalance = await zeroEx.token.getBalanceAsync(wethContractAddress, addressWithETH);
            expect(preWETHBalance).to.be.bignumber.equal(0);

            const overWETHBalance = preWETHBalance.add(999999999);

            return expect(
                zeroEx.etherToken.withdrawAsync(wethContractAddress, overWETHBalance, addressWithETH),
            ).to.be.rejectedWith(ZeroExError.InsufficientWEthBalanceForWithdrawal);
        });
    });
    describe('#subscribe', () => {
        const indexFilterValues = {};
        let etherTokenAddress: string;
        before(() => {
            const tokenUtils = new TokenUtils(tokens);
            const etherToken = tokenUtils.getWethTokenOrThrow();
            etherTokenAddress = etherToken.address;
        });
        afterEach(() => {
            zeroEx.etherToken._unsubscribeAll();
        });
        // Hack: Mocha does not allow a test to be both async and have a `done` callback
        // Since we need to await the receipt of the event in the `subscribe` callback,
        // we do need both. A hack is to make the top-level async fn w/ a done callback and then
        // wrap the rest of the test in an async block
        // Source: https://github.com/mochajs/mocha/issues/2407
        it('Should receive the Transfer event when tokens are transfered', (done: DoneCallback) => {
            (async () => {
                const callback = reportNodeCallbackErrors(done)(
                    (logEvent: DecodedLogEvent<TransferContractEventArgs>) => {
                        expect(logEvent).to.not.be.undefined();
                        expect(logEvent.isRemoved).to.be.false();
                        expect(logEvent.log.logIndex).to.be.equal(0);
                        expect(logEvent.log.transactionIndex).to.be.equal(0);
                        expect(logEvent.log.blockNumber).to.be.a('number');
                        const args = logEvent.log.args;
                        expect(args._from).to.be.equal(addressWithETH);
                        expect(args._to).to.be.equal(addressWithoutFunds);
                        expect(args._value).to.be.bignumber.equal(transferAmount);
                    },
                );
                await zeroEx.etherToken.depositAsync(etherTokenAddress, transferAmount, addressWithETH);
                zeroEx.etherToken.subscribe(etherTokenAddress, EtherTokenEvents.Transfer, indexFilterValues, callback);
                await zeroEx.token.transferAsync(
                    etherTokenAddress,
                    addressWithETH,
                    addressWithoutFunds,
                    transferAmount,
                );
            })().catch(done);
        });
        it('Should receive the Approval event when allowance is being set', (done: DoneCallback) => {
            (async () => {
                const callback = reportNodeCallbackErrors(done)(
                    (logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
                        expect(logEvent).to.not.be.undefined();
                        expect(logEvent.isRemoved).to.be.false();
                        const args = logEvent.log.args;
                        expect(args._owner).to.be.equal(addressWithETH);
                        expect(args._spender).to.be.equal(addressWithoutFunds);
                        expect(args._value).to.be.bignumber.equal(allowanceAmount);
                    },
                );
                zeroEx.etherToken.subscribe(etherTokenAddress, EtherTokenEvents.Approval, indexFilterValues, callback);
                await zeroEx.token.setAllowanceAsync(
                    etherTokenAddress,
                    addressWithETH,
                    addressWithoutFunds,
                    allowanceAmount,
                );
            })().catch(done);
        });
        it('Should receive the Deposit event when ether is being deposited', (done: DoneCallback) => {
            (async () => {
                const callback = reportNodeCallbackErrors(done)(
                    (logEvent: DecodedLogEvent<DepositContractEventArgs>) => {
                        expect(logEvent).to.not.be.undefined();
                        expect(logEvent.isRemoved).to.be.false();
                        const args = logEvent.log.args;
                        expect(args._owner).to.be.equal(addressWithETH);
                        expect(args._value).to.be.bignumber.equal(depositAmount);
                    },
                );
                zeroEx.etherToken.subscribe(etherTokenAddress, EtherTokenEvents.Deposit, indexFilterValues, callback);
                await zeroEx.etherToken.depositAsync(etherTokenAddress, depositAmount, addressWithETH);
            })().catch(done);
        });
        it('Should receive the Withdrawal event when ether is being withdrawn', (done: DoneCallback) => {
            (async () => {
                const callback = reportNodeCallbackErrors(done)(
                    (logEvent: DecodedLogEvent<WithdrawalContractEventArgs>) => {
                        expect(logEvent).to.not.be.undefined();
                        expect(logEvent.isRemoved).to.be.false();
                        const args = logEvent.log.args;
                        expect(args._owner).to.be.equal(addressWithETH);
                        expect(args._value).to.be.bignumber.equal(depositAmount);
                    },
                );
                await zeroEx.etherToken.depositAsync(etherTokenAddress, depositAmount, addressWithETH);
                zeroEx.etherToken.subscribe(
                    etherTokenAddress,
                    EtherTokenEvents.Withdrawal,
                    indexFilterValues,
                    callback,
                );
                await zeroEx.etherToken.withdrawAsync(etherTokenAddress, withdrawalAmount, addressWithETH);
            })().catch(done);
        });
        it('should cancel outstanding subscriptions when ZeroEx.setProvider is called', (done: DoneCallback) => {
            (async () => {
                const callbackNeverToBeCalled = reportNodeCallbackErrors(done)(
                    (logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
                        done(new Error('Expected this subscription to have been cancelled'));
                    },
                );
                zeroEx.etherToken.subscribe(
                    etherTokenAddress,
                    EtherTokenEvents.Transfer,
                    indexFilterValues,
                    callbackNeverToBeCalled,
                );
                const callbackToBeCalled = reportNodeCallbackErrors(done)();
                const newProvider = web3Factory.getRpcProvider();
                zeroEx.setProvider(newProvider, constants.TESTRPC_NETWORK_ID);
                await zeroEx.etherToken.depositAsync(etherTokenAddress, transferAmount, addressWithETH);
                zeroEx.etherToken.subscribe(
                    etherTokenAddress,
                    EtherTokenEvents.Transfer,
                    indexFilterValues,
                    callbackToBeCalled,
                );
                await zeroEx.token.transferAsync(
                    etherTokenAddress,
                    addressWithETH,
                    addressWithoutFunds,
                    transferAmount,
                );
            })().catch(done);
        });
        it('Should cancel subscription when unsubscribe called', (done: DoneCallback) => {
            (async () => {
                const callbackNeverToBeCalled = reportNodeCallbackErrors(done)(
                    (logEvent: DecodedLogEvent<ApprovalContractEventArgs>) => {
                        done(new Error('Expected this subscription to have been cancelled'));
                    },
                );
                await zeroEx.etherToken.depositAsync(etherTokenAddress, transferAmount, addressWithETH);
                const subscriptionToken = zeroEx.etherToken.subscribe(
                    etherTokenAddress,
                    EtherTokenEvents.Transfer,
                    indexFilterValues,
                    callbackNeverToBeCalled,
                );
                zeroEx.etherToken.unsubscribe(subscriptionToken);
                await zeroEx.token.transferAsync(
                    etherTokenAddress,
                    addressWithETH,
                    addressWithoutFunds,
                    transferAmount,
                );
                done();
            })().catch(done);
        });
    });
    describe('#getLogsAsync', () => {
        let etherTokenAddress: string;
        let tokenTransferProxyAddress: string;
        const blockRange: BlockRange = {
            fromBlock: 0,
            toBlock: BlockParamLiteral.Latest,
        };
        let txHash: string;
        before(() => {
            addressWithETH = userAddresses[0];
            const tokenUtils = new TokenUtils(tokens);
            const etherToken = tokenUtils.getWethTokenOrThrow();
            etherTokenAddress = etherToken.address;
            tokenTransferProxyAddress = zeroEx.proxy.getContractAddress();
        });
        it('should get logs with decoded args emitted by Approval', async () => {
            txHash = await zeroEx.token.setUnlimitedProxyAllowanceAsync(etherTokenAddress, addressWithETH);
            await zeroEx.awaitTransactionMinedAsync(txHash);
            const eventName = EtherTokenEvents.Approval;
            const indexFilterValues = {};
            const logs = await zeroEx.etherToken.getLogsAsync<ApprovalContractEventArgs>(
                etherTokenAddress,
                eventName,
                blockRange,
                indexFilterValues,
            );
            expect(logs).to.have.length(1);
            const args = logs[0].args;
            expect(logs[0].event).to.be.equal(eventName);
            expect(args._owner).to.be.equal(addressWithETH);
            expect(args._spender).to.be.equal(tokenTransferProxyAddress);
            expect(args._value).to.be.bignumber.equal(zeroEx.token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS);
        });
        it('should get logs with decoded args emitted by Deposit', async () => {
            await zeroEx.etherToken.depositAsync(etherTokenAddress, depositAmount, addressWithETH);
            const eventName = EtherTokenEvents.Deposit;
            const indexFilterValues = {};
            const logs = await zeroEx.etherToken.getLogsAsync<DepositContractEventArgs>(
                etherTokenAddress,
                eventName,
                blockRange,
                indexFilterValues,
            );
            expect(logs).to.have.length(1);
            const args = logs[0].args;
            expect(logs[0].event).to.be.equal(eventName);
            expect(args._owner).to.be.equal(addressWithETH);
            expect(args._value).to.be.bignumber.equal(depositAmount);
        });
        it('should only get the logs with the correct event name', async () => {
            txHash = await zeroEx.token.setUnlimitedProxyAllowanceAsync(etherTokenAddress, addressWithETH);
            await zeroEx.awaitTransactionMinedAsync(txHash);
            const differentEventName = EtherTokenEvents.Transfer;
            const indexFilterValues = {};
            const logs = await zeroEx.etherToken.getLogsAsync(
                etherTokenAddress,
                differentEventName,
                blockRange,
                indexFilterValues,
            );
            expect(logs).to.have.length(0);
        });
        it('should only get the logs with the correct indexed fields', async () => {
            txHash = await zeroEx.token.setUnlimitedProxyAllowanceAsync(etherTokenAddress, addressWithETH);
            await zeroEx.awaitTransactionMinedAsync(txHash);
            txHash = await zeroEx.token.setUnlimitedProxyAllowanceAsync(etherTokenAddress, addressWithoutFunds);
            await zeroEx.awaitTransactionMinedAsync(txHash);
            const eventName = EtherTokenEvents.Approval;
            const indexFilterValues = {
                _owner: addressWithETH,
            };
            const logs = await zeroEx.etherToken.getLogsAsync<ApprovalContractEventArgs>(
                etherTokenAddress,
                eventName,
                blockRange,
                indexFilterValues,
            );
            expect(logs).to.have.length(1);
            const args = logs[0].args;
            expect(args._owner).to.be.equal(addressWithETH);
        });
    });
});