aboutsummaryrefslogtreecommitdiffstats
path: root/packages/order-utils/src
diff options
context:
space:
mode:
authorJacob Evans <jacob@dekz.net>2018-08-13 10:23:29 +0800
committerJacob Evans <jacob@dekz.net>2018-08-15 11:06:32 +0800
commit622509c508272790e3e69c09cf1a1f9696815147 (patch)
tree0a86370cc0e02a2ede06e48baa5ad123d0617276 /packages/order-utils/src
parentf9f232f5d9527926cd64027f69491b0bc6e58894 (diff)
downloaddexon-sol-tools-622509c508272790e3e69c09cf1a1f9696815147.tar
dexon-sol-tools-622509c508272790e3e69c09cf1a1f9696815147.tar.gz
dexon-sol-tools-622509c508272790e3e69c09cf1a1f9696815147.tar.bz2
dexon-sol-tools-622509c508272790e3e69c09cf1a1f9696815147.tar.lz
dexon-sol-tools-622509c508272790e3e69c09cf1a1f9696815147.tar.xz
dexon-sol-tools-622509c508272790e3e69c09cf1a1f9696815147.tar.zst
dexon-sol-tools-622509c508272790e3e69c09cf1a1f9696815147.zip
[Order-utils] Order is valid when maker amount is very small
Previously our min fillable calculation would throw a rounding error when encountering a valid order (with a small maker amount). This was inconsistent with the on-chain logic which allowed this order to be filled.
Diffstat (limited to 'packages/order-utils/src')
-rw-r--r--packages/order-utils/src/order_state_utils.ts36
1 files changed, 17 insertions, 19 deletions
diff --git a/packages/order-utils/src/order_state_utils.ts b/packages/order-utils/src/order_state_utils.ts
index 189bf4180..7974d5d0b 100644
--- a/packages/order-utils/src/order_state_utils.ts
+++ b/packages/order-utils/src/order_state_utils.ts
@@ -11,6 +11,7 @@ import { BigNumber } from '@0xproject/utils';
import { AbstractBalanceAndProxyAllowanceFetcher } from './abstract/abstract_balance_and_proxy_allowance_fetcher';
import { AbstractOrderFilledCancelledFetcher } from './abstract/abstract_order_filled_cancelled_fetcher';
import { orderHashUtils } from './order_hash';
+import { OrderValidationUtils } from './order_validation_utils';
import { RemainingFillableCalculator } from './remaining_fillable_calculator';
import { utils } from './utils';
@@ -22,10 +23,9 @@ interface SidedOrderRelevantState {
traderFeeProxyAllowance: BigNumber;
filledTakerAssetAmount: BigNumber;
remainingFillableAssetAmount: BigNumber;
+ isOrderCancelled: boolean;
}
-const ACCEPTABLE_RELATIVE_ROUNDING_ERROR = 0.0001;
-
export class OrderStateUtils {
private readonly _balanceAndProxyAllowanceFetcher: AbstractBalanceAndProxyAllowanceFetcher;
private readonly _orderFilledCancelledFetcher: AbstractOrderFilledCancelledFetcher;
@@ -34,6 +34,9 @@ export class OrderStateUtils {
sidedOrderRelevantState: SidedOrderRelevantState,
): void {
const isMakerSide = sidedOrderRelevantState.isMakerSide;
+ if (sidedOrderRelevantState.isOrderCancelled) {
+ throw new Error(ExchangeContractErrs.OrderAlreadyCancelledOrFilled);
+ }
const availableTakerAssetAmount = signedOrder.takerAssetAmount.minus(
sidedOrderRelevantState.filledTakerAssetAmount,
);
@@ -71,23 +74,15 @@ export class OrderStateUtils {
);
}
}
-
- let minFillableTakerAssetAmountWithinNoRoundingErrorRange;
- if (isMakerSide) {
- minFillableTakerAssetAmountWithinNoRoundingErrorRange = signedOrder.takerAssetAmount
- .dividedBy(ACCEPTABLE_RELATIVE_ROUNDING_ERROR)
- .dividedBy(signedOrder.makerAssetAmount);
- } else {
- minFillableTakerAssetAmountWithinNoRoundingErrorRange = signedOrder.makerAssetAmount
- .dividedBy(ACCEPTABLE_RELATIVE_ROUNDING_ERROR)
- .dividedBy(signedOrder.takerAssetAmount);
- }
-
- if (
- sidedOrderRelevantState.remainingFillableAssetAmount.lessThan(
- minFillableTakerAssetAmountWithinNoRoundingErrorRange,
- )
- ) {
+ const remainingTakerAssetAmount = signedOrder.takerAssetAmount.minus(
+ sidedOrderRelevantState.filledTakerAssetAmount,
+ );
+ const isRoundingError = OrderValidationUtils.isRoundingError(
+ remainingTakerAssetAmount,
+ signedOrder.takerAssetAmount,
+ signedOrder.makerAssetAmount,
+ );
+ if (isRoundingError) {
throw new Error(ExchangeContractErrs.OrderFillRoundingError);
}
}
@@ -101,6 +96,7 @@ export class OrderStateUtils {
public async getOpenOrderStateAsync(signedOrder: SignedOrder): Promise<OrderState> {
const orderRelevantState = await this.getOpenOrderRelevantStateAsync(signedOrder);
const orderHash = orderHashUtils.getOrderHashHex(signedOrder);
+ const isOrderCancelled = await this._orderFilledCancelledFetcher.isOrderCancelledAsync(orderHash);
const sidedOrderRelevantState = {
isMakerSide: true,
traderBalance: orderRelevantState.makerBalance,
@@ -109,6 +105,7 @@ export class OrderStateUtils {
traderFeeProxyAllowance: orderRelevantState.makerFeeProxyAllowance,
filledTakerAssetAmount: orderRelevantState.filledTakerAssetAmount,
remainingFillableAssetAmount: orderRelevantState.remainingFillableMakerAssetAmount,
+ isOrderCancelled,
};
try {
OrderStateUtils._validateIfOrderIsValid(signedOrder, sidedOrderRelevantState);
@@ -278,6 +275,7 @@ export class OrderStateUtils {
traderFeeProxyAllowance,
filledTakerAssetAmount,
remainingFillableAssetAmount,
+ isOrderCancelled,
};
return sidedOrderRelevantState;
}