aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contract-wrappers/src/stores/balance_proxy_allowance_lazy_store.ts
diff options
context:
space:
mode:
authorAmir Bandeali <abandeali1@gmail.com>2018-05-16 03:52:49 +0800
committerAmir Bandeali <abandeali1@gmail.com>2018-05-16 03:52:49 +0800
commit9e0471bfbb4bb2b3b490e10ce34b16c88e8bab9a (patch)
treef72aae5170b6f1f6d3d70ebf6c03ed171680ff50 /packages/contract-wrappers/src/stores/balance_proxy_allowance_lazy_store.ts
parent9744b1906a111aa0c65c8fafb4db66aef32a5a23 (diff)
parent6aed4fb1ae27dabed027c855f2cbdc0bfb4f3b6b (diff)
downloaddexon-sol-tools-9e0471bfbb4bb2b3b490e10ce34b16c88e8bab9a.tar
dexon-sol-tools-9e0471bfbb4bb2b3b490e10ce34b16c88e8bab9a.tar.gz
dexon-sol-tools-9e0471bfbb4bb2b3b490e10ce34b16c88e8bab9a.tar.bz2
dexon-sol-tools-9e0471bfbb4bb2b3b490e10ce34b16c88e8bab9a.tar.lz
dexon-sol-tools-9e0471bfbb4bb2b3b490e10ce34b16c88e8bab9a.tar.xz
dexon-sol-tools-9e0471bfbb4bb2b3b490e10ce34b16c88e8bab9a.tar.zst
dexon-sol-tools-9e0471bfbb4bb2b3b490e10ce34b16c88e8bab9a.zip
Merge branch 'development' into v2-prototype
Diffstat (limited to 'packages/contract-wrappers/src/stores/balance_proxy_allowance_lazy_store.ts')
-rw-r--r--packages/contract-wrappers/src/stores/balance_proxy_allowance_lazy_store.ts91
1 files changed, 91 insertions, 0 deletions
diff --git a/packages/contract-wrappers/src/stores/balance_proxy_allowance_lazy_store.ts b/packages/contract-wrappers/src/stores/balance_proxy_allowance_lazy_store.ts
new file mode 100644
index 000000000..614195157
--- /dev/null
+++ b/packages/contract-wrappers/src/stores/balance_proxy_allowance_lazy_store.ts
@@ -0,0 +1,91 @@
+import { AbstractBalanceAndProxyAllowanceFetcher } from '@0xproject/order-utils';
+import { BlockParamLiteral } from '@0xproject/types';
+import { BigNumber } from '@0xproject/utils';
+import * as _ from 'lodash';
+
+import { TokenWrapper } from '../contract_wrappers/token_wrapper';
+
+/**
+ * Copy on read store for balances/proxyAllowances of tokens/accounts
+ */
+export class BalanceAndProxyAllowanceLazyStore implements AbstractBalanceAndProxyAllowanceFetcher {
+ private _tokenWrapper: TokenWrapper;
+ private _defaultBlock: BlockParamLiteral;
+ private _balance: {
+ [tokenAddress: string]: {
+ [userAddress: string]: BigNumber;
+ };
+ };
+ private _proxyAllowance: {
+ [tokenAddress: string]: {
+ [userAddress: string]: BigNumber;
+ };
+ };
+ constructor(token: TokenWrapper, defaultBlock: BlockParamLiteral) {
+ this._tokenWrapper = token;
+ this._defaultBlock = defaultBlock;
+ this._balance = {};
+ this._proxyAllowance = {};
+ }
+ public async getBalanceAsync(tokenAddress: string, userAddress: string): Promise<BigNumber> {
+ if (_.isUndefined(this._balance[tokenAddress]) || _.isUndefined(this._balance[tokenAddress][userAddress])) {
+ const methodOpts = {
+ defaultBlock: this._defaultBlock,
+ };
+ const balance = await this._tokenWrapper.getBalanceAsync(tokenAddress, userAddress, methodOpts);
+ this.setBalance(tokenAddress, userAddress, balance);
+ }
+ const cachedBalance = this._balance[tokenAddress][userAddress];
+ return cachedBalance;
+ }
+ public setBalance(tokenAddress: string, userAddress: string, balance: BigNumber): void {
+ if (_.isUndefined(this._balance[tokenAddress])) {
+ this._balance[tokenAddress] = {};
+ }
+ this._balance[tokenAddress][userAddress] = balance;
+ }
+ public deleteBalance(tokenAddress: string, userAddress: string): void {
+ if (!_.isUndefined(this._balance[tokenAddress])) {
+ delete this._balance[tokenAddress][userAddress];
+ if (_.isEmpty(this._balance[tokenAddress])) {
+ delete this._balance[tokenAddress];
+ }
+ }
+ }
+ public async getProxyAllowanceAsync(tokenAddress: string, userAddress: string): Promise<BigNumber> {
+ if (
+ _.isUndefined(this._proxyAllowance[tokenAddress]) ||
+ _.isUndefined(this._proxyAllowance[tokenAddress][userAddress])
+ ) {
+ const methodOpts = {
+ defaultBlock: this._defaultBlock,
+ };
+ const proxyAllowance = await this._tokenWrapper.getProxyAllowanceAsync(
+ tokenAddress,
+ userAddress,
+ methodOpts,
+ );
+ this.setProxyAllowance(tokenAddress, userAddress, proxyAllowance);
+ }
+ const cachedProxyAllowance = this._proxyAllowance[tokenAddress][userAddress];
+ return cachedProxyAllowance;
+ }
+ public setProxyAllowance(tokenAddress: string, userAddress: string, proxyAllowance: BigNumber): void {
+ if (_.isUndefined(this._proxyAllowance[tokenAddress])) {
+ this._proxyAllowance[tokenAddress] = {};
+ }
+ this._proxyAllowance[tokenAddress][userAddress] = proxyAllowance;
+ }
+ public deleteProxyAllowance(tokenAddress: string, userAddress: string): void {
+ if (!_.isUndefined(this._proxyAllowance[tokenAddress])) {
+ delete this._proxyAllowance[tokenAddress][userAddress];
+ if (_.isEmpty(this._proxyAllowance[tokenAddress])) {
+ delete this._proxyAllowance[tokenAddress];
+ }
+ }
+ }
+ public deleteAll(): void {
+ this._balance = {};
+ this._proxyAllowance = {};
+ }
+}