diff options
author | Fabio Berger <me@fabioberger.com> | 2018-05-12 01:11:27 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-12 01:11:27 +0800 |
commit | 6aed4fb1ae27dabed027c855f2cbdc0bfb4f3b6b (patch) | |
tree | 8d374bf8f4f70a40d75c8bf4b35fe9a169250909 /packages/contract-wrappers/src/stores | |
parent | f42f608f3f97a5244f09f17ae5ee184c0f775de3 (diff) | |
parent | ad8e12eeaebd40fdafac7cbef96121d4454909df (diff) | |
download | dexon-sol-tools-6aed4fb1ae27dabed027c855f2cbdc0bfb4f3b6b.tar dexon-sol-tools-6aed4fb1ae27dabed027c855f2cbdc0bfb4f3b6b.tar.gz dexon-sol-tools-6aed4fb1ae27dabed027c855f2cbdc0bfb4f3b6b.tar.bz2 dexon-sol-tools-6aed4fb1ae27dabed027c855f2cbdc0bfb4f3b6b.tar.lz dexon-sol-tools-6aed4fb1ae27dabed027c855f2cbdc0bfb4f3b6b.tar.xz dexon-sol-tools-6aed4fb1ae27dabed027c855f2cbdc0bfb4f3b6b.tar.zst dexon-sol-tools-6aed4fb1ae27dabed027c855f2cbdc0bfb4f3b6b.zip |
Merge pull request #579 from 0xProject/breakUp0xjs
Split 0x.js into: contract-wrappers & order-watcher
Diffstat (limited to 'packages/contract-wrappers/src/stores')
-rw-r--r-- | packages/contract-wrappers/src/stores/balance_proxy_allowance_lazy_store.ts | 91 | ||||
-rw-r--r-- | packages/contract-wrappers/src/stores/order_filled_cancelled_lazy_store.ts | 75 |
2 files changed, 166 insertions, 0 deletions
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<BigNumber> { + 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<BigNumber> { + 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..3d96a95f3 --- /dev/null +++ b/packages/contract-wrappers/src/stores/order_filled_cancelled_lazy_store.ts @@ -0,0 +1,75 @@ +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<BigNumber> { + 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<BigNumber> { + 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): Promise<BigNumber> { + const unavailableTakerAmount = await this._exchangeWrapper.getUnavailableTakerAmountAsync(orderHash); + return unavailableTakerAmount; + } + public getZRXTokenAddress(): string { + const zrxToken = this._exchangeWrapper.getZRXTokenAddress(); + return zrxToken; + } +} |