diff options
author | Fabio Berger <me@fabioberger.com> | 2017-11-13 09:49:56 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-11-13 09:49:56 +0800 |
commit | a22661670f105a2bf527aca0e803689e0302ed17 (patch) | |
tree | 86042b06c407b388e39be690d9a40db218f82675 /src/stores | |
parent | 442f35a1fdd98846d3985548b3de6f5c620e68a1 (diff) | |
parent | 5aef16c2aacd279a8e688b4e735526bff7e4970f (diff) | |
download | dexon-sol-tools-a22661670f105a2bf527aca0e803689e0302ed17.tar dexon-sol-tools-a22661670f105a2bf527aca0e803689e0302ed17.tar.gz dexon-sol-tools-a22661670f105a2bf527aca0e803689e0302ed17.tar.bz2 dexon-sol-tools-a22661670f105a2bf527aca0e803689e0302ed17.tar.lz dexon-sol-tools-a22661670f105a2bf527aca0e803689e0302ed17.tar.xz dexon-sol-tools-a22661670f105a2bf527aca0e803689e0302ed17.tar.zst dexon-sol-tools-a22661670f105a2bf527aca0e803689e0302ed17.zip |
Merge branch 'orderWatcher' of github.com:0xProject/0x.js into orderWatcher
* 'orderWatcher' of github.com:0xProject/0x.js: (33 commits)
Remove old tests
Remove unused code
Fix tests
Remove redundant spaces
Don't store empty objects
Fix a typo
Remove duplicate operations
Remove redundant instance variables
Fix tests
Remove blockStore and default to numConfirmations === 0
Add a comment
Store number of confirmations in a blockStore
Remove tautology check
Pass blockStore to eventWatcher
Fix last merge conflicts
Clear cache on unsubscribe
Clear store cache on events
Add more configs for order watcher
Make subscribe function async and make blockStore operational
Adjust tests to new interface
...
Diffstat (limited to 'src/stores')
-rw-r--r-- | src/stores/balance_proxy_allowance_lazy_store.ts | 82 | ||||
-rw-r--r-- | src/stores/order_filled_cancelled_lazy_store.ts | 61 |
2 files changed, 143 insertions, 0 deletions
diff --git a/src/stores/balance_proxy_allowance_lazy_store.ts b/src/stores/balance_proxy_allowance_lazy_store.ts new file mode 100644 index 000000000..c83e61606 --- /dev/null +++ b/src/stores/balance_proxy_allowance_lazy_store.ts @@ -0,0 +1,82 @@ +import * as _ from 'lodash'; +import * as Web3 from 'web3'; +import {BigNumber} from 'bignumber.js'; +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 balance: { + [tokenAddress: string]: { + [userAddress: string]: BigNumber, + }, + }; + private proxyAllowance: { + [tokenAddress: string]: { + [userAddress: string]: BigNumber, + }, + }; + constructor(token: TokenWrapper) { + this.token = token; + 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: BlockParamLiteral.Pending, + }; + 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: BlockParamLiteral.Pending, + }; + 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 = {}; + } +} diff --git a/src/stores/order_filled_cancelled_lazy_store.ts b/src/stores/order_filled_cancelled_lazy_store.ts new file mode 100644 index 000000000..9d74da096 --- /dev/null +++ b/src/stores/order_filled_cancelled_lazy_store.ts @@ -0,0 +1,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 = {}; + } +} |