aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/balance_proxy_allowance_lazy_store.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-11-11 00:11:32 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-11-13 09:03:17 +0800
commitddbcf5f4706a9d75cd5fd4af508f3c07a78f2886 (patch)
tree0037f9569b45418c5632b057488a99df95af8dfe /src/stores/balance_proxy_allowance_lazy_store.ts
parent6becf22a2f752ef7c34ce1b423efd51773cc5fde (diff)
downloaddexon-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/stores/balance_proxy_allowance_lazy_store.ts')
-rw-r--r--src/stores/balance_proxy_allowance_lazy_store.ts54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/stores/balance_proxy_allowance_lazy_store.ts b/src/stores/balance_proxy_allowance_lazy_store.ts
new file mode 100644
index 000000000..e8178b9a9
--- /dev/null
+++ b/src/stores/balance_proxy_allowance_lazy_store.ts
@@ -0,0 +1,54 @@
+import * as _ from 'lodash';
+import {BigNumber} from 'bignumber.js';
+import {TokenWrapper} from '../contract_wrappers/token_wrapper';
+
+/**
+ * Copy on read store for balances/proxyAllowances of tokens/accounts
+ */
+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;
+ }
+}