From e92926e8814257d71f45ded8011cb17b08fc37de Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Sat, 16 Jun 2018 21:45:12 -0700 Subject: Make cancelOrdersUpTo compatible with sender abstraction --- packages/contracts/test/exchange/transactions.ts | 112 +++++++++++++++++++++++ 1 file changed, 112 insertions(+) (limited to 'packages/contracts/test/exchange/transactions.ts') diff --git a/packages/contracts/test/exchange/transactions.ts b/packages/contracts/test/exchange/transactions.ts index 21ff123e5..088b537cc 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 cancelSalt = orderSalt.add(1); + const cancelData = exchange.cancelOrdersUpTo.getABIEncodedTransactionData(cancelSalt); + const signedCancelTx = makerTransactionFactory.newSignedTransaction(cancelData); + await exchangeWrapperContract.cancelOrdersUpTo.sendTransactionAsync( + cancelSalt, + 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 cancelSalt = orderSalt.add(1); + await exchangeWrapper.cancelOrdersUpToAsync(cancelSalt, 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', () => { -- cgit v1.2.3 From 6b08e6b80996d9553f280c6d7ade293ae7333711 Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Mon, 18 Jun 2018 08:07:04 -0700 Subject: Rename makerEpoch => orderEpoch --- packages/contracts/test/exchange/transactions.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'packages/contracts/test/exchange/transactions.ts') diff --git a/packages/contracts/test/exchange/transactions.ts b/packages/contracts/test/exchange/transactions.ts index 088b537cc..9a625880c 100644 --- a/packages/contracts/test/exchange/transactions.ts +++ b/packages/contracts/test/exchange/transactions.ts @@ -218,11 +218,11 @@ describe('Exchange transactions', () => { senderAddress: exchangeWrapperContract.address, salt: orderSalt, }); - const cancelSalt = orderSalt.add(1); - const cancelData = exchange.cancelOrdersUpTo.getABIEncodedTransactionData(cancelSalt); + const targetOrderEpoch = orderSalt.add(1); + const cancelData = exchange.cancelOrdersUpTo.getABIEncodedTransactionData(targetOrderEpoch); const signedCancelTx = makerTransactionFactory.newSignedTransaction(cancelData); await exchangeWrapperContract.cancelOrdersUpTo.sendTransactionAsync( - cancelSalt, + targetOrderEpoch, signedCancelTx.salt, signedCancelTx.signature, { @@ -256,8 +256,8 @@ describe('Exchange transactions', () => { senderAddress: exchangeWrapperContract.address, salt: orderSalt, }); - const cancelSalt = orderSalt.add(1); - await exchangeWrapper.cancelOrdersUpToAsync(cancelSalt, makerAddress); + const targetOrderEpoch = orderSalt.add(1); + await exchangeWrapper.cancelOrdersUpToAsync(targetOrderEpoch, makerAddress); erc20Balances = await erc20Wrapper.getBalancesAsync(); const takerAssetFillAmount = signedOrder.takerAssetAmount; -- cgit v1.2.3