diff options
Diffstat (limited to 'packages/contracts/test/exchange/wrapper.ts')
-rw-r--r-- | packages/contracts/test/exchange/wrapper.ts | 211 |
1 files changed, 105 insertions, 106 deletions
diff --git a/packages/contracts/test/exchange/wrapper.ts b/packages/contracts/test/exchange/wrapper.ts index b1851a55c..bf5a89222 100644 --- a/packages/contracts/test/exchange/wrapper.ts +++ b/packages/contracts/test/exchange/wrapper.ts @@ -1,4 +1,4 @@ -import { ZeroEx } from '0x.js'; +import { SignedOrder, ZeroEx } from '0x.js'; import { BlockchainLifecycle, devConstants, web3Factory } from '@0xproject/dev-utils'; import { BigNumber } from '@0xproject/utils'; import { Web3Wrapper } from '@0xproject/web3-wrapper'; @@ -13,7 +13,6 @@ import { TokenTransferProxyContract } from '../../src/contract_wrappers/generate 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, ContractName } from '../../util/types'; import { chaiSetup } from '../utils/chai_setup'; @@ -76,15 +75,15 @@ describe('Exchange', () => { exchangeContractAddress: exchange.address, maker, feeRecipient, - makerToken: rep.address, - takerToken: dgd.address, + makerTokenAddress: rep.address, + takerTokenAddress: dgd.address, 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(web3Wrapper, defaultOrderParams); + orderFactory = new OrderFactory(zeroEx, defaultOrderParams); dmyBalances = new Balances([rep, dgd, zrx], [maker, taker, feeRecipient]); await Promise.all([ rep.approve.sendTransactionAsync(tokenTransferProxy.address, INIT_ALLOW, { from: maker }), @@ -113,38 +112,38 @@ describe('Exchange', () => { }); it('should transfer the correct amounts', async () => { - const order = await orderFactory.newSignedOrderAsync({ + const signedOrder = 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, { + const fillTakerTokenAmount = signedOrder.takerTokenAmount.div(2); + await exWrapper.fillOrKillOrderAsync(signedOrder, taker, { fillTakerTokenAmount, }); const newBalances = await dmyBalances.getAsync(); const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); - const makerFee = order.params.makerFee + .times(signedOrder.makerTokenAmount) + .dividedToIntegerBy(signedOrder.takerTokenAmount); + const makerFee = signedOrder.makerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - const takerFee = order.params.takerFee + .dividedToIntegerBy(signedOrder.makerTokenAmount); + const takerFee = signedOrder.takerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - expect(newBalances[maker][order.params.makerToken]).to.be.bignumber.equal( - balances[maker][order.params.makerToken].minus(fillMakerTokenAmount), + .dividedToIntegerBy(signedOrder.makerTokenAmount); + expect(newBalances[maker][signedOrder.makerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrder.makerTokenAddress].minus(fillMakerTokenAmount), ); - expect(newBalances[maker][order.params.takerToken]).to.be.bignumber.equal( - balances[maker][order.params.takerToken].add(fillTakerTokenAmount), + expect(newBalances[maker][signedOrder.takerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrder.takerTokenAddress].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][signedOrder.takerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrder.takerTokenAddress].minus(fillTakerTokenAmount), ); - expect(newBalances[taker][order.params.makerToken]).to.be.bignumber.equal( - balances[taker][order.params.makerToken].add(fillMakerTokenAmount), + expect(newBalances[taker][signedOrder.makerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrder.makerTokenAddress].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( @@ -152,30 +151,30 @@ describe('Exchange', () => { ); }); - it('should throw if an order is expired', async () => { - const order = await orderFactory.newSignedOrderAsync({ - expirationTimestampInSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), + it('should throw if an signedOrder is expired', async () => { + const signedOrder = await orderFactory.newSignedOrderAsync({ + expirationUnixTimestampSec: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), }); - return expect(exWrapper.fillOrKillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); + return expect(exWrapper.fillOrKillOrderAsync(signedOrder, taker)).to.be.rejectedWith(constants.REVERT); }); it('should throw if entire fillTakerTokenAmount not filled', async () => { - const order = await orderFactory.newSignedOrderAsync(); + const signedOrder = await orderFactory.newSignedOrderAsync(); const from = taker; - await exWrapper.fillOrderAsync(order, from, { - fillTakerTokenAmount: order.params.takerTokenAmount.div(2), + await exWrapper.fillOrderAsync(signedOrder, from, { + fillTakerTokenAmount: signedOrder.takerTokenAmount.div(2), }); - return expect(exWrapper.fillOrKillOrderAsync(order, taker)).to.be.rejectedWith(constants.REVERT); + return expect(exWrapper.fillOrKillOrderAsync(signedOrder, taker)).to.be.rejectedWith(constants.REVERT); }); }); describe('batch functions', () => { - let orders: Order[]; + let signedOrders: SignedOrder[]; beforeEach(async () => { - orders = await Promise.all([ + signedOrders = await Promise.all([ orderFactory.newSignedOrderAsync(), orderFactory.newSignedOrderAsync(), orderFactory.newSignedOrderAsync(), @@ -186,32 +185,32 @@ describe('Exchange', () => { 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 makerTokenAddress = rep.address; + const takerTokenAddress = dgd.address; + signedOrders.forEach(signedOrder => { + const fillTakerTokenAmount = signedOrder.takerTokenAmount.div(2); const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); - const makerFee = order.params.makerFee + .times(signedOrder.makerTokenAmount) + .dividedToIntegerBy(signedOrder.takerTokenAmount); + const makerFee = signedOrder.makerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - const takerFee = order.params.takerFee + .dividedToIntegerBy(signedOrder.makerTokenAmount); + const takerFee = signedOrder.takerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); + .dividedToIntegerBy(signedOrder.makerTokenAmount); fillTakerTokenAmounts.push(fillTakerTokenAmount); - balances[maker][makerToken] = balances[maker][makerToken].minus(fillMakerTokenAmount); - balances[maker][takerToken] = balances[maker][takerToken].add(fillTakerTokenAmount); + balances[maker][makerTokenAddress] = balances[maker][makerTokenAddress].minus(fillMakerTokenAmount); + balances[maker][takerTokenAddress] = balances[maker][takerTokenAddress].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][makerTokenAddress] = balances[taker][makerTokenAddress].add(fillMakerTokenAmount); + balances[taker][takerTokenAddress] = balances[taker][takerTokenAddress].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, { + await exWrapper.batchFillOrdersAsync(signedOrders, taker, { fillTakerTokenAmounts, }); @@ -223,32 +222,32 @@ describe('Exchange', () => { 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 makerTokenAddress = rep.address; + const takerTokenAddress = dgd.address; + signedOrders.forEach(signedOrder => { + const fillTakerTokenAmount = signedOrder.takerTokenAmount.div(2); const fillMakerTokenAmount = fillTakerTokenAmount - .times(order.params.makerTokenAmount) - .dividedToIntegerBy(order.params.takerTokenAmount); - const makerFee = order.params.makerFee + .times(signedOrder.makerTokenAmount) + .dividedToIntegerBy(signedOrder.takerTokenAmount); + const makerFee = signedOrder.makerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); - const takerFee = order.params.takerFee + .dividedToIntegerBy(signedOrder.makerTokenAmount); + const takerFee = signedOrder.takerFee .times(fillMakerTokenAmount) - .dividedToIntegerBy(order.params.makerTokenAmount); + .dividedToIntegerBy(signedOrder.makerTokenAmount); fillTakerTokenAmounts.push(fillTakerTokenAmount); - balances[maker][makerToken] = balances[maker][makerToken].minus(fillMakerTokenAmount); - balances[maker][takerToken] = balances[maker][takerToken].add(fillTakerTokenAmount); + balances[maker][makerTokenAddress] = balances[maker][makerTokenAddress].minus(fillMakerTokenAmount); + balances[maker][takerTokenAddress] = balances[maker][takerTokenAddress].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][makerTokenAddress] = balances[taker][makerTokenAddress].add(fillMakerTokenAmount); + balances[taker][takerTokenAddress] = balances[taker][takerTokenAddress].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, { + await exWrapper.batchFillOrKillOrdersAsync(signedOrders, taker, { fillTakerTokenAmounts, }); @@ -256,17 +255,17 @@ describe('Exchange', () => { expect(newBalances).to.be.deep.equal(balances); }); - it('should throw if a single order does not fill the expected amount', async () => { + it('should throw if a single signedOrder does not fill the expected amount', async () => { const fillTakerTokenAmounts: BigNumber[] = []; - orders.forEach(order => { - const fillTakerTokenAmount = order.params.takerTokenAmount.div(2); + signedOrders.forEach(signedOrder => { + const fillTakerTokenAmount = signedOrder.takerTokenAmount.div(2); fillTakerTokenAmounts.push(fillTakerTokenAmount); }); - await exWrapper.fillOrKillOrderAsync(orders[0], taker); + await exWrapper.fillOrKillOrderAsync(signedOrders[0], taker); return expect( - exWrapper.batchFillOrKillOrdersAsync(orders, taker, { + exWrapper.batchFillOrKillOrdersAsync(signedOrders, taker, { fillTakerTokenAmounts, }), ).to.be.rejectedWith(constants.REVERT); @@ -275,34 +274,34 @@ describe('Exchange', () => { 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), + const fillTakerTokenAmount = signedOrders[0].takerTokenAmount.plus( + signedOrders[1].takerTokenAmount.div(2), ); - await exWrapper.fillOrdersUpToAsync(orders, taker, { + await exWrapper.fillOrdersUpToAsync(signedOrders, taker, { fillTakerTokenAmount, }); const newBalances = await dmyBalances.getAsync(); - const fillMakerTokenAmount = orders[0].params.makerTokenAmount.add( - orders[1].params.makerTokenAmount.dividedToIntegerBy(2), + const fillMakerTokenAmount = signedOrders[0].makerTokenAmount.add( + signedOrders[1].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), + const makerFee = signedOrders[0].makerFee.add(signedOrders[1].makerFee.dividedToIntegerBy(2)); + const takerFee = signedOrders[0].takerFee.add(signedOrders[1].takerFee.dividedToIntegerBy(2)); + expect(newBalances[maker][signedOrders[0].makerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrders[0].makerTokenAddress].minus(fillMakerTokenAmount), ); - expect(newBalances[maker][orders[0].params.takerToken]).to.be.bignumber.equal( - balances[maker][orders[0].params.takerToken].add(fillTakerTokenAmount), + expect(newBalances[maker][signedOrders[0].takerTokenAddress]).to.be.bignumber.equal( + balances[maker][signedOrders[0].takerTokenAddress].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][signedOrders[0].takerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrders[0].takerTokenAddress].minus(fillTakerTokenAmount), ); - expect(newBalances[taker][orders[0].params.makerToken]).to.be.bignumber.equal( - balances[taker][orders[0].params.makerToken].add(fillMakerTokenAmount), + expect(newBalances[taker][signedOrders[0].makerTokenAddress]).to.be.bignumber.equal( + balances[taker][signedOrders[0].makerTokenAddress].add(fillMakerTokenAmount), ); expect(newBalances[taker][zrx.address]).to.be.bignumber.equal( balances[taker][zrx.address].minus(takerFee), @@ -312,28 +311,28 @@ describe('Exchange', () => { ); }); - it('should fill all orders if cannot fill entire fillTakerTokenAmount', async () => { + it('should fill all signedOrders 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, + signedOrders.forEach(signedOrder => { + balances[maker][signedOrder.makerTokenAddress] = balances[maker][ + signedOrder.makerTokenAddress + ].minus(signedOrder.makerTokenAmount); + balances[maker][signedOrder.takerTokenAddress] = balances[maker][signedOrder.takerTokenAddress].add( + signedOrder.takerTokenAmount, ); - balances[maker][order.params.takerToken] = balances[maker][order.params.takerToken].add( - order.params.takerTokenAmount, + balances[maker][zrx.address] = balances[maker][zrx.address].minus(signedOrder.makerFee); + balances[taker][signedOrder.makerTokenAddress] = balances[taker][signedOrder.makerTokenAddress].add( + signedOrder.makerTokenAmount, ); - 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[taker][signedOrder.takerTokenAddress] = balances[taker][ + signedOrder.takerTokenAddress + ].minus(signedOrder.takerTokenAmount); + balances[taker][zrx.address] = balances[taker][zrx.address].minus(signedOrder.takerFee); balances[feeRecipient][zrx.address] = balances[feeRecipient][zrx.address].add( - order.params.makerFee.add(order.params.takerFee), + signedOrder.makerFee.add(signedOrder.takerFee), ); }); - await exWrapper.fillOrdersUpToAsync(orders, taker, { + await exWrapper.fillOrdersUpToAsync(signedOrders, taker, { fillTakerTokenAmount, }); @@ -341,15 +340,15 @@ describe('Exchange', () => { expect(newBalances).to.be.deep.equal(balances); }); - it('should throw when an order does not use the same takerToken', async () => { - orders = await Promise.all([ + it('should throw when an signedOrder does not use the same takerTokenAddress', async () => { + signedOrders = await Promise.all([ orderFactory.newSignedOrderAsync(), - orderFactory.newSignedOrderAsync({ takerToken: zrx.address }), + orderFactory.newSignedOrderAsync({ takerTokenAddress: zrx.address }), orderFactory.newSignedOrderAsync(), ]); return expect( - exWrapper.fillOrdersUpToAsync(orders, taker, { + exWrapper.fillOrdersUpToAsync(signedOrders, taker, { fillTakerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(1000), 18), }), ).to.be.rejectedWith(constants.REVERT); @@ -357,13 +356,13 @@ describe('Exchange', () => { }); describe('batchCancelOrders', () => { - it('should be able to cancel multiple orders', async () => { - const cancelTakerTokenAmounts = _.map(orders, order => order.params.takerTokenAmount); - await exWrapper.batchCancelOrdersAsync(orders, maker, { + it('should be able to cancel multiple signedOrders', async () => { + const cancelTakerTokenAmounts = _.map(signedOrders, signedOrder => signedOrder.takerTokenAmount); + await exWrapper.batchCancelOrdersAsync(signedOrders, maker, { cancelTakerTokenAmounts, }); - await exWrapper.batchFillOrdersAsync(orders, taker, { + await exWrapper.batchFillOrdersAsync(signedOrders, taker, { fillTakerTokenAmounts: cancelTakerTokenAmounts, }); const newBalances = await dmyBalances.getAsync(); |