aboutsummaryrefslogblamecommitdiffstats
path: root/packages/0x.js/src/stores/order_filled_cancelled_lazy_store.ts
blob: 9d74da096a69b96be4c6dcaf1411f70e99457ac8 (plain) (tree)
1
2
3
4
5
6
7
8



                                                                      
                                           

   
                                                        


                                            





                                       
                                            
                                 





                                                                                   
                                                        









                                                                                                           


                                                             


                                                                                      
                                                        









                                                                                                                


                                                                



                                       
 
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 = {};
    }
}