Rename OrderAlreadyCancelledOrFilled -> OrderCancelled.
Remove try catch of throwing errors in favour of returning the Errors in a OrderValidationResult
This commit is contained in:
@@ -25,6 +25,14 @@ interface SidedOrderRelevantState {
|
|||||||
remainingFillableAssetAmount: BigNumber;
|
remainingFillableAssetAmount: BigNumber;
|
||||||
isOrderCancelled: boolean;
|
isOrderCancelled: boolean;
|
||||||
}
|
}
|
||||||
|
interface OrderValidResult {
|
||||||
|
isValid: true;
|
||||||
|
}
|
||||||
|
interface OrderInvalidResult {
|
||||||
|
isValid: false;
|
||||||
|
error: ExchangeContractErrs;
|
||||||
|
}
|
||||||
|
type OrderValidationResult = OrderValidResult | OrderInvalidResult;
|
||||||
|
|
||||||
export class OrderStateUtils {
|
export class OrderStateUtils {
|
||||||
private readonly _balanceAndProxyAllowanceFetcher: AbstractBalanceAndProxyAllowanceFetcher;
|
private readonly _balanceAndProxyAllowanceFetcher: AbstractBalanceAndProxyAllowanceFetcher;
|
||||||
@@ -32,46 +40,42 @@ export class OrderStateUtils {
|
|||||||
private static _validateIfOrderIsValid(
|
private static _validateIfOrderIsValid(
|
||||||
signedOrder: SignedOrder,
|
signedOrder: SignedOrder,
|
||||||
sidedOrderRelevantState: SidedOrderRelevantState,
|
sidedOrderRelevantState: SidedOrderRelevantState,
|
||||||
): void {
|
): OrderValidationResult {
|
||||||
const isMakerSide = sidedOrderRelevantState.isMakerSide;
|
const isMakerSide = sidedOrderRelevantState.isMakerSide;
|
||||||
if (sidedOrderRelevantState.isOrderCancelled) {
|
if (sidedOrderRelevantState.isOrderCancelled) {
|
||||||
throw new Error(ExchangeContractErrs.OrderAlreadyCancelledOrFilled);
|
return { isValid: false, error: ExchangeContractErrs.OrderCancelled };
|
||||||
}
|
}
|
||||||
const availableTakerAssetAmount = signedOrder.takerAssetAmount.minus(
|
const availableTakerAssetAmount = signedOrder.takerAssetAmount.minus(
|
||||||
sidedOrderRelevantState.filledTakerAssetAmount,
|
sidedOrderRelevantState.filledTakerAssetAmount,
|
||||||
);
|
);
|
||||||
if (availableTakerAssetAmount.eq(0)) {
|
if (availableTakerAssetAmount.eq(0)) {
|
||||||
throw new Error(ExchangeContractErrs.OrderRemainingFillAmountZero);
|
return { isValid: false, error: ExchangeContractErrs.OrderRemainingFillAmountZero };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sidedOrderRelevantState.traderBalance.eq(0)) {
|
if (sidedOrderRelevantState.traderBalance.eq(0)) {
|
||||||
throw new Error(
|
const error = isMakerSide
|
||||||
isMakerSide
|
? ExchangeContractErrs.InsufficientMakerBalance
|
||||||
? ExchangeContractErrs.InsufficientMakerBalance
|
: ExchangeContractErrs.InsufficientTakerBalance;
|
||||||
: ExchangeContractErrs.InsufficientTakerBalance,
|
return { isValid: false, error };
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if (sidedOrderRelevantState.traderProxyAllowance.eq(0)) {
|
if (sidedOrderRelevantState.traderProxyAllowance.eq(0)) {
|
||||||
throw new Error(
|
const error = isMakerSide
|
||||||
isMakerSide
|
? ExchangeContractErrs.InsufficientMakerAllowance
|
||||||
? ExchangeContractErrs.InsufficientMakerAllowance
|
: ExchangeContractErrs.InsufficientTakerAllowance;
|
||||||
: ExchangeContractErrs.InsufficientTakerAllowance,
|
return { isValid: false, error };
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if (!signedOrder.makerFee.eq(0)) {
|
if (!signedOrder.makerFee.eq(0)) {
|
||||||
if (sidedOrderRelevantState.traderFeeBalance.eq(0)) {
|
if (sidedOrderRelevantState.traderFeeBalance.eq(0)) {
|
||||||
throw new Error(
|
const error = isMakerSide
|
||||||
isMakerSide
|
? ExchangeContractErrs.InsufficientMakerFeeBalance
|
||||||
? ExchangeContractErrs.InsufficientMakerFeeBalance
|
: ExchangeContractErrs.InsufficientTakerFeeBalance;
|
||||||
: ExchangeContractErrs.InsufficientTakerFeeBalance,
|
return { isValid: false, error };
|
||||||
);
|
|
||||||
}
|
}
|
||||||
if (sidedOrderRelevantState.traderFeeProxyAllowance.eq(0)) {
|
if (sidedOrderRelevantState.traderFeeProxyAllowance.eq(0)) {
|
||||||
throw new Error(
|
const error = isMakerSide
|
||||||
isMakerSide
|
? ExchangeContractErrs.InsufficientMakerFeeAllowance
|
||||||
? ExchangeContractErrs.InsufficientMakerFeeAllowance
|
: ExchangeContractErrs.InsufficientTakerFeeAllowance;
|
||||||
: ExchangeContractErrs.InsufficientTakerFeeAllowance,
|
return { isValid: false, error };
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const remainingTakerAssetAmount = signedOrder.takerAssetAmount.minus(
|
const remainingTakerAssetAmount = signedOrder.takerAssetAmount.minus(
|
||||||
@@ -83,8 +87,9 @@ export class OrderStateUtils {
|
|||||||
signedOrder.makerAssetAmount,
|
signedOrder.makerAssetAmount,
|
||||||
);
|
);
|
||||||
if (isRoundingError) {
|
if (isRoundingError) {
|
||||||
throw new Error(ExchangeContractErrs.OrderFillRoundingError);
|
return { isValid: false, error: ExchangeContractErrs.OrderFillRoundingError };
|
||||||
}
|
}
|
||||||
|
return { isValid: true };
|
||||||
}
|
}
|
||||||
constructor(
|
constructor(
|
||||||
balanceAndProxyAllowanceFetcher: AbstractBalanceAndProxyAllowanceFetcher,
|
balanceAndProxyAllowanceFetcher: AbstractBalanceAndProxyAllowanceFetcher,
|
||||||
@@ -107,19 +112,19 @@ export class OrderStateUtils {
|
|||||||
remainingFillableAssetAmount: orderRelevantState.remainingFillableMakerAssetAmount,
|
remainingFillableAssetAmount: orderRelevantState.remainingFillableMakerAssetAmount,
|
||||||
isOrderCancelled,
|
isOrderCancelled,
|
||||||
};
|
};
|
||||||
try {
|
const orderValidationResult = OrderStateUtils._validateIfOrderIsValid(signedOrder, sidedOrderRelevantState);
|
||||||
OrderStateUtils._validateIfOrderIsValid(signedOrder, sidedOrderRelevantState);
|
if (orderValidationResult.isValid) {
|
||||||
const orderState: OrderStateValid = {
|
const orderState: OrderStateValid = {
|
||||||
isValid: true,
|
isValid: true,
|
||||||
orderHash,
|
orderHash,
|
||||||
orderRelevantState,
|
orderRelevantState,
|
||||||
};
|
};
|
||||||
return orderState;
|
return orderState;
|
||||||
} catch (err) {
|
} else {
|
||||||
const orderState: OrderStateInvalid = {
|
const orderState: OrderStateInvalid = {
|
||||||
isValid: false,
|
isValid: false,
|
||||||
orderHash,
|
orderHash,
|
||||||
error: err.message,
|
error: orderValidationResult.error,
|
||||||
};
|
};
|
||||||
return orderState;
|
return orderState;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ import { BigNumber } from '@0xproject/utils';
|
|||||||
import * as chai from 'chai';
|
import * as chai from 'chai';
|
||||||
import 'mocha';
|
import 'mocha';
|
||||||
|
|
||||||
import { AbstractBalanceAndProxyAllowanceFetcher, AbstractOrderFilledCancelledFetcher, OrderStateUtils } from '../src';
|
import { AbstractBalanceAndProxyAllowanceFetcher } from '../src/abstract/abstract_balance_and_proxy_allowance_fetcher';
|
||||||
|
import { AbstractOrderFilledCancelledFetcher } from '../src/abstract/abstract_order_filled_cancelled_fetcher';
|
||||||
|
import { OrderStateUtils } from '../src/order_state_utils';
|
||||||
|
|
||||||
import { chaiSetup } from './utils/chai_setup';
|
import { chaiSetup } from './utils/chai_setup';
|
||||||
import { testOrderFactory } from './utils/test_order_factory';
|
import { testOrderFactory } from './utils/test_order_factory';
|
||||||
|
|||||||
@@ -501,7 +501,7 @@ describe('OrderWatcher', () => {
|
|||||||
expect(orderState.isValid).to.be.false();
|
expect(orderState.isValid).to.be.false();
|
||||||
const invalidOrderState = orderState as OrderStateInvalid;
|
const invalidOrderState = orderState as OrderStateInvalid;
|
||||||
expect(invalidOrderState.orderHash).to.be.equal(orderHash);
|
expect(invalidOrderState.orderHash).to.be.equal(orderHash);
|
||||||
expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.OrderAlreadyCancelledOrFilled);
|
expect(invalidOrderState.error).to.be.equal(ExchangeContractErrs.OrderCancelled);
|
||||||
});
|
});
|
||||||
orderWatcher.subscribe(callback);
|
orderWatcher.subscribe(callback);
|
||||||
await contractWrappers.exchange.cancelOrderAsync(signedOrder);
|
await contractWrappers.exchange.cancelOrderAsync(signedOrder);
|
||||||
|
|||||||
@@ -62,8 +62,7 @@ export interface ValidatorSignature {
|
|||||||
export enum ExchangeContractErrs {
|
export enum ExchangeContractErrs {
|
||||||
OrderFillExpired = 'ORDER_FILL_EXPIRED',
|
OrderFillExpired = 'ORDER_FILL_EXPIRED',
|
||||||
OrderCancelExpired = 'ORDER_CANCEL_EXPIRED',
|
OrderCancelExpired = 'ORDER_CANCEL_EXPIRED',
|
||||||
OrderCancelAmountZero = 'ORDER_CANCEL_AMOUNT_ZERO',
|
OrderCancelled = 'ORDER_CANCELLED',
|
||||||
OrderAlreadyCancelledOrFilled = 'ORDER_ALREADY_CANCELLED_OR_FILLED',
|
|
||||||
OrderFillAmountZero = 'ORDER_FILL_AMOUNT_ZERO',
|
OrderFillAmountZero = 'ORDER_FILL_AMOUNT_ZERO',
|
||||||
OrderRemainingFillAmountZero = 'ORDER_REMAINING_FILL_AMOUNT_ZERO',
|
OrderRemainingFillAmountZero = 'ORDER_REMAINING_FILL_AMOUNT_ZERO',
|
||||||
OrderFillRoundingError = 'ORDER_FILL_ROUNDING_ERROR',
|
OrderFillRoundingError = 'ORDER_FILL_ROUNDING_ERROR',
|
||||||
|
|||||||
@@ -247,12 +247,9 @@ export const utils = {
|
|||||||
} = {
|
} = {
|
||||||
[ExchangeContractErrs.OrderFillExpired]: 'This order has expired',
|
[ExchangeContractErrs.OrderFillExpired]: 'This order has expired',
|
||||||
[ExchangeContractErrs.OrderCancelExpired]: 'This order has expired',
|
[ExchangeContractErrs.OrderCancelExpired]: 'This order has expired',
|
||||||
[ExchangeContractErrs.OrderCancelAmountZero]: "Order cancel amount can't be 0",
|
[ExchangeContractErrs.OrderCancelled]: 'This order has been cancelled',
|
||||||
[ExchangeContractErrs.OrderAlreadyCancelledOrFilled]:
|
|
||||||
'This order has already been completely filled or cancelled',
|
|
||||||
[ExchangeContractErrs.OrderFillAmountZero]: "Order fill amount can't be 0",
|
[ExchangeContractErrs.OrderFillAmountZero]: "Order fill amount can't be 0",
|
||||||
[ExchangeContractErrs.OrderRemainingFillAmountZero]:
|
[ExchangeContractErrs.OrderRemainingFillAmountZero]: 'This order has already been completely filled',
|
||||||
'This order has already been completely filled or cancelled',
|
|
||||||
[ExchangeContractErrs.OrderFillRoundingError]:
|
[ExchangeContractErrs.OrderFillRoundingError]:
|
||||||
'Rounding error will occur when filling this order. Please try filling a different amount.',
|
'Rounding error will occur when filling this order. Please try filling a different amount.',
|
||||||
[ExchangeContractErrs.InsufficientTakerBalance]:
|
[ExchangeContractErrs.InsufficientTakerBalance]:
|
||||||
|
|||||||
Reference in New Issue
Block a user