diff options
-rw-r--r-- | src/contract_wrappers/exchange_wrapper.ts | 34 | ||||
-rw-r--r-- | src/utils/order_validation_utils.ts | 24 | ||||
-rw-r--r-- | test/order_validation_test.ts | 69 |
3 files changed, 65 insertions, 62 deletions
diff --git a/src/contract_wrappers/exchange_wrapper.ts b/src/contract_wrappers/exchange_wrapper.ts index 4589107a6..76c35840a 100644 --- a/src/contract_wrappers/exchange_wrapper.ts +++ b/src/contract_wrappers/exchange_wrapper.ts @@ -161,7 +161,7 @@ export class ExchangeWrapper extends ContractWrapper { await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper); const exchangeInstance = await this._getExchangeContractAsync(); - await this.validateFillOrderAndThrowIfInvalidAsync(signedOrder, fillTakerTokenAmount, takerAddress); + await this.validateFillOrderThrowIfInvalidAsync(signedOrder, fillTakerTokenAmount, takerAddress); const [orderAddresses, orderValues] = ExchangeWrapper._getOrderAddressesAndValues(signedOrder); @@ -226,7 +226,7 @@ export class ExchangeWrapper extends ContractWrapper { assert.isBoolean('shouldThrowOnInsufficientBalanceOrAllowance', shouldThrowOnInsufficientBalanceOrAllowance); await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper); for (const signedOrder of signedOrders) { - await this.validateFillOrderAndThrowIfInvalidAsync( + await this.validateFillOrderThrowIfInvalidAsync( signedOrder, fillTakerTokenAmount, takerAddress); } if (_.isEmpty(signedOrders)) { @@ -311,7 +311,7 @@ export class ExchangeWrapper extends ContractWrapper { assert.isBoolean('shouldThrowOnInsufficientBalanceOrAllowance', shouldThrowOnInsufficientBalanceOrAllowance); await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper); for (const orderFillRequest of orderFillRequests) { - await this.validateFillOrderAndThrowIfInvalidAsync( + await this.validateFillOrderThrowIfInvalidAsync( orderFillRequest.signedOrder, orderFillRequest.takerTokenFillAmount, takerAddress); } if (_.isEmpty(orderFillRequests)) { @@ -378,7 +378,7 @@ export class ExchangeWrapper extends ContractWrapper { const exchangeInstance = await this._getExchangeContractAsync(); - await this.validateFillOrKillOrderAndThrowIfInvalidAsync(signedOrder, fillTakerTokenAmount, takerAddress); + await this.validateFillOrKillOrderThrowIfInvalidAsync(signedOrder, fillTakerTokenAmount, takerAddress); const [orderAddresses, orderValues] = ExchangeWrapper._getOrderAddressesAndValues(signedOrder); @@ -430,7 +430,7 @@ export class ExchangeWrapper extends ContractWrapper { } const exchangeInstance = await this._getExchangeContractAsync(); for (const request of orderFillOrKillRequests) { - await this.validateFillOrKillOrderAndThrowIfInvalidAsync( + await this.validateFillOrKillOrderThrowIfInvalidAsync( request.signedOrder, request.fillTakerAmount, takerAddress); } @@ -488,7 +488,7 @@ export class ExchangeWrapper extends ContractWrapper { await assert.isSenderAddressAsync('order.maker', order.maker, this._web3Wrapper); const exchangeInstance = await this._getExchangeContractAsync(); - await this.validateCancelOrderAndThrowIfInvalidAsync(order, cancelTakerTokenAmount); + await this.validateCancelOrderThrowIfInvalidAsync(order, cancelTakerTokenAmount); const [orderAddresses, orderValues] = ExchangeWrapper._getOrderAddressesAndValues(order); const gas = await exchangeInstance.cancelOrder.estimateGas( @@ -534,7 +534,7 @@ export class ExchangeWrapper extends ContractWrapper { const maker = makers[0]; await assert.isSenderAddressAsync('maker', maker, this._web3Wrapper); for (const cancellationRequest of orderCancellationRequests) { - await this.validateCancelOrderAndThrowIfInvalidAsync( + await this.validateCancelOrderThrowIfInvalidAsync( cancellationRequest.order, cancellationRequest.takerTokenCancelAmount, ); } @@ -634,14 +634,14 @@ export class ExchangeWrapper extends ContractWrapper { * @param takerAddress The user Ethereum address who would like to fill this order. * Must be available via the supplied Web3.Provider passed to 0x.js. */ - public async validateFillOrderAndThrowIfInvalidAsync(signedOrder: SignedOrder, - fillTakerTokenAmount: BigNumber.BigNumber, - takerAddress: string): Promise<void> { + public async validateFillOrderThrowIfInvalidAsync(signedOrder: SignedOrder, + fillTakerTokenAmount: BigNumber.BigNumber, + takerAddress: string): Promise<void> { assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema); assert.isBigNumber('fillTakerTokenAmount', fillTakerTokenAmount); await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper); const zrxTokenAddress = await this._getZRXTokenAddressAsync(); - await this._orderValidationUtils.validateFillOrderAndThrowIfInvalidAsync( + await this._orderValidationUtils.validateFillOrderThrowIfInvalidAsync( signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress); } /** @@ -650,13 +650,13 @@ export class ExchangeWrapper extends ContractWrapper { * The order you would like to cancel. * @param cancelTakerTokenAmount The amount (specified in taker tokens) that you would like to cancel. */ - public async validateCancelOrderAndThrowIfInvalidAsync( + public async validateCancelOrderThrowIfInvalidAsync( order: Order, cancelTakerTokenAmount: BigNumber.BigNumber): Promise<void> { assert.doesConformToSchema('order', order, orderSchema); assert.isBigNumber('cancelTakerTokenAmount', cancelTakerTokenAmount); const orderHash = utils.getOrderHashHex(order); const unavailableTakerTokenAmount = await this.getUnavailableTakerAmountAsync(orderHash); - await this._orderValidationUtils.validateCancelOrderAndThrowIfInvalidAsync( + await this._orderValidationUtils.validateCancelOrderThrowIfInvalidAsync( order, cancelTakerTokenAmount, unavailableTakerTokenAmount); } /** @@ -667,14 +667,14 @@ export class ExchangeWrapper extends ContractWrapper { * @param takerAddress The user Ethereum address who would like to fill this order. * Must be available via the supplied Web3.Provider passed to 0x.js. */ - public async validateFillOrKillOrderAndThrowIfInvalidAsync(signedOrder: SignedOrder, - fillTakerTokenAmount: BigNumber.BigNumber, - takerAddress: string): Promise<void> { + public async validateFillOrKillOrderThrowIfInvalidAsync(signedOrder: SignedOrder, + fillTakerTokenAmount: BigNumber.BigNumber, + takerAddress: string): Promise<void> { assert.doesConformToSchema('signedOrder', signedOrder, signedOrderSchema); assert.isBigNumber('fillTakerTokenAmount', fillTakerTokenAmount); await assert.isSenderAddressAsync('takerAddress', takerAddress, this._web3Wrapper); const zrxTokenAddress = await this._getZRXTokenAddressAsync(); - await this._orderValidationUtils.validateFillOrKillOrderAndThrowIfInvalidAsync( + await this._orderValidationUtils.validateFillOrKillOrderThrowIfInvalidAsync( signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress); } public async isRoundingErrorAsync(numerator: BigNumber.BigNumber, diff --git a/src/utils/order_validation_utils.ts b/src/utils/order_validation_utils.ts index 686dfe33f..691c8a7e0 100644 --- a/src/utils/order_validation_utils.ts +++ b/src/utils/order_validation_utils.ts @@ -11,10 +11,10 @@ export class OrderValidationUtils { this.tokenWrapper = tokenWrapper; this.exchangeWrapper = exchangeWrapper; } - public async validateFillOrderAndThrowIfInvalidAsync(signedOrder: SignedOrder, - fillTakerTokenAmount: BigNumber.BigNumber, - takerAddress: string, - zrxTokenAddress: string): Promise<void> { + public async validateFillOrderThrowIfInvalidAsync(signedOrder: SignedOrder, + fillTakerTokenAmount: BigNumber.BigNumber, + takerAddress: string, + zrxTokenAddress: string): Promise<void> { if (fillTakerTokenAmount.eq(0)) { throw new Error(ExchangeContractErrs.OrderRemainingFillAmountZero); } @@ -36,11 +36,11 @@ export class OrderValidationUtils { throw new Error(ExchangeContractErrs.OrderFillRoundingError); } } - public async validateFillOrKillOrderAndThrowIfInvalidAsync(signedOrder: SignedOrder, - fillTakerTokenAmount: BigNumber.BigNumber, - takerAddress: string, - zrxTokenAddress: string): Promise<void> { - await this.validateFillOrderAndThrowIfInvalidAsync( + public async validateFillOrKillOrderThrowIfInvalidAsync(signedOrder: SignedOrder, + fillTakerTokenAmount: BigNumber.BigNumber, + takerAddress: string, + zrxTokenAddress: string): Promise<void> { + await this.validateFillOrderThrowIfInvalidAsync( signedOrder, fillTakerTokenAmount, takerAddress, zrxTokenAddress); // Check that fillValue available >= fillTakerAmount const orderHashHex = utils.getOrderHashHex(signedOrder); @@ -50,9 +50,9 @@ export class OrderValidationUtils { throw new Error(ExchangeContractErrs.InsufficientRemainingFillAmount); } } - public async validateCancelOrderAndThrowIfInvalidAsync(order: Order, - cancelTakerTokenAmount: BigNumber.BigNumber, - unavailableTakerTokenAmount: BigNumber.BigNumber, + public async validateCancelOrderThrowIfInvalidAsync(order: Order, + cancelTakerTokenAmount: BigNumber.BigNumber, + unavailableTakerTokenAmount: BigNumber.BigNumber, ): Promise<void> { if (cancelTakerTokenAmount.eq(0)) { throw new Error(ExchangeContractErrs.OrderCancelAmountZero); diff --git a/test/order_validation_test.ts b/test/order_validation_test.ts index a2e8e520c..162527e5d 100644 --- a/test/order_validation_test.ts +++ b/test/order_validation_test.ts @@ -8,6 +8,7 @@ import {ZeroEx, SignedOrder, Token, ExchangeContractErrs} from '../src'; import {TokenUtils} from './utils/token_utils'; import {BlockchainLifecycle} from './utils/blockchain_lifecycle'; import {FillScenarios} from './utils/fill_scenarios'; +import {OrderValidationUtils} from '../src/utils/order_validation_utils'; chaiSetup.configure(); const expect = chai.expect; @@ -28,6 +29,7 @@ describe('OrderValidation', () => { let makerAddress: string; let takerAddress: string; let feeRecipient: string; + let orderValidationUtils: OrderValidationUtils; const fillableAmount = new BigNumber(5); const fillTakerAmount = new BigNumber(5); const shouldThrowOnInsufficientBalanceOrAllowance = false; @@ -44,6 +46,7 @@ describe('OrderValidation', () => { const [makerToken, takerToken] = tokenUtils.getNonProtocolTokens(); makerTokenAddress = makerToken.address; takerTokenAddress = takerToken.address; + orderValidationUtils = new OrderValidationUtils(zeroEx.token, zeroEx.exchange); }); beforeEach(async () => { await blockchainLifecycle.startAsync(); @@ -57,7 +60,7 @@ describe('OrderValidation', () => { makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); const zeroFillAmount = new BigNumber(0); - return expect(zeroEx.exchange.validateFillOrderAndThrowIfInvalidAsync( + return expect(zeroEx.exchange.validateFillOrderThrowIfInvalidAsync( signedOrder, zeroFillAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.OrderRemainingFillAmountZero); }); @@ -66,7 +69,7 @@ describe('OrderValidation', () => { makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, ); const nonTakerAddress = userAddresses[6]; - return expect(zeroEx.exchange.validateFillOrderAndThrowIfInvalidAsync( + return expect(zeroEx.exchange.validateFillOrderThrowIfInvalidAsync( signedOrder, fillTakerAmount, nonTakerAddress, )).to.be.rejectedWith(ExchangeContractErrs.TransactionSenderIsNotFillOrderTaker); }); @@ -76,7 +79,7 @@ describe('OrderValidation', () => { makerTokenAddress, takerTokenAddress, makerAddress, takerAddress, fillableAmount, expirationInPast, ); - return expect(zeroEx.exchange.validateFillOrderAndThrowIfInvalidAsync( + return expect(zeroEx.exchange.validateFillOrderThrowIfInvalidAsync( signedOrder, fillTakerAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.OrderFillExpired); }); @@ -88,7 +91,7 @@ describe('OrderValidation', () => { makerAmount, takerAmount, ); const fillTakerAmountThatCausesRoundingError = new BigNumber(3); - return expect(zeroEx.exchange.validateFillOrderAndThrowIfInvalidAsync( + return expect(zeroEx.exchange.validateFillOrderThrowIfInvalidAsync( signedOrder, fillTakerAmountThatCausesRoundingError, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.OrderFillRoundingError); }); @@ -105,7 +108,7 @@ describe('OrderValidation', () => { await zeroEx.token.transferAsync(makerTokenAddress, coinbase, makerAddress, fillAmountDifference); await zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, tooLargeFillAmount); - return expect(zeroEx.exchange.validateFillOrKillOrderAndThrowIfInvalidAsync( + return expect(zeroEx.exchange.validateFillOrKillOrderThrowIfInvalidAsync( signedOrder, tooLargeFillAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.InsufficientRemainingFillAmount); }); @@ -126,7 +129,7 @@ describe('OrderValidation', () => { }); it('should throw when cancel amount is zero', async () => { const zeroCancelAmount = new BigNumber(0); - return expect(zeroEx.exchange.validateCancelOrderAndThrowIfInvalidAsync(signedOrder, zeroCancelAmount)) + return expect(zeroEx.exchange.validateCancelOrderThrowIfInvalidAsync(signedOrder, zeroCancelAmount)) .to.be.rejectedWith(ExchangeContractErrs.OrderCancelAmountZero); }); it('should throw when order is expired', async () => { @@ -136,16 +139,16 @@ describe('OrderValidation', () => { fillableAmount, expirationInPast, ); orderHashHex = ZeroEx.getOrderHashHex(expiredSignedOrder); - return expect(zeroEx.exchange.validateCancelOrderAndThrowIfInvalidAsync(expiredSignedOrder, cancelAmount)) + return expect(zeroEx.exchange.validateCancelOrderThrowIfInvalidAsync(expiredSignedOrder, cancelAmount)) .to.be.rejectedWith(ExchangeContractErrs.OrderCancelExpired); }); it('should throw when order is already cancelled or filled', async () => { await zeroEx.exchange.cancelOrderAsync(signedOrder, fillableAmount); - return expect(zeroEx.exchange.validateCancelOrderAndThrowIfInvalidAsync(signedOrder, fillableAmount)) + return expect(zeroEx.exchange.validateCancelOrderThrowIfInvalidAsync(signedOrder, fillableAmount)) .to.be.rejectedWith(ExchangeContractErrs.OrderAlreadyCancelledOrFilled); }); }); - describe('#validateFillOrderBalancesAndAllowancesAndThrowIfInvalidAsync', () => { + describe('#validateFillOrderTakerBalancesAllowancesThrowIfInvalidAsync', () => { describe('should throw when not enough balance or allowance to fulfill the order', () => { const balanceToSubtractFromMaker = new BigNumber(3); const lackingAllowance = new BigNumber(3); @@ -159,32 +162,32 @@ describe('OrderValidation', () => { await zeroEx.token.transferAsync( takerTokenAddress, takerAddress, coinbase, balanceToSubtractFromMaker, ); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress, + return expect((orderValidationUtils as any).validateFillOrderTakerBalancesAllowancesThrowIfInvalidAsync( + signedOrder, fillTakerAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.InsufficientTakerBalance); }); it('should throw when taker allowance is less than fill amount', async () => { const newAllowanceWhichIsLessThanFillAmount = fillTakerAmount.minus(lackingAllowance); await zeroEx.token.setProxyAllowanceAsync(takerTokenAddress, takerAddress, newAllowanceWhichIsLessThanFillAmount); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress, + return expect((orderValidationUtils as any).validateFillOrderTakerBalancesAllowancesThrowIfInvalidAsync( + signedOrder, fillTakerAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.InsufficientTakerAllowance); }); it('should throw when maker balance is less than maker fill amount', async () => { await zeroEx.token.transferAsync( makerTokenAddress, makerAddress, coinbase, balanceToSubtractFromMaker, ); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress, + return expect((orderValidationUtils as any).validateFillOrderTakerBalancesAllowancesThrowIfInvalidAsync( + signedOrder, fillTakerAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.InsufficientMakerBalance); }); it('should throw when maker allowance is less than maker fill amount', async () => { const newAllowanceWhichIsLessThanFillAmount = fillTakerAmount.minus(lackingAllowance); await zeroEx.token.setProxyAllowanceAsync(makerTokenAddress, makerAddress, newAllowanceWhichIsLessThanFillAmount); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress, + return expect((orderValidationUtils as any).validateFillOrderTakerBalancesAllowancesThrowIfInvalidAsync( + signedOrder, fillTakerAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.InsufficientMakerAllowance); }); }); @@ -203,16 +206,16 @@ describe('OrderValidation', () => { await zeroEx.token.transferAsync( zrxTokenAddress, makerAddress, coinbase, balanceToSubtractFromMaker, ); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress, + return expect((orderValidationUtils as any).validateFillOrderTakerBalancesAllowancesThrowIfInvalidAsync( + signedOrder, fillTakerAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.InsufficientMakerFeeBalance); }); it('should throw when maker doesn\'t have enough allowance to pay fees', async () => { const newAllowanceWhichIsLessThanFees = makerFee.minus(1); await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, makerAddress, newAllowanceWhichIsLessThanFees); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress, + return expect((orderValidationUtils as any).validateFillOrderTakerBalancesAllowancesThrowIfInvalidAsync( + signedOrder, fillTakerAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.InsufficientMakerFeeAllowance); }); it('should throw when taker doesn\'t have enough balance to pay fees', async () => { @@ -220,16 +223,16 @@ describe('OrderValidation', () => { await zeroEx.token.transferAsync( zrxTokenAddress, takerAddress, coinbase, balanceToSubtractFromTaker, ); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress, + return expect((orderValidationUtils as any).validateFillOrderTakerBalancesAllowancesThrowIfInvalidAsync( + signedOrder, fillTakerAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.InsufficientTakerFeeBalance); }); it('should throw when taker doesn\'t have enough allowance to pay fees', async () => { const newAllowanceWhichIsLessThanFees = makerFee.minus(1); await zeroEx.token.setProxyAllowanceAsync(zrxTokenAddress, takerAddress, newAllowanceWhichIsLessThanFees); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress, + return expect((orderValidationUtils as any).validateFillOrderTakerBalancesAllowancesThrowIfInvalidAsync( + signedOrder, fillTakerAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.InsufficientTakerFeeAllowance); }); }); @@ -249,8 +252,8 @@ describe('OrderValidation', () => { await zeroEx.token.transferAsync( zrxTokenAddress, makerAddress, coinbase, balanceToSubtractFromMaker, ); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress, + return expect((orderValidationUtils as any).validateFillOrderTakerBalancesAllowancesThrowIfInvalidAsync( + signedOrder, fillTakerAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.InsufficientMakerBalance); }); it('should throw on insufficient allowance when makerToken is ZRX', async () => { @@ -258,8 +261,8 @@ describe('OrderValidation', () => { const newAllowanceWhichIsInsufficient = oldAllowance.minus(1); await zeroEx.token.setProxyAllowanceAsync( zrxTokenAddress, makerAddress, newAllowanceWhichIsInsufficient); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress, + return expect((orderValidationUtils as any).validateFillOrderTakerBalancesAllowancesThrowIfInvalidAsync( + signedOrder, fillTakerAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.InsufficientMakerAllowance); }); }); @@ -279,8 +282,8 @@ describe('OrderValidation', () => { await zeroEx.token.transferAsync( zrxTokenAddress, takerAddress, coinbase, balanceToSubtractFromTaker, ); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress, + return expect((orderValidationUtils as any).validateFillOrderTakerBalancesAllowancesThrowIfInvalidAsync( + signedOrder, fillTakerAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.InsufficientTakerBalance); }); it('should throw on insufficient allowance when takerToken is ZRX', async () => { @@ -288,8 +291,8 @@ describe('OrderValidation', () => { const newAllowanceWhichIsInsufficient = oldAllowance.minus(1); await zeroEx.token.setProxyAllowanceAsync( zrxTokenAddress, takerAddress, newAllowanceWhichIsInsufficient); - return expect(zeroEx.exchange.fillOrderAsync( - signedOrder, fillTakerAmount, shouldThrowOnInsufficientBalanceOrAllowance, takerAddress, + return expect((orderValidationUtils as any).validateFillOrderTakerBalancesAllowancesThrowIfInvalidAsync( + signedOrder, fillTakerAmount, takerAddress, )).to.be.rejectedWith(ExchangeContractErrs.InsufficientTakerAllowance); }); }); |