diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2018-06-20 06:23:28 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-20 06:23:28 +0800 |
commit | e216eb1e7b49f1ad4bc2c8a07007b4ef94373c5d (patch) | |
tree | 70215e5dbf309870b9da483344ecd6f5678c5c4e /packages/contracts/test | |
parent | 0438fdde4919df13471f9b566b218ff9379d6d27 (diff) | |
parent | 6b08e6b80996d9553f280c6d7ade293ae7333711 (diff) | |
download | dexon-sol-tools-e216eb1e7b49f1ad4bc2c8a07007b4ef94373c5d.tar dexon-sol-tools-e216eb1e7b49f1ad4bc2c8a07007b4ef94373c5d.tar.gz dexon-sol-tools-e216eb1e7b49f1ad4bc2c8a07007b4ef94373c5d.tar.bz2 dexon-sol-tools-e216eb1e7b49f1ad4bc2c8a07007b4ef94373c5d.tar.lz dexon-sol-tools-e216eb1e7b49f1ad4bc2c8a07007b4ef94373c5d.tar.xz dexon-sol-tools-e216eb1e7b49f1ad4bc2c8a07007b4ef94373c5d.tar.zst dexon-sol-tools-e216eb1e7b49f1ad4bc2c8a07007b4ef94373c5d.zip |
Merge pull request #714 from 0xProject/feature/contracts/cancelOrdersUpToSender
Make cancelOrdersUpTo compatible with sender abstraction
Diffstat (limited to 'packages/contracts/test')
-rw-r--r-- | packages/contracts/test/exchange/core.ts | 30 | ||||
-rw-r--r-- | packages/contracts/test/exchange/transactions.ts | 112 |
2 files changed, 127 insertions, 15 deletions
diff --git a/packages/contracts/test/exchange/core.ts b/packages/contracts/test/exchange/core.ts index 63c2fa6c0..ea37a1e99 100644 --- a/packages/contracts/test/exchange/core.ts +++ b/packages/contracts/test/exchange/core.ts @@ -620,30 +620,30 @@ describe('Exchange core', () => { }); describe('cancelOrdersUpTo', () => { - it('should fail to set makerEpoch less than current makerEpoch', async () => { - const makerEpoch = new BigNumber(1); - await exchangeWrapper.cancelOrdersUpToAsync(makerEpoch, makerAddress); - const lesserMakerEpoch = new BigNumber(0); + it('should fail to set orderEpoch less than current orderEpoch', async () => { + const orderEpoch = new BigNumber(1); + await exchangeWrapper.cancelOrdersUpToAsync(orderEpoch, makerAddress); + const lesserOrderEpoch = new BigNumber(0); return expectRevertOrAlwaysFailingTransactionAsync( - exchangeWrapper.cancelOrdersUpToAsync(lesserMakerEpoch, makerAddress), + exchangeWrapper.cancelOrdersUpToAsync(lesserOrderEpoch, makerAddress), ); }); - it('should fail to set makerEpoch equal to existing makerEpoch', async () => { - const makerEpoch = new BigNumber(1); - await exchangeWrapper.cancelOrdersUpToAsync(makerEpoch, makerAddress); + it('should fail to set orderEpoch equal to existing orderEpoch', async () => { + const orderEpoch = new BigNumber(1); + await exchangeWrapper.cancelOrdersUpToAsync(orderEpoch, makerAddress); return expectRevertOrAlwaysFailingTransactionAsync( - exchangeWrapper.cancelOrdersUpToAsync(makerEpoch, makerAddress), + exchangeWrapper.cancelOrdersUpToAsync(orderEpoch, makerAddress), ); }); - it('should cancel only orders with a makerEpoch less than existing makerEpoch', async () => { - // Cancel all transactions with a makerEpoch less than 1 - const makerEpoch = new BigNumber(1); - await exchangeWrapper.cancelOrdersUpToAsync(makerEpoch, makerAddress); + it('should cancel only orders with a orderEpoch less than existing orderEpoch', async () => { + // Cancel all transactions with a orderEpoch less than 1 + const orderEpoch = new BigNumber(1); + await exchangeWrapper.cancelOrdersUpToAsync(orderEpoch, makerAddress); - // Create 3 orders with makerEpoch values: 0,1,2,3 - // Since we cancelled with makerEpoch=1, orders with makerEpoch<=1 will not be processed + // Create 3 orders with orderEpoch values: 0,1,2,3 + // Since we cancelled with orderEpoch=1, orders with orderEpoch<=1 will not be processed erc20Balances = await erc20Wrapper.getBalancesAsync(); const signedOrders = [ orderFactory.newSignedOrder({ diff --git a/packages/contracts/test/exchange/transactions.ts b/packages/contracts/test/exchange/transactions.ts index 21ff123e5..9a625880c 100644 --- a/packages/contracts/test/exchange/transactions.ts +++ b/packages/contracts/test/exchange/transactions.ts @@ -7,6 +7,7 @@ import * as chai from 'chai'; import { DummyERC20TokenContract } from '../../src/generated_contract_wrappers/dummy_e_r_c20_token'; import { ERC20ProxyContract } from '../../src/generated_contract_wrappers/e_r_c20_proxy'; import { ExchangeContract } from '../../src/generated_contract_wrappers/exchange'; +import { ExchangeWrapperContract } from '../../src/generated_contract_wrappers/exchange_wrapper'; import { WhitelistContract } from '../../src/generated_contract_wrappers/whitelist'; import { artifacts } from '../../src/utils/artifacts'; import { expectRevertOrAlwaysFailingTransactionAsync } from '../../src/utils/assertions'; @@ -198,6 +199,117 @@ describe('Exchange transactions', () => { ); }); }); + + describe('cancelOrdersUpTo', () => { + let exchangeWrapperContract: ExchangeWrapperContract; + + before(async () => { + exchangeWrapperContract = await ExchangeWrapperContract.deployFrom0xArtifactAsync( + artifacts.ExchangeWrapper, + provider, + txDefaults, + exchange.address, + ); + }); + + it("should cancel an order if called from the order's sender", async () => { + const orderSalt = new BigNumber(0); + signedOrder = orderFactory.newSignedOrder({ + senderAddress: exchangeWrapperContract.address, + salt: orderSalt, + }); + const targetOrderEpoch = orderSalt.add(1); + const cancelData = exchange.cancelOrdersUpTo.getABIEncodedTransactionData(targetOrderEpoch); + const signedCancelTx = makerTransactionFactory.newSignedTransaction(cancelData); + await exchangeWrapperContract.cancelOrdersUpTo.sendTransactionAsync( + targetOrderEpoch, + signedCancelTx.salt, + signedCancelTx.signature, + { + from: makerAddress, + }, + ); + + const takerAssetFillAmount = signedOrder.takerAssetAmount; + orderWithoutExchangeAddress = orderUtils.getOrderWithoutExchangeAddress(signedOrder); + const fillData = exchange.fillOrder.getABIEncodedTransactionData( + orderWithoutExchangeAddress, + takerAssetFillAmount, + signedOrder.signature, + ); + const signedFillTx = takerTransactionFactory.newSignedTransaction(fillData); + return expectRevertOrAlwaysFailingTransactionAsync( + exchangeWrapperContract.fillOrder.sendTransactionAsync( + orderWithoutExchangeAddress, + takerAssetFillAmount, + signedFillTx.salt, + signedOrder.signature, + signedFillTx.signature, + { from: takerAddress }, + ), + ); + }); + + it("should not cancel an order if not called from the order's sender", async () => { + const orderSalt = new BigNumber(0); + signedOrder = orderFactory.newSignedOrder({ + senderAddress: exchangeWrapperContract.address, + salt: orderSalt, + }); + const targetOrderEpoch = orderSalt.add(1); + await exchangeWrapper.cancelOrdersUpToAsync(targetOrderEpoch, makerAddress); + + erc20Balances = await erc20Wrapper.getBalancesAsync(); + const takerAssetFillAmount = signedOrder.takerAssetAmount; + orderWithoutExchangeAddress = orderUtils.getOrderWithoutExchangeAddress(signedOrder); + const data = exchange.fillOrder.getABIEncodedTransactionData( + orderWithoutExchangeAddress, + takerAssetFillAmount, + signedOrder.signature, + ); + signedTx = takerTransactionFactory.newSignedTransaction(data); + await exchangeWrapperContract.fillOrder.sendTransactionAsync( + orderWithoutExchangeAddress, + takerAssetFillAmount, + signedTx.salt, + signedOrder.signature, + signedTx.signature, + { from: takerAddress }, + ); + + const newBalances = await erc20Wrapper.getBalancesAsync(); + const makerAssetFillAmount = takerAssetFillAmount + .times(signedOrder.makerAssetAmount) + .dividedToIntegerBy(signedOrder.takerAssetAmount); + const makerFeePaid = signedOrder.makerFee + .times(makerAssetFillAmount) + .dividedToIntegerBy(signedOrder.makerAssetAmount); + const takerFeePaid = signedOrder.takerFee + .times(makerAssetFillAmount) + .dividedToIntegerBy(signedOrder.makerAssetAmount); + expect(newBalances[makerAddress][defaultMakerTokenAddress]).to.be.bignumber.equal( + erc20Balances[makerAddress][defaultMakerTokenAddress].minus(makerAssetFillAmount), + ); + expect(newBalances[makerAddress][defaultTakerTokenAddress]).to.be.bignumber.equal( + erc20Balances[makerAddress][defaultTakerTokenAddress].add(takerAssetFillAmount), + ); + expect(newBalances[makerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[makerAddress][zrxToken.address].minus(makerFeePaid), + ); + expect(newBalances[takerAddress][defaultTakerTokenAddress]).to.be.bignumber.equal( + erc20Balances[takerAddress][defaultTakerTokenAddress].minus(takerAssetFillAmount), + ); + expect(newBalances[takerAddress][defaultMakerTokenAddress]).to.be.bignumber.equal( + erc20Balances[takerAddress][defaultMakerTokenAddress].add(makerAssetFillAmount), + ); + expect(newBalances[takerAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[takerAddress][zrxToken.address].minus(takerFeePaid), + ); + expect(newBalances[feeRecipientAddress][zrxToken.address]).to.be.bignumber.equal( + erc20Balances[feeRecipientAddress][zrxToken.address].add(makerFeePaid.add(takerFeePaid)), + ); + }); + }); }); describe('Whitelist', () => { |