diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2018-03-30 03:38:13 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2018-04-21 04:56:17 +0800 |
commit | 0c74a3bc6610a9c71bf2c5111138ae3e162cffd6 (patch) | |
tree | 5ee33615d71824285f0da796c6e40926a3d01656 | |
parent | 75f77f3e8675c468d88456aec02561732c7646fb (diff) | |
download | dexon-0x-contracts-0c74a3bc6610a9c71bf2c5111138ae3e162cffd6.tar.gz dexon-0x-contracts-0c74a3bc6610a9c71bf2c5111138ae3e162cffd6.tar.zst dexon-0x-contracts-0c74a3bc6610a9c71bf2c5111138ae3e162cffd6.zip |
Add tests for marketBuyOrders
-rw-r--r-- | packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol | 4 | ||||
-rw-r--r-- | packages/contracts/test/exchange/wrapper.ts | 174 |
2 files changed, 174 insertions, 4 deletions
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol index cfd52f71d..030743348 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol @@ -278,7 +278,7 @@ contract MixinWrapperFunctions is returns (uint256 takerAmountBought) { for (uint256 i = 0; i < orders.length; i++) { - require(orders[i].takerTokenAddress == orders[0].takerTokenAddress); + require(orders[i].makerTokenAddress == orders[0].makerTokenAddress); uint256 remainingTakerBuyAmount = safeSub(takerBuyAmount, takerAmountBought); uint256 takerSellAmount = getPartialAmount( orders[i].makerBuyAmount, @@ -319,7 +319,7 @@ contract MixinWrapperFunctions is returns (uint256 takerAmountBought) { for (uint256 i = 0; i < orders.length; i++) { - require(orders[i].takerTokenAddress == orders[0].takerTokenAddress); + require(orders[i].makerTokenAddress == orders[0].makerTokenAddress); uint256 remainingTakerBuyAmount = safeSub(takerBuyAmount, takerAmountBought); uint256 takerSellAmount = getPartialAmount( orders[i].makerBuyAmount, diff --git a/packages/contracts/test/exchange/wrapper.ts b/packages/contracts/test/exchange/wrapper.ts index 00e703b2f..295d5b204 100644 --- a/packages/contracts/test/exchange/wrapper.ts +++ b/packages/contracts/test/exchange/wrapper.ts @@ -539,7 +539,7 @@ describe('Exchange', () => { }); }); - describe('marketFillOrders', () => { + describe('marketSellOrders', () => { it('should stop when the entire takerSellAmount is filled', async () => { const takerSellAmount = signedOrders[0].makerBuyAmount.plus(signedOrders[1].makerBuyAmount.div(2)); await exWrapper.marketSellOrdersAsync(signedOrders, takerAddress, { @@ -624,7 +624,7 @@ describe('Exchange', () => { }); }); - describe('marketFillOrdersNoThrow', () => { + describe('marketSellOrdersNoThrow', () => { it('should stop when the entire takerSellAmount is filled', async () => { const takerSellAmount = signedOrders[0].makerBuyAmount.plus(signedOrders[1].makerBuyAmount.div(2)); await exWrapper.marketSellOrdersNoThrowAsync(signedOrders, takerAddress, { @@ -709,6 +709,176 @@ describe('Exchange', () => { }); }); + describe('marketBuyOrders', () => { + it('should stop when the entire takerBuyAmount is filled', async () => { + const takerBuyAmount = signedOrders[0].makerSellAmount.plus(signedOrders[1].makerSellAmount.div(2)); + await exWrapper.marketBuyOrdersAsync(signedOrders, takerAddress, { + takerBuyAmount, + }); + + const newBalances = await dmyBalances.getAsync(); + + const makerAmountBought = signedOrders[0].makerBuyAmount.add( + signedOrders[1].makerBuyAmount.dividedToIntegerBy(2), + ); + const makerFee = signedOrders[0].makerFee.add(signedOrders[1].makerFee.dividedToIntegerBy(2)); + const takerFee = signedOrders[0].takerFee.add(signedOrders[1].takerFee.dividedToIntegerBy(2)); + expect(newBalances[makerAddress][signedOrders[0].makerTokenAddress]).to.be.bignumber.equal( + balances[makerAddress][signedOrders[0].makerTokenAddress].minus(takerBuyAmount), + ); + expect(newBalances[makerAddress][signedOrders[0].takerTokenAddress]).to.be.bignumber.equal( + balances[makerAddress][signedOrders[0].takerTokenAddress].add(makerAmountBought), + ); + 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(makerAmountBought), + ); + expect(newBalances[takerAddress][signedOrders[0].makerTokenAddress]).to.be.bignumber.equal( + balances[takerAddress][signedOrders[0].makerTokenAddress].add(takerBuyAmount), + ); + 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 takerBuyAmount', async () => { + const takerBuyAmount = ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18); + _.forEach(signedOrders, signedOrder => { + balances[makerAddress][signedOrder.makerTokenAddress] = balances[makerAddress][ + signedOrder.makerTokenAddress + ].minus(signedOrder.makerSellAmount); + balances[makerAddress][signedOrder.takerTokenAddress] = balances[makerAddress][ + signedOrder.takerTokenAddress + ].add(signedOrder.makerBuyAmount); + balances[makerAddress][zrx.address] = balances[makerAddress][zrx.address].minus( + signedOrder.makerFee, + ); + balances[takerAddress][signedOrder.makerTokenAddress] = balances[takerAddress][ + signedOrder.makerTokenAddress + ].add(signedOrder.makerSellAmount); + balances[takerAddress][signedOrder.takerTokenAddress] = balances[takerAddress][ + signedOrder.takerTokenAddress + ].minus(signedOrder.makerBuyAmount); + balances[takerAddress][zrx.address] = balances[takerAddress][zrx.address].minus( + signedOrder.takerFee, + ); + balances[feeRecipientAddress][zrx.address] = balances[feeRecipientAddress][zrx.address].add( + signedOrder.makerFee.add(signedOrder.takerFee), + ); + }); + await exWrapper.marketBuyOrdersAsync(signedOrders, takerAddress, { + takerBuyAmount, + }); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should throw when an signedOrder does not use the same makerTokenAddress', async () => { + signedOrders = [ + orderFactory.newSignedOrder(), + orderFactory.newSignedOrder({ makerTokenAddress: zrx.address }), + orderFactory.newSignedOrder(), + ]; + + return expect( + exWrapper.marketBuyOrdersAsync(signedOrders, takerAddress, { + takerBuyAmount: ZeroEx.toBaseUnitAmount(new BigNumber(1000), 18), + }), + ).to.be.rejectedWith(constants.REVERT); + }); + }); + + describe('marketBuyOrdersNoThrow', () => { + it('should stop when the entire takerBuyAmount is filled', async () => { + const takerBuyAmount = signedOrders[0].makerSellAmount.plus(signedOrders[1].makerSellAmount.div(2)); + await exWrapper.marketBuyOrdersNoThrowAsync(signedOrders, takerAddress, { + takerBuyAmount, + }); + + const newBalances = await dmyBalances.getAsync(); + + const makerAmountBought = signedOrders[0].makerBuyAmount.add( + signedOrders[1].makerBuyAmount.dividedToIntegerBy(2), + ); + const makerFee = signedOrders[0].makerFee.add(signedOrders[1].makerFee.dividedToIntegerBy(2)); + const takerFee = signedOrders[0].takerFee.add(signedOrders[1].takerFee.dividedToIntegerBy(2)); + expect(newBalances[makerAddress][signedOrders[0].makerTokenAddress]).to.be.bignumber.equal( + balances[makerAddress][signedOrders[0].makerTokenAddress].minus(takerBuyAmount), + ); + expect(newBalances[makerAddress][signedOrders[0].takerTokenAddress]).to.be.bignumber.equal( + balances[makerAddress][signedOrders[0].takerTokenAddress].add(makerAmountBought), + ); + 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(makerAmountBought), + ); + expect(newBalances[takerAddress][signedOrders[0].makerTokenAddress]).to.be.bignumber.equal( + balances[takerAddress][signedOrders[0].makerTokenAddress].add(takerBuyAmount), + ); + 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 takerSellAmount', async () => { + const takerSellAmount = ZeroEx.toBaseUnitAmount(new BigNumber(100000), 18); + _.forEach(signedOrders, signedOrder => { + balances[makerAddress][signedOrder.makerTokenAddress] = balances[makerAddress][ + signedOrder.makerTokenAddress + ].minus(signedOrder.makerSellAmount); + balances[makerAddress][signedOrder.takerTokenAddress] = balances[makerAddress][ + signedOrder.takerTokenAddress + ].add(signedOrder.makerBuyAmount); + balances[makerAddress][zrx.address] = balances[makerAddress][zrx.address].minus( + signedOrder.makerFee, + ); + balances[takerAddress][signedOrder.makerTokenAddress] = balances[takerAddress][ + signedOrder.makerTokenAddress + ].add(signedOrder.makerSellAmount); + balances[takerAddress][signedOrder.takerTokenAddress] = balances[takerAddress][ + signedOrder.takerTokenAddress + ].minus(signedOrder.makerBuyAmount); + balances[takerAddress][zrx.address] = balances[takerAddress][zrx.address].minus( + signedOrder.takerFee, + ); + balances[feeRecipientAddress][zrx.address] = balances[feeRecipientAddress][zrx.address].add( + signedOrder.makerFee.add(signedOrder.takerFee), + ); + }); + await exWrapper.marketSellOrdersNoThrowAsync(signedOrders, takerAddress, { + takerSellAmount, + }); + + const newBalances = await dmyBalances.getAsync(); + expect(newBalances).to.be.deep.equal(balances); + }); + + it('should throw when a signedOrder does not use the same makerTokenAddress', async () => { + signedOrders = [ + orderFactory.newSignedOrder(), + orderFactory.newSignedOrder({ makerTokenAddress: zrx.address }), + orderFactory.newSignedOrder(), + ]; + + return expect( + exWrapper.marketBuyOrdersNoThrowAsync(signedOrders, takerAddress, { + takerBuyAmount: 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.makerBuyAmount); |