From 209266dbed9d7d038c90c2da8d9b99acab77c80c Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Wed, 9 May 2018 20:36:28 +0200 Subject: Split 0x.js into contract-wrappers, order-watcher but keep 0x.js as a unifying library with the same interface --- .../stores/balance_proxy_allowance_lazy_store.ts | 91 ++++++++++++++++++++++ .../stores/order_filled_cancelled_lazy_store.ts | 73 +++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 packages/contract-wrappers/src/stores/balance_proxy_allowance_lazy_store.ts create mode 100644 packages/contract-wrappers/src/stores/order_filled_cancelled_lazy_store.ts (limited to 'packages/contract-wrappers/src/stores') 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 { + 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 { + 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 = {}; + } +} diff --git a/packages/contract-wrappers/src/stores/order_filled_cancelled_lazy_store.ts b/packages/contract-wrappers/src/stores/order_filled_cancelled_lazy_store.ts new file mode 100644 index 000000000..b554191a2 --- /dev/null +++ b/packages/contract-wrappers/src/stores/order_filled_cancelled_lazy_store.ts @@ -0,0 +1,73 @@ +import { AbstractOrderFilledCancelledFetcher } from '@0xproject/order-utils'; +import { BlockParamLiteral } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { ExchangeWrapper } from '../contract_wrappers/exchange_wrapper'; + +/** + * Copy on read store for filled/cancelled taker amounts + */ +export class OrderFilledCancelledLazyStore implements AbstractOrderFilledCancelledFetcher { + private _exchangeWrapper: ExchangeWrapper; + private _defaultBlock: BlockParamLiteral; + private _filledTakerAmount: { + [orderHash: string]: BigNumber; + }; + private _cancelledTakerAmount: { + [orderHash: string]: BigNumber; + }; + constructor(exchange: ExchangeWrapper, defaultBlock: BlockParamLiteral) { + this._exchangeWrapper = exchange; + this._defaultBlock = defaultBlock; + this._filledTakerAmount = {}; + this._cancelledTakerAmount = {}; + } + public async getFilledTakerAmountAsync(orderHash: string): Promise { + if (_.isUndefined(this._filledTakerAmount[orderHash])) { + const methodOpts = { + defaultBlock: this._defaultBlock, + }; + const filledTakerAmount = await this._exchangeWrapper.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 { + if (_.isUndefined(this._cancelledTakerAmount[orderHash])) { + const methodOpts = { + defaultBlock: this._defaultBlock, + }; + const cancelledTakerAmount = await this._exchangeWrapper.getCancelledTakerAmountAsync( + 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 = {}; + } + public async getUnavailableTakerAmountAsync(orderHash: string) { + return this._exchangeWrapper.getUnavailableTakerAmountAsync(orderHash); + } + public getZRXTokenAddress(): string { + return this._exchangeWrapper.getZRXTokenAddress(); + } +} -- cgit v1.2.3