aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorFabio Berger <me@fabioberger.com>2017-11-13 09:26:21 +0800
committerGitHub <noreply@github.com>2017-11-13 09:26:21 +0800
commit5aef16c2aacd279a8e688b4e735526bff7e4970f (patch)
treea85a712d3a478199a09ac6aebac6e43d2e3f3c3a /src/utils
parent6becf22a2f752ef7c34ce1b423efd51773cc5fde (diff)
parente512e38efbbec030add83ce2bc50f0d17862bdd6 (diff)
downloaddexon-sol-tools-5aef16c2aacd279a8e688b4e735526bff7e4970f.tar
dexon-sol-tools-5aef16c2aacd279a8e688b4e735526bff7e4970f.tar.gz
dexon-sol-tools-5aef16c2aacd279a8e688b4e735526bff7e4970f.tar.bz2
dexon-sol-tools-5aef16c2aacd279a8e688b4e735526bff7e4970f.tar.lz
dexon-sol-tools-5aef16c2aacd279a8e688b4e735526bff7e4970f.tar.xz
dexon-sol-tools-5aef16c2aacd279a8e688b4e735526bff7e4970f.tar.zst
dexon-sol-tools-5aef16c2aacd279a8e688b4e735526bff7e4970f.zip
Merge pull request #211 from 0xProject/feature/orderWatcherLocalStateStore
Order watcher local state store
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/exchange_transfer_simulator.ts76
-rw-r--r--src/utils/order_state_utils.ts53
2 files changed, 47 insertions, 82 deletions
diff --git a/src/utils/exchange_transfer_simulator.ts b/src/utils/exchange_transfer_simulator.ts
index 89b23c8ab..308ef06db 100644
--- a/src/utils/exchange_transfer_simulator.ts
+++ b/src/utils/exchange_transfer_simulator.ts
@@ -1,7 +1,8 @@
import * as _ from 'lodash';
import BigNumber from 'bignumber.js';
-import {ExchangeContractErrs, TradeSide, TransferType} from '../types';
+import {ExchangeContractErrs, TradeSide, TransferType, BlockParamLiteral} from '../types';
import {TokenWrapper} from '../contract_wrappers/token_wrapper';
+import {BalanceAndProxyAllowanceLazyStore} from '../stores/balance_proxy_allowance_lazy_store';
enum FailureReason {
Balance = 'balance',
@@ -31,58 +32,13 @@ const ERR_MSG_MAPPING = {
},
};
-/**
- * Copy on read store for balances/proxyAllowances of tokens/accounts touched in trades
- */
-export class BalanceAndProxyAllowanceLazyStore {
- protected _token: TokenWrapper;
- private _balance: {
- [tokenAddress: string]: {
- [userAddress: string]: BigNumber,
- },
- };
- private _proxyAllowance: {
- [tokenAddress: string]: {
- [userAddress: string]: BigNumber,
- },
- };
+export class ExchangeTransferSimulator {
+ private store: BalanceAndProxyAllowanceLazyStore;
+ private UNLIMITED_ALLOWANCE_IN_BASE_UNITS: BigNumber;
constructor(token: TokenWrapper) {
- this._token = token;
- this._balance = {};
- this._proxyAllowance = {};
- }
- protected async getBalanceAsync(tokenAddress: string, userAddress: string): Promise<BigNumber> {
- if (_.isUndefined(this._balance[tokenAddress]) || _.isUndefined(this._balance[tokenAddress][userAddress])) {
- const balance = await this._token.getBalanceAsync(tokenAddress, userAddress);
- this.setBalance(tokenAddress, userAddress, balance);
- }
- const cachedBalance = this._balance[tokenAddress][userAddress];
- return cachedBalance;
- }
- protected setBalance(tokenAddress: string, userAddress: string, balance: BigNumber): void {
- if (_.isUndefined(this._balance[tokenAddress])) {
- this._balance[tokenAddress] = {};
- }
- this._balance[tokenAddress][userAddress] = balance;
+ this.store = new BalanceAndProxyAllowanceLazyStore(token);
+ this.UNLIMITED_ALLOWANCE_IN_BASE_UNITS = token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS;
}
- protected async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise<BigNumber> {
- if (_.isUndefined(this._proxyAllowance[tokenAddress]) ||
- _.isUndefined(this._proxyAllowance[tokenAddress][userAddress])) {
- const proxyAllowance = await this._token.getProxyAllowanceAsync(tokenAddress, userAddress);
- this.setProxyAllowance(tokenAddress, userAddress, proxyAllowance);
- }
- const cachedProxyAllowance = this._proxyAllowance[tokenAddress][userAddress];
- return cachedProxyAllowance;
- }
- protected setProxyAllowance(tokenAddress: string, userAddress: string, proxyAllowance: BigNumber): void {
- if (_.isUndefined(this._proxyAllowance[tokenAddress])) {
- this._proxyAllowance[tokenAddress] = {};
- }
- this._proxyAllowance[tokenAddress][userAddress] = proxyAllowance;
- }
-}
-
-export class ExchangeTransferSimulator extends BalanceAndProxyAllowanceLazyStore {
/**
* Simulates transferFrom call performed by a proxy
* @param tokenAddress Address of the token to be transferred
@@ -95,8 +51,8 @@ export class ExchangeTransferSimulator extends BalanceAndProxyAllowanceLazyStore
public async transferFromAsync(tokenAddress: string, from: string, to: string,
amountInBaseUnits: BigNumber, tradeSide: TradeSide,
transferType: TransferType): Promise<void> {
- const balance = await this.getBalanceAsync(tokenAddress, from);
- const proxyAllowance = await this.getProxyAllowanceAsync(tokenAddress, from);
+ const balance = await this.store.getBalanceAsync(tokenAddress, from);
+ const proxyAllowance = await this.store.getProxyAllowanceAsync(tokenAddress, from);
if (proxyAllowance.lessThan(amountInBaseUnits)) {
this.throwValidationError(FailureReason.ProxyAllowance, tradeSide, transferType);
}
@@ -109,20 +65,20 @@ export class ExchangeTransferSimulator extends BalanceAndProxyAllowanceLazyStore
}
private async decreaseProxyAllowanceAsync(tokenAddress: string, userAddress: string,
amountInBaseUnits: BigNumber): Promise<void> {
- const proxyAllowance = await this.getProxyAllowanceAsync(tokenAddress, userAddress);
- if (!proxyAllowance.eq(this._token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS)) {
- this.setProxyAllowance(tokenAddress, userAddress, proxyAllowance.minus(amountInBaseUnits));
+ const proxyAllowance = await this.store.getProxyAllowanceAsync(tokenAddress, userAddress);
+ if (!proxyAllowance.eq(this.UNLIMITED_ALLOWANCE_IN_BASE_UNITS)) {
+ this.store.setProxyAllowance(tokenAddress, userAddress, proxyAllowance.minus(amountInBaseUnits));
}
}
private async increaseBalanceAsync(tokenAddress: string, userAddress: string,
amountInBaseUnits: BigNumber): Promise<void> {
- const balance = await this.getBalanceAsync(tokenAddress, userAddress);
- this.setBalance(tokenAddress, userAddress, balance.plus(amountInBaseUnits));
+ const balance = await this.store.getBalanceAsync(tokenAddress, userAddress);
+ this.store.setBalance(tokenAddress, userAddress, balance.plus(amountInBaseUnits));
}
private async decreaseBalanceAsync(tokenAddress: string, userAddress: string,
amountInBaseUnits: BigNumber): Promise<void> {
- const balance = await this.getBalanceAsync(tokenAddress, userAddress);
- this.setBalance(tokenAddress, userAddress, balance.minus(amountInBaseUnits));
+ const balance = await this.store.getBalanceAsync(tokenAddress, userAddress);
+ this.store.setBalance(tokenAddress, userAddress, balance.minus(amountInBaseUnits));
}
private throwValidationError(failureReason: FailureReason, tradeSide: TradeSide,
transferType: TransferType): Promise<never> {
diff --git a/src/utils/order_state_utils.ts b/src/utils/order_state_utils.ts
index 36a4b68d6..f82601cae 100644
--- a/src/utils/order_state_utils.ts
+++ b/src/utils/order_state_utils.ts
@@ -1,4 +1,5 @@
import * as _ from 'lodash';
+import * as Web3 from 'web3';
import BigNumber from 'bignumber.js';
import {
ExchangeContractErrs,
@@ -14,16 +15,19 @@ 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';
export class OrderStateUtils {
- private tokenWrapper: TokenWrapper;
- private exchangeWrapper: ExchangeWrapper;
- constructor(tokenWrapper: TokenWrapper, exchangeWrapper: ExchangeWrapper) {
- this.tokenWrapper = tokenWrapper;
- this.exchangeWrapper = exchangeWrapper;
+ private balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore;
+ private orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore;
+ constructor(balanceAndProxyAllowanceLazyStore: BalanceAndProxyAllowanceLazyStore,
+ orderFilledCancelledLazyStore: OrderFilledCancelledLazyStore) {
+ this.balanceAndProxyAllowanceLazyStore = balanceAndProxyAllowanceLazyStore;
+ this.orderFilledCancelledLazyStore = orderFilledCancelledLazyStore;
}
- public async getOrderStateAsync(signedOrder: SignedOrder, methodOpts?: MethodOpts): Promise<OrderState> {
- const orderRelevantState = await this.getOrderRelevantStateAsync(signedOrder, methodOpts);
+ public async getOrderStateAsync(signedOrder: SignedOrder): Promise<OrderState> {
+ const orderRelevantState = await this.getOrderRelevantStateAsync(signedOrder);
const orderHash = ZeroEx.getOrderHashHex(signedOrder);
try {
this.validateIfOrderIsValid(signedOrder, orderRelevantState);
@@ -42,26 +46,31 @@ export class OrderStateUtils {
return orderState;
}
}
- public async getOrderRelevantStateAsync(
- signedOrder: SignedOrder, methodOpts?: MethodOpts): Promise<OrderRelevantState> {
- const zrxTokenAddress = await this.exchangeWrapper.getZRXTokenAddressAsync();
+ public async getOrderRelevantStateAsync(signedOrder: SignedOrder): Promise<OrderRelevantState> {
+ // HACK: We access the private property here but otherwise the interface will be less nice.
+ // If we pass it from the instantiator - there is no opportunity to get it there
+ // 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 orderHash = ZeroEx.getOrderHashHex(signedOrder);
- const makerBalance = await this.tokenWrapper.getBalanceAsync(
- signedOrder.makerTokenAddress, signedOrder.maker, methodOpts,
+ const makerBalance = await this.balanceAndProxyAllowanceLazyStore.getBalanceAsync(
+ signedOrder.makerTokenAddress, signedOrder.maker,
);
- const makerProxyAllowance = await this.tokenWrapper.getProxyAllowanceAsync(
- signedOrder.makerTokenAddress, signedOrder.maker, methodOpts,
+ const makerProxyAllowance = await this.balanceAndProxyAllowanceLazyStore.getProxyAllowanceAsync(
+ signedOrder.makerTokenAddress, signedOrder.maker,
);
- const makerFeeBalance = await this.tokenWrapper.getBalanceAsync(
- zrxTokenAddress, signedOrder.maker, methodOpts,
+ const makerFeeBalance = await this.balanceAndProxyAllowanceLazyStore.getBalanceAsync(
+ zrxTokenAddress, signedOrder.maker,
);
- const makerFeeProxyAllowance = await this.tokenWrapper.getProxyAllowanceAsync(
- zrxTokenAddress, signedOrder.maker, methodOpts,
+ const makerFeeProxyAllowance = await this.balanceAndProxyAllowanceLazyStore.getProxyAllowanceAsync(
+ zrxTokenAddress, signedOrder.maker,
);
- const filledTakerTokenAmount = await this.exchangeWrapper.getFilledTakerAmountAsync(orderHash, methodOpts);
- const canceledTakerTokenAmount = await this.exchangeWrapper.getCanceledTakerAmountAsync(orderHash, methodOpts);
- const unavailableTakerTokenAmount =
- await this.exchangeWrapper.getUnavailableTakerAmountAsync(orderHash, methodOpts);
+ const filledTakerTokenAmount = await this.orderFilledCancelledLazyStore.getFilledTakerAmountAsync(orderHash);
+ const canceledTakerTokenAmount = await this.orderFilledCancelledLazyStore.getCancelledTakerAmountAsync(
+ orderHash,
+ );
+ const unavailableTakerTokenAmount = await exchange.getUnavailableTakerAmountAsync(orderHash);
const totalMakerTokenAmount = signedOrder.makerTokenAmount;
const totalTakerTokenAmount = signedOrder.takerTokenAmount;
const remainingTakerTokenAmount = totalTakerTokenAmount.minus(unavailableTakerTokenAmount);