diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-11-11 00:15:08 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-11-13 09:03:18 +0800 |
commit | 742660591f66b1bccd194803a1bf1e43a60a3b31 (patch) | |
tree | 157e846b5cbde1a847f0d821408d39e3c2e44f73 | |
parent | ddbcf5f4706a9d75cd5fd4af508f3c07a78f2886 (diff) | |
download | dexon-sol-tools-742660591f66b1bccd194803a1bf1e43a60a3b31.tar dexon-sol-tools-742660591f66b1bccd194803a1bf1e43a60a3b31.tar.gz dexon-sol-tools-742660591f66b1bccd194803a1bf1e43a60a3b31.tar.bz2 dexon-sol-tools-742660591f66b1bccd194803a1bf1e43a60a3b31.tar.lz dexon-sol-tools-742660591f66b1bccd194803a1bf1e43a60a3b31.tar.xz dexon-sol-tools-742660591f66b1bccd194803a1bf1e43a60a3b31.tar.zst dexon-sol-tools-742660591f66b1bccd194803a1bf1e43a60a3b31.zip |
Make a store an instance variable of exchange transfer simulator and stop inheriting it
-rw-r--r-- | src/stores/balance_proxy_allowance_lazy_store.ts | 10 | ||||
-rw-r--r-- | src/utils/exchange_transfer_simulator.ts | 27 |
2 files changed, 22 insertions, 15 deletions
diff --git a/src/stores/balance_proxy_allowance_lazy_store.ts b/src/stores/balance_proxy_allowance_lazy_store.ts index e8178b9a9..7f392cb82 100644 --- a/src/stores/balance_proxy_allowance_lazy_store.ts +++ b/src/stores/balance_proxy_allowance_lazy_store.ts @@ -6,7 +6,7 @@ import {TokenWrapper} from '../contract_wrappers/token_wrapper'; * Copy on read store for balances/proxyAllowances of tokens/accounts */ export class BalanceAndProxyAllowanceLazyStore { - protected token: TokenWrapper; + private token: TokenWrapper; private balance: { [tokenAddress: string]: { [userAddress: string]: BigNumber, @@ -22,7 +22,7 @@ export class BalanceAndProxyAllowanceLazyStore { this.balance = {}; this.proxyAllowance = {}; } - protected async getBalanceAsync(tokenAddress: string, userAddress: string): Promise<BigNumber> { + public 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); @@ -30,13 +30,13 @@ export class BalanceAndProxyAllowanceLazyStore { const cachedBalance = this.balance[tokenAddress][userAddress]; return cachedBalance; } - protected setBalance(tokenAddress: string, userAddress: string, balance: BigNumber): void { + public 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> { + public 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); @@ -45,7 +45,7 @@ export class BalanceAndProxyAllowanceLazyStore { const cachedProxyAllowance = this.proxyAllowance[tokenAddress][userAddress]; return cachedProxyAllowance; } - protected setProxyAllowance(tokenAddress: string, userAddress: string, proxyAllowance: BigNumber): void { + public setProxyAllowance(tokenAddress: string, userAddress: string, proxyAllowance: BigNumber): void { if (_.isUndefined(this.proxyAllowance[tokenAddress])) { this.proxyAllowance[tokenAddress] = {}; } diff --git a/src/utils/exchange_transfer_simulator.ts b/src/utils/exchange_transfer_simulator.ts index 6812759fc..ecdec0be0 100644 --- a/src/utils/exchange_transfer_simulator.ts +++ b/src/utils/exchange_transfer_simulator.ts @@ -1,6 +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 { @@ -31,7 +32,13 @@ const ERR_MSG_MAPPING = { }, }; -export class ExchangeTransferSimulator extends BalanceAndProxyAllowanceLazyStore { +export class ExchangeTransferSimulator { + private store: BalanceAndProxyAllowanceLazyStore; + private UNLIMITED_ALLOWANCE_IN_BASE_UNITS: BigNumber; + constructor(token: TokenWrapper) { + this.store = new BalanceAndProxyAllowanceLazyStore(token); + this.UNLIMITED_ALLOWANCE_IN_BASE_UNITS = token.UNLIMITED_ALLOWANCE_IN_BASE_UNITS; + } /** * Simulates transferFrom call performed by a proxy * @param tokenAddress Address of the token to be transferred @@ -44,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); } @@ -58,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> { |