diff options
author | Fabio Berger <me@fabioberger.com> | 2018-06-12 01:21:32 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2018-06-12 01:21:32 +0800 |
commit | 21f7722f1023cc9d1848737b7c986f7df7a07122 (patch) | |
tree | a4b9c57fa75fb8c92bf44ed968df8b7112f478de /packages/order-utils/src/store | |
parent | e4afe603f9c4dd5128f2446f9fe075516a3d4894 (diff) | |
download | dexon-sol-tools-21f7722f1023cc9d1848737b7c986f7df7a07122.tar dexon-sol-tools-21f7722f1023cc9d1848737b7c986f7df7a07122.tar.gz dexon-sol-tools-21f7722f1023cc9d1848737b7c986f7df7a07122.tar.bz2 dexon-sol-tools-21f7722f1023cc9d1848737b7c986f7df7a07122.tar.lz dexon-sol-tools-21f7722f1023cc9d1848737b7c986f7df7a07122.tar.xz dexon-sol-tools-21f7722f1023cc9d1848737b7c986f7df7a07122.tar.zst dexon-sol-tools-21f7722f1023cc9d1848737b7c986f7df7a07122.zip |
Move OrderValidationUtils (+ tests) and ExchangeTransferSimulator to order-utils
Diffstat (limited to 'packages/order-utils/src/store')
-rw-r--r-- | packages/order-utils/src/store/balance_and_proxy_allowance_lazy_store.ts | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/packages/order-utils/src/store/balance_and_proxy_allowance_lazy_store.ts b/packages/order-utils/src/store/balance_and_proxy_allowance_lazy_store.ts new file mode 100644 index 000000000..b5bd2801a --- /dev/null +++ b/packages/order-utils/src/store/balance_and_proxy_allowance_lazy_store.ts @@ -0,0 +1,82 @@ +import { BigNumber } from '@0xproject/utils'; +import { BlockParamLiteral } from 'ethereum-types'; +import * as _ from 'lodash'; + +import { AbstractBalanceAndProxyAllowanceFetcher } from '../abstract/abstract_balance_and_proxy_allowance_fetcher'; +import { AbstractBalanceAndProxyAllowanceLazyStore } from '../abstract/abstract_balance_and_proxy_allowance_lazy_store'; + +/** + * Copy on read store for balances/proxyAllowances of tokens/accounts + */ +export class BalanceAndProxyAllowanceLazyStore implements AbstractBalanceAndProxyAllowanceLazyStore { + private _balanceAndProxyAllowanceFetcher: AbstractBalanceAndProxyAllowanceFetcher; + private _balance: { + [assetData: string]: { + [userAddress: string]: BigNumber; + }; + }; + private _proxyAllowance: { + [assetData: string]: { + [userAddress: string]: BigNumber; + }; + }; + constructor(token: AbstractBalanceAndProxyAllowanceFetcher) { + this._balanceAndProxyAllowanceFetcher = token; + this._balance = {}; + this._proxyAllowance = {}; + } + public async getBalanceAsync(assetData: string, userAddress: string): Promise<BigNumber> { + if (_.isUndefined(this._balance[assetData]) || _.isUndefined(this._balance[assetData][userAddress])) { + const balance = await this._balanceAndProxyAllowanceFetcher.getBalanceAsync(assetData, userAddress); + this.setBalance(assetData, userAddress, balance); + } + const cachedBalance = this._balance[assetData][userAddress]; + return cachedBalance; + } + public setBalance(assetData: string, userAddress: string, balance: BigNumber): void { + if (_.isUndefined(this._balance[assetData])) { + this._balance[assetData] = {}; + } + this._balance[assetData][userAddress] = balance; + } + public deleteBalance(assetData: string, userAddress: string): void { + if (!_.isUndefined(this._balance[assetData])) { + delete this._balance[assetData][userAddress]; + if (_.isEmpty(this._balance[assetData])) { + delete this._balance[assetData]; + } + } + } + public async getProxyAllowanceAsync(assetData: string, userAddress: string): Promise<BigNumber> { + if ( + _.isUndefined(this._proxyAllowance[assetData]) || + _.isUndefined(this._proxyAllowance[assetData][userAddress]) + ) { + const proxyAllowance = await this._balanceAndProxyAllowanceFetcher.getProxyAllowanceAsync( + assetData, + userAddress, + ); + this.setProxyAllowance(assetData, userAddress, proxyAllowance); + } + const cachedProxyAllowance = this._proxyAllowance[assetData][userAddress]; + return cachedProxyAllowance; + } + public setProxyAllowance(assetData: string, userAddress: string, proxyAllowance: BigNumber): void { + if (_.isUndefined(this._proxyAllowance[assetData])) { + this._proxyAllowance[assetData] = {}; + } + this._proxyAllowance[assetData][userAddress] = proxyAllowance; + } + public deleteProxyAllowance(assetData: string, userAddress: string): void { + if (!_.isUndefined(this._proxyAllowance[assetData])) { + delete this._proxyAllowance[assetData][userAddress]; + if (_.isEmpty(this._proxyAllowance[assetData])) { + delete this._proxyAllowance[assetData]; + } + } + } + public deleteAll(): void { + this._balance = {}; + this._proxyAllowance = {}; + } +} |