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;
|
||||
isOrderCancelled: boolean;
|
||||
}
|
||||
interface OrderValidResult {
|
||||
isValid: true;
|
||||
}
|
||||
interface OrderInvalidResult {
|
||||
isValid: false;
|
||||
error: ExchangeContractErrs;
|
||||
}
|
||||
type OrderValidationResult = OrderValidResult | OrderInvalidResult;
|
||||
|
||||
export class OrderStateUtils {
|
||||
private readonly _balanceAndProxyAllowanceFetcher: AbstractBalanceAndProxyAllowanceFetcher;
|
||||
@@ -32,46 +40,42 @@ export class OrderStateUtils {
|
||||
private static _validateIfOrderIsValid(
|
||||
signedOrder: SignedOrder,
|
||||
sidedOrderRelevantState: SidedOrderRelevantState,
|
||||
): void {
|
||||
): OrderValidationResult {
|
||||
const isMakerSide = sidedOrderRelevantState.isMakerSide;
|
||||
if (sidedOrderRelevantState.isOrderCancelled) {
|
||||
throw new Error(ExchangeContractErrs.OrderAlreadyCancelledOrFilled);
|
||||
return { isValid: false, error: ExchangeContractErrs.OrderCancelled };
|
||||
}
|
||||
const availableTakerAssetAmount = signedOrder.takerAssetAmount.minus(
|
||||
sidedOrderRelevantState.filledTakerAssetAmount,
|
||||
);
|
||||
if (availableTakerAssetAmount.eq(0)) {
|
||||
throw new Error(ExchangeContractErrs.OrderRemainingFillAmountZero);
|
||||
return { isValid: false, error: ExchangeContractErrs.OrderRemainingFillAmountZero };
|
||||
}
|
||||
|
||||
if (sidedOrderRelevantState.traderBalance.eq(0)) {
|
||||
throw new Error(
|
||||
isMakerSide
|
||||
? ExchangeContractErrs.InsufficientMakerBalance
|
||||
: ExchangeContractErrs.InsufficientTakerBalance,
|
||||
);
|
||||
const error = isMakerSide
|
||||
? ExchangeContractErrs.InsufficientMakerBalance
|
||||
: ExchangeContractErrs.InsufficientTakerBalance;
|
||||
return { isValid: false, error };
|
||||
}
|
||||
if (sidedOrderRelevantState.traderProxyAllowance.eq(0)) {
|
||||
throw new Error(
|
||||
isMakerSide
|
||||
? ExchangeContractErrs.InsufficientMakerAllowance
|
||||
: ExchangeContractErrs.InsufficientTakerAllowance,
|
||||
);
|
||||
const error = isMakerSide
|
||||
? ExchangeContractErrs.InsufficientMakerAllowance
|
||||
: ExchangeContractErrs.InsufficientTakerAllowance;
|
||||
return { isValid: false, error };
|
||||
}
|
||||
if (!signedOrder.makerFee.eq(0)) {
|
||||
if (sidedOrderRelevantState.traderFeeBalance.eq(0)) {
|
||||
throw new Error(
|
||||
isMakerSide
|
||||
? ExchangeContractErrs.InsufficientMakerFeeBalance
|
||||
: ExchangeContractErrs.InsufficientTakerFeeBalance,
|
||||
);
|
||||
const error = isMakerSide
|
||||
? ExchangeContractErrs.InsufficientMakerFeeBalance
|
||||
: ExchangeContractErrs.InsufficientTakerFeeBalance;
|
||||
return { isValid: false, error };
|
||||
}
|
||||
if (sidedOrderRelevantState.traderFeeProxyAllowance.eq(0)) {
|
||||
throw new Error(
|
||||
isMakerSide
|
||||
? ExchangeContractErrs.InsufficientMakerFeeAllowance
|
||||
: ExchangeContractErrs.InsufficientTakerFeeAllowance,
|
||||
);
|
||||
const error = isMakerSide
|
||||
? ExchangeContractErrs.InsufficientMakerFeeAllowance
|
||||
: ExchangeContractErrs.InsufficientTakerFeeAllowance;
|
||||
return { isValid: false, error };
|
||||
}
|
||||
}
|
||||
const remainingTakerAssetAmount = signedOrder.takerAssetAmount.minus(
|
||||
@@ -83,8 +87,9 @@ export class OrderStateUtils {
|
||||
signedOrder.makerAssetAmount,
|
||||
);
|
||||
if (isRoundingError) {
|
||||
throw new Error(ExchangeContractErrs.OrderFillRoundingError);
|
||||
return { isValid: false, error: ExchangeContractErrs.OrderFillRoundingError };
|
||||
}
|
||||
return { isValid: true };
|
||||
}
|
||||
constructor(
|
||||
balanceAndProxyAllowanceFetcher: AbstractBalanceAndProxyAllowanceFetcher,
|
||||
@@ -107,19 +112,19 @@ export class OrderStateUtils {
|
||||
remainingFillableAssetAmount: orderRelevantState.remainingFillableMakerAssetAmount,
|
||||
isOrderCancelled,
|
||||
};
|
||||
try {
|
||||
OrderStateUtils._validateIfOrderIsValid(signedOrder, sidedOrderRelevantState);
|
||||
const orderValidationResult = OrderStateUtils._validateIfOrderIsValid(signedOrder, sidedOrderRelevantState);
|
||||
if (orderValidationResult.isValid) {
|
||||
const orderState: OrderStateValid = {
|
||||
isValid: true,
|
||||
orderHash,
|
||||
orderRelevantState,
|
||||
};
|
||||
return orderState;
|
||||
} catch (err) {
|
||||
} else {
|
||||
const orderState: OrderStateInvalid = {
|
||||
isValid: false,
|
||||
orderHash,
|
||||
error: err.message,
|
||||
error: orderValidationResult.error,
|
||||
};
|
||||
return orderState;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user