aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/order_filled_cancelled_lazy_store.ts
blob: 9d74da096a69b96be4c6dcaf1411f70e99457ac8 (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
import * as _ from 'lodash';
import * as Web3 from 'web3';
import {BigNumber} from 'bignumber.js';
import {ExchangeWrapper} from '../contract_wrappers/exchange_wrapper';
import {BlockParamLiteral} from '../types';

/**
 * Copy on read store for filled/cancelled taker amounts
 */
export class OrderFilledCancelledLazyStore {
    private exchange: ExchangeWrapper;
    private filledTakerAmount: {
        [orderHash: string]: BigNumber,
    };
    private cancelledTakerAmount: {
        [orderHash: string]: BigNumber,
    };
    constructor(exchange: ExchangeWrapper) {
        this.exchange = exchange;
        this.filledTakerAmount = {};
        this.cancelledTakerAmount = {};
    }
    public async getFilledTakerAmountAsync(orderHash: string): Promise<BigNumber> {
        if (_.isUndefined(this.filledTakerAmount[orderHash])) {
            const methodOpts = {
                defaultBlock: BlockParamLiteral.Pending,
            };
            const filledTakerAmount = await this.exchange.getFilledTakerAmountAsync(orderHash, methodOpts);
            this.setFilledTakerAmount(orderHash, filledTakerAmount);
        }
        const cachedFilled = this.filledTakerAmount[orderHash];
        return cachedFilled;
    }
    public setFilledTakerAmount(orderHash: string, filledTakerAmount: BigNumber): void {
        this.filledTakerAmount[orderHash] = filledTakerAmount;
    }
    public deleteFilledTakerAmount(orderHash: string): void {
        delete this.filledTakerAmount[orderHash];
    }
    public async getCancelledTakerAmountAsync(orderHash: string): Promise<BigNumber> {
        if (_.isUndefined(this.cancelledTakerAmount[orderHash])) {
            const methodOpts = {
                defaultBlock: BlockParamLiteral.Pending,
            };
            const cancelledTakerAmount = await this.exchange.getCanceledTakerAmountAsync(orderHash, methodOpts);
            this.setCancelledTakerAmount(orderHash, cancelledTakerAmount);
        }
        const cachedCancelled = this.cancelledTakerAmount[orderHash];
        return cachedCancelled;
    }
    public setCancelledTakerAmount(orderHash: string, cancelledTakerAmount: BigNumber): void {
        this.cancelledTakerAmount[orderHash] = cancelledTakerAmount;
    }
    public deleteCancelledTakerAmount(orderHash: string): void {
        delete this.cancelledTakerAmount[orderHash];
    }
    public deleteAll(): void {
        this.filledTakerAmount = {};
        this.cancelledTakerAmount = {};
    }
}