diff options
-rw-r--r-- | packages/contracts/src/utils/exchange_wrapper.ts | 42 | ||||
-rw-r--r-- | packages/contracts/test/exchange/core.ts | 112 | ||||
-rw-r--r-- | packages/contracts/test/exchange/wrapper.ts | 320 |
3 files changed, 344 insertions, 130 deletions
diff --git a/packages/contracts/src/utils/exchange_wrapper.ts b/packages/contracts/src/utils/exchange_wrapper.ts index 9dd414be2..b53519871 100644 --- a/packages/contracts/src/utils/exchange_wrapper.ts +++ b/packages/contracts/src/utils/exchange_wrapper.ts @@ -144,6 +144,27 @@ export class ExchangeWrapper { }); return tx; } + public async batchFillOrdersNoThrowAsync( + orders: SignedOrder[], + from: string, + opts: { takerTokenFillAmounts?: BigNumber[] } = {}, + ): Promise<TransactionReceiptWithDecodedLogs> { + const params = formatters.createBatchFill(orders, opts.takerTokenFillAmounts); + const txHash = await this._exchange.batchFillOrdersNoThrow.sendTransactionAsync( + params.orders, + params.takerTokenFillAmounts, + params.signatures, + { from }, + ); + const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); + tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); + tx.logs = _.map(tx.logs, log => { + const logWithDecodedArgs = this._logDecoder.decodeLogOrThrow(log); + wrapLogBigNumbers(logWithDecodedArgs); + return logWithDecodedArgs; + }); + return tx; + } public async marketFillOrdersAsync( orders: SignedOrder[], from: string, @@ -165,6 +186,27 @@ export class ExchangeWrapper { }); return tx; } + public async marketFillOrdersNoThrowAsync( + orders: SignedOrder[], + from: string, + opts: { takerTokenFillAmount: BigNumber }, + ): Promise<TransactionReceiptWithDecodedLogs> { + const params = formatters.createMarketFillOrders(orders, opts.takerTokenFillAmount); + const txHash = await this._exchange.marketFillOrdersNoThrow.sendTransactionAsync( + params.orders, + params.takerTokenFillAmount, + params.signatures, + { from }, + ); + const tx = await this._zeroEx.awaitTransactionMinedAsync(txHash); + tx.logs = _.filter(tx.logs, log => log.address === this._exchange.address); + tx.logs = _.map(tx.logs, log => { + const logWithDecodedArgs = this._logDecoder.decodeLogOrThrow(log); + wrapLogBigNumbers(logWithDecodedArgs); + return logWithDecodedArgs; + }); + return tx; + } public async batchCancelOrdersAsync( orders: SignedOrder[], from: string, diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts index 9b8f9e1d2..c31f7ab74 100644 --- a/packages/contracts/test/exchange/core.ts +++ b/packages/contracts/test/exchange/core.ts @@ -538,17 +538,6 @@ describe('Exchange', () => { ).to.be.rejectedWith(constants.REVERT); }); - // it('should not change balances if maker balances are too low to fill order and \ - // shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - // signedOrder = await orderFactory.newSignedOrderAsync({ - // makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), - // }); - - // await exWrapper.fillOrderAsync(signedOrder, taker); - // const newBalances = await dmyBalances.getAsync(); - // expect(newBalances).to.be.deep.equal(balances); - // }); - it('should throw if maker balances are too low to fill order', async () => { signedOrder = orderFactory.newSignedOrder({ makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), @@ -557,17 +546,6 @@ describe('Exchange', () => { return expect(exWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith(constants.REVERT); }); - // it('should not change balances if taker balances are too low to fill order and \ - // shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - // signedOrder = await orderFactory.newSignedOrderAsync({ - // takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), - // }); - - // await exWrapper.fillOrderAsync(signedOrder, taker); - // const newBalances = await dmyBalances.getAsync(); - // expect(newBalances).to.be.deep.equal(balances); - // }); - it('should throw if taker balances are too low to fill order', async () => { signedOrder = orderFactory.newSignedOrder({ takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), @@ -576,18 +554,6 @@ describe('Exchange', () => { return expect(exWrapper.fillOrderAsync(signedOrder, takerAddress)).to.be.rejectedWith(constants.REVERT); }); - // it('should not change balances if maker allowances are too low to fill order and \ - // shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - // await rep.approve.sendTransactionAsync(tokenTransferProxy.address, new BigNumber(0), { from: maker }); - // await exWrapper.fillOrderAsync(signedOrder, taker); - // await rep.approve.sendTransactionAsync(tokenTransferProxy.address, INITIAL_ALLOWANCE, { - // from: maker, - // }); - - // const newBalances = await dmyBalances.getAsync(); - // expect(newBalances).to.be.deep.equal(balances); - // }); - it('should throw if maker allowances are too low to fill order', async () => { await rep.approve.sendTransactionAsync(tokenTransferProxy.address, new BigNumber(0), { from: makerAddress, @@ -598,18 +564,6 @@ describe('Exchange', () => { }); }); - // it('should not change balances if taker allowances are too low to fill order and \ - // shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - // await dgd.approve.sendTransactionAsync(tokenTransferProxy.address, new BigNumber(0), { from: taker }); - // await exWrapper.fillOrderAsync(signedOrder, taker); - // await dgd.approve.sendTransactionAsync(tokenTransferProxy.address, INITIAL_ALLOWANCE, { - // from: taker, - // }); - - // const newBalances = await dmyBalances.getAsync(); - // expect(newBalances).to.be.deep.equal(balances); - // }); - it('should throw if taker allowances are too low to fill order', async () => { await dgd.approve.sendTransactionAsync(tokenTransferProxy.address, new BigNumber(0), { from: takerAddress, @@ -620,72 +574,6 @@ describe('Exchange', () => { }); }); - // it('should not change balances if makerTokenAddress is ZRX, makerTokenAmount + makerFee > maker balance, \ - // and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - // const makerZRXBalance = new BigNumber(balances[maker][zrx.address]); - // signedOrder = await orderFactory.newSignedOrderAsync({ - // makerTokenAddress: zrx.address, - // makerTokenAmount: makerZRXBalance, - // makerFee: new BigNumber(1), - // }); - // await exWrapper.fillOrderAsync(signedOrder, taker); - // const newBalances = await dmyBalances.getAsync(); - // expect(newBalances).to.be.deep.equal(balances); - // }); - - // it('should not change balances if makerTokenAddress is ZRX, makerTokenAmount + makerFee > maker allowance, \ - // and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - // const makerZRXAllowance = await zrx.allowance(maker, tokenTransferProxy.address); - // signedOrder = await orderFactory.newSignedOrderAsync({ - // makerTokenAddress: zrx.address, - // makerTokenAmount: new BigNumber(makerZRXAllowance), - // makerFee: new BigNumber(1), - // }); - // await exWrapper.fillOrderAsync(signedOrder, taker); - // const newBalances = await dmyBalances.getAsync(); - // expect(newBalances).to.be.deep.equal(balances); - // }); - - // it('should not change balances if takerTokenAddress is ZRX, takerTokenAmount + takerFee > taker balance, \ - // and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - // const takerZRXBalance = new BigNumber(balances[taker][zrx.address]); - // signedOrder = await orderFactory.newSignedOrderAsync({ - // takerTokenAddress: zrx.address, - // takerTokenAmount: takerZRXBalance, - // takerFee: new BigNumber(1), - // }); - // await exWrapper.fillOrderAsync(signedOrder, taker); - // const newBalances = await dmyBalances.getAsync(); - // expect(newBalances).to.be.deep.equal(balances); - // }); - - // it('should not change balances if takerTokenAddress is ZRX, takerTokenAmount + takerFee > taker allowance, \ - // and shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - // const takerZRXAllowance = await zrx.allowance(taker, tokenTransferProxy.address); - // signedOrder = await orderFactory.newSignedOrderAsync({ - // takerTokenAddress: zrx.address, - // takerTokenAmount: new BigNumber(takerZRXAllowance), - // takerFee: new BigNumber(1), - // }); - // await exWrapper.fillOrderAsync(signedOrder, taker); - // const newBalances = await dmyBalances.getAsync(); - // expect(newBalances).to.be.deep.equal(balances); - // }); - - // it('should throw if getBalance or getAllowance attempts to change state and \ - // shouldThrowOnInsufficientBalanceOrAllowance = false', async () => { - // const maliciousToken = await deployer.deployAsync(ContractName.MaliciousToken); - // await maliciousToken.approve.sendTransactionAsync(tokenTransferProxy.address, INITIAL_ALLOWANCE, { - // from: taker, - // }); - - // signedOrder = await orderFactory.newSignedOrderAsync({ - // takerTokenAddress: maliciousToken.address, - // }); - - // return expect(exWrapper.fillOrderAsync(signedOrder, taker)).to.be.rejectedWith(constants.REVERT); - // }); - it('should not change balances if an order is expired', async () => { signedOrder = orderFactory.newSignedOrder({ expirationTimeSeconds: new BigNumber(Math.floor((Date.now() - 10000) / 1000)), diff --git a/packages/contracts/test/exchange/wrapper.ts b/packages/contracts/test/exchange/wrapper.ts index e5933a7e7..0ea1f2c82 100644 --- a/packages/contracts/test/exchange/wrapper.ts +++ b/packages/contracts/test/exchange/wrapper.ts @@ -34,8 +34,8 @@ describe('Exchange', () => { let takerAddress: string; let feeRecipientAddress: string; - const INIT_BAL = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); - const INIT_ALLOW = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); + const INITIAL_BALANCE = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); + const INITIAL_ALLOWANCE = ZeroEx.toBaseUnitAmount(new BigNumber(10000), 18); let rep: DummyTokenContract; let dgd: DummyTokenContract; @@ -95,18 +95,18 @@ describe('Exchange', () => { orderFactory = new OrderFactory(secretKey, defaultOrderParams); dmyBalances = new Balances([rep, dgd, zrx], [makerAddress, takerAddress, feeRecipientAddress]); await Promise.all([ - rep.approve.sendTransactionAsync(tokenTransferProxy.address, INIT_ALLOW, { from: makerAddress }), - rep.approve.sendTransactionAsync(tokenTransferProxy.address, INIT_ALLOW, { from: takerAddress }), - rep.setBalance.sendTransactionAsync(makerAddress, INIT_BAL, { from: tokenOwner }), - rep.setBalance.sendTransactionAsync(takerAddress, INIT_BAL, { from: tokenOwner }), - dgd.approve.sendTransactionAsync(tokenTransferProxy.address, INIT_ALLOW, { from: makerAddress }), - dgd.approve.sendTransactionAsync(tokenTransferProxy.address, INIT_ALLOW, { from: takerAddress }), - dgd.setBalance.sendTransactionAsync(makerAddress, INIT_BAL, { from: tokenOwner }), - dgd.setBalance.sendTransactionAsync(takerAddress, INIT_BAL, { from: tokenOwner }), - zrx.approve.sendTransactionAsync(tokenTransferProxy.address, INIT_ALLOW, { from: makerAddress }), - zrx.approve.sendTransactionAsync(tokenTransferProxy.address, INIT_ALLOW, { from: takerAddress }), - zrx.setBalance.sendTransactionAsync(makerAddress, INIT_BAL, { from: tokenOwner }), - zrx.setBalance.sendTransactionAsync(takerAddress, INIT_BAL, { from: tokenOwner }), + rep.approve.sendTransactionAsync(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: makerAddress }), + rep.approve.sendTransactionAsync(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: takerAddress }), + rep.setBalance.sendTransactionAsync(makerAddress, INITIAL_BALANCE, { from: tokenOwner }), + rep.setBalance.sendTransactionAsync(takerAddress, INITIAL_BALANCE, { from: tokenOwner }), + dgd.approve.sendTransactionAsync(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: makerAddress }), + dgd.approve.sendTransactionAsync(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: takerAddress }), + dgd.setBalance.sendTransactionAsync(makerAddress, INITIAL_BALANCE, { from: tokenOwner }), + dgd.setBalance.sendTransactionAsync(takerAddress, INITIAL_BALANCE, { from: tokenOwner }), + zrx.approve.sendTransactionAsync(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: makerAddress }), + zrx.approve.sendTransactionAsync(tokenTransferProxy.address, INITIAL_ALLOWANCE, { from: takerAddress }), + zrx.setBalance.sendTransactionAsync(makerAddress, INITIAL_BALANCE, { from: tokenOwner }), + zrx.setBalance.sendTransactionAsync(takerAddress, INITIAL_BALANCE, { from: tokenOwner }), ]); }); beforeEach(async () => { @@ -228,6 +228,102 @@ describe('Exchange', () => { balances[feeRecipientAddress][zrx.address].add(makerFeeAmount.add(takerFeeAmount)), ); }); + + it('should not change balances if maker balances are too low to fill order', async () => { + const signedOrder = orderFactory.newSignedOrder({ + makerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), + }); + + await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should not change balances if taker balances are too low to fill order', async () => { + const signedOrder = orderFactory.newSignedOrder({ + takerTokenAmount: ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18), + }); + + await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should not change balances if maker allowances are too low to fill order', async () => { + const signedOrder = orderFactory.newSignedOrder(); + await rep.approve.sendTransactionAsync(tokenTransferProxy.address, new BigNumber(0), { + from: makerAddress, + }); + await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + await rep.approve.sendTransactionAsync(tokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: makerAddress, + }); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should not change balances if taker allowances are too low to fill order', async () => { + const signedOrder = orderFactory.newSignedOrder(); + await dgd.approve.sendTransactionAsync(tokenTransferProxy.address, new BigNumber(0), { + from: takerAddress, + }); + await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + await dgd.approve.sendTransactionAsync(tokenTransferProxy.address, INITIAL_ALLOWANCE, { + from: takerAddress, + }); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should not change balances if makerTokenAddress is ZRX, makerTokenAmount + makerFee > maker balance', async () => { + const makerZRXBalance = new BigNumber(balances[makerAddress][zrx.address]); + const signedOrder = orderFactory.newSignedOrder({ + makerTokenAddress: zrx.address, + makerTokenAmount: makerZRXBalance, + makerFeeAmount: new BigNumber(1), + }); + await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should not change balances if makerTokenAddress is ZRX, makerTokenAmount + makerFee > maker allowance', async () => { + const makerZRXAllowance = await zrx.allowance.callAsync(makerAddress, tokenTransferProxy.address); + const signedOrder = orderFactory.newSignedOrder({ + makerTokenAddress: zrx.address, + makerTokenAmount: new BigNumber(makerZRXAllowance), + makerFeeAmount: new BigNumber(1), + }); + await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should not change balances if takerTokenAddress is ZRX, takerTokenAmount + takerFee > taker balance', async () => { + const takerZRXBalance = new BigNumber(balances[takerAddress][zrx.address]); + const signedOrder = orderFactory.newSignedOrder({ + takerTokenAddress: zrx.address, + takerTokenAmount: takerZRXBalance, + takerFeeAmount: new BigNumber(1), + }); + await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should not change balances if takerTokenAddress is ZRX, takerTokenAmount + takerFee > taker allowance', async () => { + const takerZRXAllowance = await zrx.allowance.callAsync(takerAddress, tokenTransferProxy.address); + const signedOrder = orderFactory.newSignedOrder({ + takerTokenAddress: zrx.address, + takerTokenAmount: new BigNumber(takerZRXAllowance), + takerFeeAmount: new BigNumber(1), + }); + await exWrapper.fillOrderNoThrowAsync(signedOrder, takerAddress); + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); }); describe('batch functions', () => { @@ -245,7 +341,7 @@ describe('Exchange', () => { const takerTokenFillAmounts: BigNumber[] = []; const makerTokenAddress = rep.address; const takerTokenAddress = dgd.address; - signedOrders.forEach(signedOrder => { + _.forEach(signedOrders, signedOrder => { const takerTokenFillAmount = signedOrder.takerTokenAmount.div(2); const makerTokenFillAmount = takerTokenFillAmount .times(signedOrder.makerTokenAmount) @@ -290,7 +386,7 @@ describe('Exchange', () => { const takerTokenFillAmounts: BigNumber[] = []; const makerTokenAddress = rep.address; const takerTokenAddress = dgd.address; - signedOrders.forEach(signedOrder => { + _.forEach(signedOrders, signedOrder => { const takerTokenFillAmount = signedOrder.takerTokenAmount.div(2); const makerTokenFillAmount = takerTokenFillAmount .times(signedOrder.makerTokenAmount) @@ -331,7 +427,7 @@ describe('Exchange', () => { it('should throw if a single signedOrder does not fill the expected amount', async () => { const takerTokenFillAmounts: BigNumber[] = []; - signedOrders.forEach(signedOrder => { + _.forEach(signedOrders, signedOrder => { const takerTokenFillAmount = signedOrder.takerTokenAmount.div(2); takerTokenFillAmounts.push(takerTokenFillAmount); }); @@ -346,6 +442,103 @@ describe('Exchange', () => { }); }); + describe('batchFillOrdersNoThrow', async () => { + it('should transfer the correct amounts', async () => { + const takerTokenFillAmounts: BigNumber[] = []; + const makerTokenAddress = rep.address; + const takerTokenAddress = dgd.address; + _.forEach(signedOrders, signedOrder => { + const takerTokenFillAmount = signedOrder.takerTokenAmount.div(2); + const makerTokenFillAmount = takerTokenFillAmount + .times(signedOrder.makerTokenAmount) + .dividedToIntegerBy(signedOrder.takerTokenAmount); + const makerFeeAmount = signedOrder.makerFeeAmount + .times(makerTokenFillAmount) + .dividedToIntegerBy(signedOrder.makerTokenAmount); + const takerFeeAmount = signedOrder.takerFeeAmount + .times(makerTokenFillAmount) + .dividedToIntegerBy(signedOrder.makerTokenAmount); + takerTokenFillAmounts.push(takerTokenFillAmount); + balances[makerAddress][makerTokenAddress] = balances[makerAddress][makerTokenAddress].minus( + makerTokenFillAmount, + ); + balances[makerAddress][takerTokenAddress] = balances[makerAddress][takerTokenAddress].add( + takerTokenFillAmount, + ); + balances[makerAddress][zrx.address] = balances[makerAddress][zrx.address].minus(makerFeeAmount); + balances[takerAddress][makerTokenAddress] = balances[takerAddress][makerTokenAddress].add( + makerTokenFillAmount, + ); + balances[takerAddress][takerTokenAddress] = balances[takerAddress][takerTokenAddress].minus( + takerTokenFillAmount, + ); + balances[takerAddress][zrx.address] = balances[takerAddress][zrx.address].minus(takerFeeAmount); + balances[feeRecipientAddress][zrx.address] = balances[feeRecipientAddress][zrx.address].add( + makerFeeAmount.add(takerFeeAmount), + ); + }); + + await exWrapper.batchFillOrdersNoThrowAsync(signedOrders, takerAddress, { + takerTokenFillAmounts, + }); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should not throw if an order is invalid and fill the remaining orders', async () => { + const takerTokenFillAmounts: BigNumber[] = []; + const makerTokenAddress = rep.address; + const takerTokenAddress = dgd.address; + + const invalidOrder = { + ...signedOrders[0], + signature: '0x00', + }; + const validOrders = signedOrders.slice(1); + + takerTokenFillAmounts.push(invalidOrder.takerTokenAmount.div(2)); + _.forEach(validOrders, signedOrder => { + const takerTokenFillAmount = signedOrder.takerTokenAmount.div(2); + const makerTokenFillAmount = takerTokenFillAmount + .times(signedOrder.makerTokenAmount) + .dividedToIntegerBy(signedOrder.takerTokenAmount); + const makerFeeAmount = signedOrder.makerFeeAmount + .times(makerTokenFillAmount) + .dividedToIntegerBy(signedOrder.makerTokenAmount); + const takerFeeAmount = signedOrder.takerFeeAmount + .times(makerTokenFillAmount) + .dividedToIntegerBy(signedOrder.makerTokenAmount); + takerTokenFillAmounts.push(takerTokenFillAmount); + balances[makerAddress][makerTokenAddress] = balances[makerAddress][makerTokenAddress].minus( + makerTokenFillAmount, + ); + balances[makerAddress][takerTokenAddress] = balances[makerAddress][takerTokenAddress].add( + takerTokenFillAmount, + ); + balances[makerAddress][zrx.address] = balances[makerAddress][zrx.address].minus(makerFeeAmount); + balances[takerAddress][makerTokenAddress] = balances[takerAddress][makerTokenAddress].add( + makerTokenFillAmount, + ); + balances[takerAddress][takerTokenAddress] = balances[takerAddress][takerTokenAddress].minus( + takerTokenFillAmount, + ); + balances[takerAddress][zrx.address] = balances[takerAddress][zrx.address].minus(takerFeeAmount); + balances[feeRecipientAddress][zrx.address] = balances[feeRecipientAddress][zrx.address].add( + makerFeeAmount.add(takerFeeAmount), + ); + }); + + const newOrders = [invalidOrder, ...validOrders]; + await exWrapper.batchFillOrdersNoThrowAsync(newOrders, takerAddress, { + takerTokenFillAmounts, + }); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + }); + describe('marketFillOrders', () => { it('should stop when the entire takerTokenFillAmount is filled', async () => { const takerTokenFillAmount = signedOrders[0].takerTokenAmount.plus( @@ -391,7 +584,7 @@ describe('Exchange', () => { it('should fill all signedOrders if cannot fill entire takerTokenFillAmount', async () => { const takerTokenFillAmount = ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18); - signedOrders.forEach(signedOrder => { + _.forEach(signedOrders, signedOrder => { balances[makerAddress][signedOrder.makerTokenAddress] = balances[makerAddress][ signedOrder.makerTokenAddress ].minus(signedOrder.makerTokenAmount); @@ -437,6 +630,97 @@ describe('Exchange', () => { }); }); + describe('marketFillOrdersNoThrow', () => { + it('should stop when the entire takerTokenFillAmount is filled', async () => { + const takerTokenFillAmount = signedOrders[0].takerTokenAmount.plus( + signedOrders[1].takerTokenAmount.div(2), + ); + await exWrapper.marketFillOrdersNoThrowAsync(signedOrders, takerAddress, { + takerTokenFillAmount, + }); + + const newBalances = await dmyBalances.getAsync(); + + const makerTokenFillAmount = signedOrders[0].makerTokenAmount.add( + signedOrders[1].makerTokenAmount.dividedToIntegerBy(2), + ); + const makerFee = signedOrders[0].makerFeeAmount.add( + signedOrders[1].makerFeeAmount.dividedToIntegerBy(2), + ); + const takerFee = signedOrders[0].takerFeeAmount.add( + signedOrders[1].takerFeeAmount.dividedToIntegerBy(2), + ); + expect(newBalances[makerAddress][signedOrders[0].makerTokenAddress]).to.be.bignumber.equal( + balances[makerAddress][signedOrders[0].makerTokenAddress].minus(makerTokenFillAmount), + ); + expect(newBalances[makerAddress][signedOrders[0].takerTokenAddress]).to.be.bignumber.equal( + balances[makerAddress][signedOrders[0].takerTokenAddress].add(takerTokenFillAmount), + ); + expect(newBalances[makerAddress][zrx.address]).to.be.bignumber.equal( + balances[makerAddress][zrx.address].minus(makerFee), + ); + expect(newBalances[takerAddress][signedOrders[0].takerTokenAddress]).to.be.bignumber.equal( + balances[takerAddress][signedOrders[0].takerTokenAddress].minus(takerTokenFillAmount), + ); + expect(newBalances[takerAddress][signedOrders[0].makerTokenAddress]).to.be.bignumber.equal( + balances[takerAddress][signedOrders[0].makerTokenAddress].add(makerTokenFillAmount), + ); + expect(newBalances[takerAddress][zrx.address]).to.be.bignumber.equal( + balances[takerAddress][zrx.address].minus(takerFee), + ); + expect(newBalances[feeRecipientAddress][zrx.address]).to.be.bignumber.equal( + balances[feeRecipientAddress][zrx.address].add(makerFee.add(takerFee)), + ); + }); + + it('should fill all signedOrders if cannot fill entire takerTokenFillAmount', async () => { + const takerTokenFillAmount = ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18); + _.forEach(signedOrders, signedOrder => { + balances[makerAddress][signedOrder.makerTokenAddress] = balances[makerAddress][ + signedOrder.makerTokenAddress + ].minus(signedOrder.makerTokenAmount); + balances[makerAddress][signedOrder.takerTokenAddress] = balances[makerAddress][ + signedOrder.takerTokenAddress + ].add(signedOrder.takerTokenAmount); + balances[makerAddress][zrx.address] = balances[makerAddress][zrx.address].minus( + signedOrder.makerFeeAmount, + ); + balances[takerAddress][signedOrder.makerTokenAddress] = balances[takerAddress][ + signedOrder.makerTokenAddress + ].add(signedOrder.makerTokenAmount); + balances[takerAddress][signedOrder.takerTokenAddress] = balances[takerAddress][ + signedOrder.takerTokenAddress + ].minus(signedOrder.takerTokenAmount); + balances[takerAddress][zrx.address] = balances[takerAddress][zrx.address].minus( + signedOrder.takerFeeAmount, + ); + balances[feeRecipientAddress][zrx.address] = balances[feeRecipientAddress][zrx.address].add( + signedOrder.makerFeeAmount.add(signedOrder.takerFeeAmount), + ); + }); + await exWrapper.marketFillOrdersNoThrowAsync(signedOrders, takerAddress, { + takerTokenFillAmount, + }); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should throw when a signedOrder does not use the same takerTokenAddress', async () => { + signedOrders = [ + orderFactory.newSignedOrder(), + orderFactory.newSignedOrder({ takerTokenAddress: zrx.address }), + orderFactory.newSignedOrder(), + ]; + + return expect( + exWrapper.marketFillOrdersNoThrowAsync(signedOrders, takerAddress, { + takerTokenFillAmount: ZeroEx.toBaseUnitAmount(new BigNumber(1000), 18), + }), + ).to.be.rejectedWith(constants.REVERT); + }); + }); + describe('batchCancelOrders', () => { it('should be able to cancel multiple signedOrders', async () => { const takerTokenCancelAmounts = _.map(signedOrders, signedOrder => signedOrder.takerTokenAmount); |