aboutsummaryrefslogtreecommitdiffstats
path: root/packages/0x.js/src/utils/order_state_utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/0x.js/src/utils/order_state_utils.ts')
-rw-r--r--packages/0x.js/src/utils/order_state_utils.ts91
1 files changed, 45 insertions, 46 deletions
diff --git a/packages/0x.js/src/utils/order_state_utils.ts b/packages/0x.js/src/utils/order_state_utils.ts
index 1d8f02a18..6b7f811ae 100644
--- a/packages/0x.js/src/utils/order_state_utils.ts
+++ b/packages/0x.js/src/utils/order_state_utils.ts
@@ -1,29 +1,61 @@
+import BigNumber from 'bignumber.js';
import * as _ from 'lodash';
import * as Web3 from 'web3';
-import BigNumber from 'bignumber.js';
+
+import {ZeroEx} from '../0x';
+import {ExchangeWrapper} from '../contract_wrappers/exchange_wrapper';
+import {TokenWrapper} from '../contract_wrappers/token_wrapper';
+import {RemainingFillableCalculator} from '../order_watcher/remaining_fillable_calculator';
+import {BalanceAndProxyAllowanceLazyStore} from '../stores/balance_proxy_allowance_lazy_store';
+import {OrderFilledCancelledLazyStore} from '../stores/order_filled_cancelled_lazy_store';
import {
ExchangeContractErrs,
- SignedOrder,
- OrderRelevantState,
MethodOpts,
+ OrderRelevantState,
OrderState,
- OrderStateValid,
OrderStateInvalid,
+ OrderStateValid,
+ SignedOrder,
} from '../types';
-import {ZeroEx} from '../0x';
-import {TokenWrapper} from '../contract_wrappers/token_wrapper';
-import {ExchangeWrapper} from '../contract_wrappers/exchange_wrapper';
-import {utils} from '../utils/utils';
import {constants} from '../utils/constants';
-import {OrderFilledCancelledLazyStore} from '../stores/order_filled_cancelled_lazy_store';
-import {BalanceAndProxyAllowanceLazyStore} from '../stores/balance_proxy_allowance_lazy_store';
-import {RemainingFillableCalculator} from '../order_watcher/remaining_fillable_calculator';
+import {utils} from '../utils/utils';
const ACCEPTABLE_RELATIVE_ROUNDING_ERROR = 0.0001;
export class OrderStateUtils {
private balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore;
private orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore;
+ private static validateIfOrderIsValid(signedOrder: SignedOrder, orderRelevantState: OrderRelevantState): void {
+ const unavailableTakerTokenAmount = orderRelevantState.cancelledTakerTokenAmount.add(
+ orderRelevantState.filledTakerTokenAmount,
+ );
+ const availableTakerTokenAmount = signedOrder.takerTokenAmount.minus(unavailableTakerTokenAmount);
+ if (availableTakerTokenAmount.eq(0)) {
+ throw new Error(ExchangeContractErrs.OrderRemainingFillAmountZero);
+ }
+
+ if (orderRelevantState.makerBalance.eq(0)) {
+ throw new Error(ExchangeContractErrs.InsufficientMakerBalance);
+ }
+ if (orderRelevantState.makerProxyAllowance.eq(0)) {
+ throw new Error(ExchangeContractErrs.InsufficientMakerAllowance);
+ }
+ if (!signedOrder.makerFee.eq(0)) {
+ if (orderRelevantState.makerFeeBalance.eq(0)) {
+ throw new Error(ExchangeContractErrs.InsufficientMakerFeeBalance);
+ }
+ if (orderRelevantState.makerFeeProxyAllowance.eq(0)) {
+ throw new Error(ExchangeContractErrs.InsufficientMakerFeeAllowance);
+ }
+ }
+ const minFillableTakerTokenAmountWithinNoRoundingErrorRange = signedOrder.takerTokenAmount
+ .dividedBy(ACCEPTABLE_RELATIVE_ROUNDING_ERROR)
+ .dividedBy(signedOrder.makerTokenAmount);
+ if (orderRelevantState.remainingFillableTakerTokenAmount
+ .lessThan(minFillableTakerTokenAmountWithinNoRoundingErrorRange)) {
+ throw new Error(ExchangeContractErrs.OrderFillRoundingError);
+ }
+ }
constructor(balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore,
orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore) {
this.balanceAndProxyAllowanceLazyStore = balanceAndProxyAllowanceLazyStore;
@@ -33,7 +65,7 @@ export class OrderStateUtils {
const orderRelevantState = await this.getOrderRelevantStateAsync(signedOrder);
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
try {
- this.validateIfOrderIsValid(signedOrder, orderRelevantState);
+ OrderStateUtils.validateIfOrderIsValid(signedOrder, orderRelevantState);
const orderState: OrderStateValid = {
isValid: true,
orderHash,
@@ -55,7 +87,7 @@ export class OrderStateUtils {
// because JS doesn't support async constructors.
// Moreover - it's cached under the hood so it's equivalent to an async constructor.
const exchange = (this.orderFilledCancelledLazyStore as any).exchange as ExchangeWrapper;
- const zrxTokenAddress = await exchange.getZRXTokenAddressAsync();
+ const zrxTokenAddress = exchange.getZRXTokenAddress();
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
const makerBalance = await this.balanceAndProxyAllowanceLazyStore.getBalanceAsync(
signedOrder.makerTokenAddress, signedOrder.maker,
@@ -102,37 +134,4 @@ export class OrderStateUtils {
};
return orderRelevantState;
}
- private validateIfOrderIsValid(signedOrder: SignedOrder, orderRelevantState: OrderRelevantState): void {
- const unavailableTakerTokenAmount = orderRelevantState.cancelledTakerTokenAmount.add(
- orderRelevantState.filledTakerTokenAmount,
- );
- const availableTakerTokenAmount = signedOrder.takerTokenAmount.minus(unavailableTakerTokenAmount);
- if (availableTakerTokenAmount.eq(0)) {
- throw new Error(ExchangeContractErrs.OrderRemainingFillAmountZero);
- }
-
- if (orderRelevantState.makerBalance.eq(0)) {
- throw new Error(ExchangeContractErrs.InsufficientMakerBalance);
- }
- if (orderRelevantState.makerProxyAllowance.eq(0)) {
- throw new Error(ExchangeContractErrs.InsufficientMakerAllowance);
- }
- if (!signedOrder.makerFee.eq(0)) {
- if (orderRelevantState.makerFeeBalance.eq(0)) {
- throw new Error(ExchangeContractErrs.InsufficientMakerFeeBalance);
- }
- if (orderRelevantState.makerFeeProxyAllowance.eq(0)) {
- throw new Error(ExchangeContractErrs.InsufficientMakerFeeAllowance);
- }
- }
- const minFillableTakerTokenAmountWithinNoRoundingErrorRange = signedOrder.takerTokenAmount
- .dividedBy(ACCEPTABLE_RELATIVE_ROUNDING_ERROR)
- .dividedBy(signedOrder.makerTokenAmount);
- if (orderRelevantState.remainingFillableTakerTokenAmount
- .lessThan(minFillableTakerTokenAmountWithinNoRoundingErrorRange)) {
- throw new Error(ExchangeContractErrs.OrderFillRoundingError);
- }
- // TODO Add linear function solver when maker token is ZRX #badass
- // Return the max amount that's fillable
- }
}