aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/exchange_transfer_simulator.ts
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/exchange_transfer_simulator.ts
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/exchange_transfer_simulator.ts')
-rw-r--r--src/utils/exchange_transfer_simulator.ts76
1 files changed, 16 insertions, 60 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> {