aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/test/ts/exchange/wrapper.ts
blob: 77cd03388c2b0779689d2dd2668ecf8216f14fcb (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
import { ZeroEx } from '0x.js';
import { BigNumber } from 'bignumber.js';
import * as chai from 'chai';
import * as _ from 'lodash';

import { Artifacts } from '../../../util/artifacts';
import { Balances } from '../../../util/balances';
import { constants } from '../../../util/constants';
import { ExchangeWrapper } from '../../../util/exchange_wrapper';
import { Order } from '../../../util/order';
import { OrderFactory } from '../../../util/order_factory';
import { BalancesByOwner, ContractInstance } from '../../../util/types';
import { chaiSetup } from '../utils/chai_setup';

chaiSetup.configure();
const expect = chai.expect;
const { Exchange, TokenTransferProxy, DummyToken, TokenRegistry } = new Artifacts(artifacts);

contract('Exchange', (accounts: string[]) => {
    const maker = accounts[0];
    const tokenOwner = accounts[0];
    const taker = accounts[1] || accounts[accounts.length - 1];
    const feeRecipient = accounts[2] || accounts[accounts.length - 1];

    const INIT_BAL = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18);
    const INIT_ALLOW = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18);

    let rep: ContractInstance;
    let dgd: ContractInstance;
    let zrx: ContractInstance;
    let exchange: ContractInstance;
    let tokenRegistry: ContractInstance;

    let balances: BalancesByOwner;

    let exWrapper: ExchangeWrapper;
    let dmyBalances: Balances;
    let orderFactory: OrderFactory;

    before(async () => {
        [tokenRegistry, exchange] = await Promise.all([TokenRegistry.deployed(), Exchange.deployed()]);
        exWrapper = new ExchangeWrapper(exchange);
        const [repAddress, dgdAddress, zrxAddress] = await Promise.all([
            tokenRegistry.getTokenAddressBySymbol('REP'),
            tokenRegistry.getTokenAddressBySymbol('DGD'),
            tokenRegistry.getTokenAddressBySymbol('ZRX'),
        ]);

        const defaultOrderParams = {
            exchangeContractAddress: Exchange.address,
            maker,
            feeRecipient,
            makerToken: repAddress,
            takerToken: dgdAddress,
            makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18),
            takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18),
            makerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18),
            takerFee: ZeroEx.toBaseUnitAmount(new BigNumber(1), 18),
        };
        orderFactory = new OrderFactory(defaultOrderParams);

        [rep, dgd, zrx] = await Promise.all([
            DummyToken.at(repAddress),
            DummyToken.at(dgdAddress),
            DummyToken.at(zrxAddress),
        ]);
        dmyBalances = new Balances([rep, dgd, zrx], [maker, taker, feeRecipient]);
        await Promise.all([
            rep.approve(TokenTransferProxy.address, INIT_ALLOW, { from: maker }),
            rep.approve(TokenTransferProxy.address, INIT_ALLOW, { from: taker }),
            rep.setBalance(maker, INIT_BAL, { from: tokenOwner }),
            rep.setBalance(taker, INIT_BAL, { from: tokenOwner }),
            dgd.approve(TokenTransferProxy.address, INIT_ALLOW, { from: maker }),
            dgd.approve(TokenTransferProxy.address, INIT_ALLOW, { from: taker }),
            dgd.setBalance(maker, INIT_BAL, { from: tokenOwner }),
            dgd.setBalance(taker, INIT_BAL, { from: tokenOwner }),
            zrx.approve(TokenTransferProxy.address, INIT_ALLOW, { from: maker }),
            zrx.approve(TokenTransferProxy.address, INIT_ALLOW, { from: taker }),
            zrx.setBalance(maker, INIT_BAL, { from: tokenOwner }),
            zrx.setBalance(taker, INIT_BAL, { from: tokenOwner }),
        ]);
    });

    describe('fillOrKillOrder', () => {
        beforeEach(async () => {
            balances = await dmyBalances.getAsync();
        });

        it('should transfer the correct amounts', async () => {
            const order = await orderFactory.newSignedOrderAsync({
                makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100), 18),
                takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(200), 18),
            });
            const fillTakerTokenAmount = order.params.takerTokenAmount.div(2);
            await exWrapper.fillOrKillOrderAsync(order, taker, {
                fillTakerTokenAmount,
            });

            const newBalances = await dmyBalances.getAsync();

            const fillMakerTokenAmount = fillTakerTokenAmount
                .times(order.params.makerTokenAmount)
                .dividedToIntegerBy(order.params.takerTokenAmount);
            const makerFee = order.params.makerFee
                .times(fillMakerTokenAmount)
                .dividedToIntegerBy(order.params.makerTokenAmount);
            const takerFee = order.params.takerFee
                .times(fillMakerTokenAmount)
                .dividedToIntegerBy(order.params.makerTokenAmount);
            expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal(
                balances[maker][order.params.makerToken].minus(fillMakerTokenAmount),
            );
            expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal(
                balances[maker][order.params.takerToken].add(fillTakerTokenAmount),
            );
            expect(newBalances[maker][zrx.address]).to.be.bignumber.equal(balances[maker][zrx.address].minus(makerFee));
            expect(newBalances[taker][order.params.takerToken]).to.be.bignumber.equal(
                balances[taker][order.params.takerToken].minus(fillTakerTokenAmount),
            );
            expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal(
                balances[taker][order.params.makerToken].add(fillMakerTokenAmount),
            );
            expect(newBalances[taker][zrx.address]).to.be.bignumber.equal(balances[taker][zrx.address].minus(takerFee));
            expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal(
                balances[feeRecipient][zrx.address].add(makerFee.add(takerFee)),
            );
        });

        it('should throw if an order is expired', async () => {
            const order = await orderFactory.newSignedOrderAsync({
                expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)),
            });

            return expect(exWrapper.fillOrKillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT);
        });

        it('should throw if entire fillTakerTokenAmount not filled', async () => {
            const order = await orderFactory.newSignedOrderAsync();

            const from = taker;
            await exWrapper.fillOrderAsync(order, from, {
                fillTakerTokenAmount: order.params.takerTokenAmount.div(2),
            });

            return expect(exWrapper.fillOrKillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT);
        });
    });

    describe('batch functions', () => {
        let orders: Order[];
        beforeEach(async () => {
            orders = await Promise.all([
                orderFactory.newSignedOrderAsync(),
                orderFactory.newSignedOrderAsync(),
                orderFactory.newSignedOrderAsync(),
            ]);
            balances = await dmyBalances.getAsync();
        });

        describe('batchFillOrders', () => {
            it('should transfer the correct amounts', async () => {
                const fillTakerTokenAmounts: BigNumber[] = [];
                const makerToken = rep.address;
                const takerToken = dgd.address;
                orders.forEach(order => {
                    const fillTakerTokenAmount = order.params.takerTokenAmount.div(2);
                    const fillMakerTokenAmount = fillTakerTokenAmount
                        .times(order.params.makerTokenAmount)
                        .dividedToIntegerBy(order.params.takerTokenAmount);
                    const makerFee = order.params.makerFee
                        .times(fillMakerTokenAmount)
                        .dividedToIntegerBy(order.params.makerTokenAmount);
                    const takerFee = order.params.takerFee
                        .times(fillMakerTokenAmount)
                        .dividedToIntegerBy(order.params.makerTokenAmount);
                    fillTakerTokenAmounts.push(fillTakerTokenAmount);
                    balances[maker][makerToken] = balances[maker][makerToken].minus(fillMakerTokenAmount);
                    balances[maker][takerToken] = balances[maker][takerToken].add(fillTakerTokenAmount);
                    balances[maker][zrx.address] = balances[maker][zrx.address].minus(makerFee);
                    balances[taker][makerToken] = balances[taker][makerToken].add(fillMakerTokenAmount);
                    balances[taker][takerToken] = balances[taker][takerToken].minus(fillTakerTokenAmount);
                    balances[taker][zrx.address] = balances[taker][zrx.address].minus(takerFee);
                    balances[feeRecipient][zrx.address] = balances[feeRecipient][zrx.address].add(
                        makerFee.add(takerFee),
                    );
                });

                await exWrapper.batchFillOrdersAsync(orders, taker, {
                    fillTakerTokenAmounts,
                });

                const newBalances = await dmyBalances.getAsync();
                expect(newBalances).to.be.deep.equal(balances);
            });
        });

        describe('batchFillOrKillOrders', () => {
            it('should transfer the correct amounts', async () => {
                const fillTakerTokenAmounts: BigNumber[] = [];
                const makerToken = rep.address;
                const takerToken = dgd.address;
                orders.forEach(order => {
                    const fillTakerTokenAmount = order.params.takerTokenAmount.div(2);
                    const fillMakerTokenAmount = fillTakerTokenAmount
                        .times(order.params.makerTokenAmount)
                        .dividedToIntegerBy(order.params.takerTokenAmount);
                    const makerFee = order.params.makerFee
                        .times(fillMakerTokenAmount)
                        .dividedToIntegerBy(order.params.makerTokenAmount);
                    const takerFee = order.params.takerFee
                        .times(fillMakerTokenAmount)
                        .dividedToIntegerBy(order.params.makerTokenAmount);
                    fillTakerTokenAmounts.push(fillTakerTokenAmount);
                    balances[maker][makerToken] = balances[maker][makerToken].minus(fillMakerTokenAmount);
                    balances[maker][takerToken] = balances[maker][takerToken].add(fillTakerTokenAmount);
                    balances[maker][zrx.address] = balances[maker][zrx.address].minus(makerFee);
                    balances[taker][makerToken] = balances[taker][makerToken].add(fillMakerTokenAmount);
                    balances[taker][takerToken] = balances[taker][takerToken].minus(fillTakerTokenAmount);
                    balances[taker][zrx.address] = balances[taker][zrx.address].minus(takerFee);
                    balances[feeRecipient][zrx.address] = balances[feeRecipient][zrx.address].add(
                        makerFee.add(takerFee),
                    );
                });

                await exWrapper.batchFillOrKillOrdersAsync(orders, taker, {
                    fillTakerTokenAmounts,
                });

                const newBalances = await dmyBalances.getAsync();
                expect(newBalances).to.be.deep.equal(balances);
            });

            it('should throw if a single order does not fill the expected amount', async () => {
                const fillTakerTokenAmounts: BigNumber[] = [];
                orders.forEach(order => {
                    const fillTakerTokenAmount = order.params.takerTokenAmount.div(2);
                    fillTakerTokenAmounts.push(fillTakerTokenAmount);
                });

                await exWrapper.fillOrKillOrderAsync(orders[0], taker);

                return expect(
                    exWrapper.batchFillOrKillOrdersAsync(orders, taker, {
                        fillTakerTokenAmounts,
                    }),
                ).to.be.rejectedWith(constants.REVERT);
            });
        });

        describe('fillOrdersUpTo', () => {
            it('should stop when the entire fillTakerTokenAmount is filled', async () => {
                const fillTakerTokenAmount = orders[0].params.takerTokenAmount.plus(
                    orders[1].params.takerTokenAmount.div(2),
                );
                await exWrapper.fillOrdersUpToAsync(orders, taker, {
                    fillTakerTokenAmount,
                });

                const newBalances = await dmyBalances.getAsync();

                const fillMakerTokenAmount = orders[0].params.makerTokenAmount.add(
                    orders[1].params.makerTokenAmount.dividedToIntegerBy(2),
                );
                const makerFee = orders[0].params.makerFee.add(orders[1].params.makerFee.dividedToIntegerBy(2));
                const takerFee = orders[0].params.takerFee.add(orders[1].params.takerFee.dividedToIntegerBy(2));
                expect(newBalances[maker][orders[0].params.makerToken]).to.be.bignumber.equal(
                    balances[maker][orders[0].params.makerToken].minus(fillMakerTokenAmount),
                );
                expect(newBalances[maker][orders[0].params.takerToken]).to.be.bignumber.equal(
                    balances[maker][orders[0].params.takerToken].add(fillTakerTokenAmount),
                );
                expect(newBalances[maker][zrx.address]).to.be.bignumber.equal(
                    balances[maker][zrx.address].minus(makerFee),
                );
                expect(newBalances[taker][orders[0].params.takerToken]).to.be.bignumber.equal(
                    balances[taker][orders[0].params.takerToken].minus(fillTakerTokenAmount),
                );
                expect(newBalances[taker][orders[0].params.makerToken]).to.be.bignumber.equal(
                    balances[taker][orders[0].params.makerToken].add(fillMakerTokenAmount),
                );
                expect(newBalances[taker][zrx.address]).to.be.bignumber.equal(
                    balances[taker][zrx.address].minus(takerFee),
                );
                expect(newBalances[feeRecipient][zrx.address]).to.be.bignumber.equal(
                    balances[feeRecipient][zrx.address].add(makerFee.add(takerFee)),
                );
            });

            it('should fill all orders if cannot fill entire fillTakerTokenAmount', async () => {
                const fillTakerTokenAmount = ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18);
                orders.forEach(order => {
                    balances[maker][order.params.makerToken] = balances[maker][order.params.makerToken].minus(
                        order.params.makerTokenAmount,
                    );
                    balances[maker][order.params.takerToken] = balances[maker][order.params.takerToken].add(
                        order.params.takerTokenAmount,
                    );
                    balances[maker][zrx.address] = balances[maker][zrx.address].minus(order.params.makerFee);
                    balances[taker][order.params.makerToken] = balances[taker][order.params.makerToken].add(
                        order.params.makerTokenAmount,
                    );
                    balances[taker][order.params.takerToken] = balances[taker][order.params.takerToken].minus(
                        order.params.takerTokenAmount,
                    );
                    balances[taker][zrx.address] = balances[taker][zrx.address].minus(order.params.takerFee);
                    balances[feeRecipient][zrx.address] = balances[feeRecipient][zrx.address].add(
                        order.params.makerFee.add(order.params.takerFee),
                    );
                });
                await exWrapper.fillOrdersUpToAsync(orders, taker, {
                    fillTakerTokenAmount,
                });

                const newBalances = await dmyBalances.getAsync();
                expect(newBalances).to.be.deep.equal(balances);
            });

            it('should throw when an order does not use the same takerToken', async () => {
                orders = await Promise.all([
                    orderFactory.newSignedOrderAsync(),
                    orderFactory.newSignedOrderAsync({ takerToken: zrx.address }),
                    orderFactory.newSignedOrderAsync(),
                ]);

                return expect(
                    exWrapper.fillOrdersUpToAsync(orders, taker, {
                        fillTakerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(1000), 18),
                    }),
                ).to.be.rejectedWith(constants.REVERT);
            });
        });

        describe('batchCancelOrders', () => {
            it('should be able to cancel multiple orders', async () => {
                const cancelTakerTokenAmounts = _.map(orders, order => order.params.takerTokenAmount);
                await exWrapper.batchCancelOrdersAsync(orders, maker, {
                    cancelTakerTokenAmounts,
                });

                await exWrapper.batchFillOrdersAsync(orders, taker, {
                    fillTakerTokenAmounts: cancelTakerTokenAmounts,
                });
                const newBalances = await dmyBalances.getAsync();
                expect(balances).to.be.deep.equal(newBalances);
            });
        });
    });
});