aboutsummaryrefslogtreecommitdiffstats
path: root/packages/0x.js/src/stores/balance_proxy_allowance_lazy_store.ts
blob: 379afa4bc90aa0e111779c0e97d9d1f35d41d5e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';

import { TokenWrapper } from '../contract_wrappers/token_wrapper';
import { BlockParamLiteral } from '../types';

/**
 * Copy on read store for balances/proxyAllowances of tokens/accounts
 */
export class BalanceAndProxyAllowanceLazyStore {
    private _token: TokenWrapper;
    private _defaultBlock: BlockParamLiteral;
    private _balance: {
        [tokenAddress: string]: {
            [userAddress: string]: BigNumber;
        };
    };
    private _proxyAllowance: {
        [tokenAddress: string]: {
            [userAddress: string]: BigNumber;
        };
    };
    constructor(token: TokenWrapper, defaultBlock: BlockParamLiteral) {
        this._token = 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._token.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._token.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 = {};
    }
}