diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-11-11 00:11:32 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-11-13 09:03:17 +0800 |
commit | ddbcf5f4706a9d75cd5fd4af508f3c07a78f2886 (patch) | |
tree | 0037f9569b45418c5632b057488a99df95af8dfe /src/utils | |
parent | 6becf22a2f752ef7c34ce1b423efd51773cc5fde (diff) | |
download | dexon-sol-tools-ddbcf5f4706a9d75cd5fd4af508f3c07a78f2886.tar dexon-sol-tools-ddbcf5f4706a9d75cd5fd4af508f3c07a78f2886.tar.gz dexon-sol-tools-ddbcf5f4706a9d75cd5fd4af508f3c07a78f2886.tar.bz2 dexon-sol-tools-ddbcf5f4706a9d75cd5fd4af508f3c07a78f2886.tar.lz dexon-sol-tools-ddbcf5f4706a9d75cd5fd4af508f3c07a78f2886.tar.xz dexon-sol-tools-ddbcf5f4706a9d75cd5fd4af508f3c07a78f2886.tar.zst dexon-sol-tools-ddbcf5f4706a9d75cd5fd4af508f3c07a78f2886.zip |
Refactor out BalanceAndProxyAllowanceLazyStore
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/exchange_transfer_simulator.ts | 55 |
1 files changed, 2 insertions, 53 deletions
diff --git a/src/utils/exchange_transfer_simulator.ts b/src/utils/exchange_transfer_simulator.ts index 89b23c8ab..6812759fc 100644 --- a/src/utils/exchange_transfer_simulator.ts +++ b/src/utils/exchange_transfer_simulator.ts @@ -1,7 +1,7 @@ import * as _ from 'lodash'; import BigNumber from 'bignumber.js'; import {ExchangeContractErrs, TradeSide, TransferType} from '../types'; -import {TokenWrapper} from '../contract_wrappers/token_wrapper'; +import {BalanceAndProxyAllowanceLazyStore} from '../stores/balance_proxy_allowance_lazy_store'; enum FailureReason { Balance = 'balance', @@ -31,57 +31,6 @@ 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, - }, - }; - 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; - } - 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 @@ -110,7 +59,7 @@ 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)) { + if (!proxyAllowance.eq(this.token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS)) { this.setProxyAllowance(tokenAddress, userAddress, proxyAllowance.minus(amountInBaseUnits)); } } |