diff options
Diffstat (limited to 'packages/order-utils')
5 files changed, 95 insertions, 1 deletions
diff --git a/packages/order-utils/package.json b/packages/order-utils/package.json index 8f5041609..20f050cb4 100644 --- a/packages/order-utils/package.json +++ b/packages/order-utils/package.json @@ -67,7 +67,7 @@ "npm-run-all": "^4.1.2", "shx": "^0.2.2", "sinon": "^4.0.0", - "tslint": "5.8.0", + "tslint": "5.10.0", "typedoc": "0xProject/typedoc", "typescript": "2.7.1" }, diff --git a/packages/order-utils/src/abstract/abstract_order_filled_cancelled_lazy_store.ts b/packages/order-utils/src/abstract/abstract_order_filled_cancelled_lazy_store.ts new file mode 100644 index 000000000..617bcb224 --- /dev/null +++ b/packages/order-utils/src/abstract/abstract_order_filled_cancelled_lazy_store.ts @@ -0,0 +1,12 @@ +import { BigNumber } from '@0xproject/utils'; + +export abstract class AbstractOrderFilledCancelledLazyStore { + public abstract async getFilledTakerAmountAsync(orderHash: string): Promise<BigNumber>; + public abstract async getIsCancelledAsync(orderHash: string): Promise<boolean>; + public abstract setFilledTakerAmount(orderHash: string, balance: BigNumber): void; + public abstract deleteFilledTakerAmount(orderHash: string): void; + public abstract setIsCancelled(orderHash: string, isCancelled: boolean): void; + public abstract deleteIsCancelled(orderHash: string): void; + public abstract deleteAll(): void; + public abstract getZRXAssetData(): string; +} diff --git a/packages/order-utils/src/index.ts b/packages/order-utils/src/index.ts index b4a7a6b67..2322409a4 100644 --- a/packages/order-utils/src/index.ts +++ b/packages/order-utils/src/index.ts @@ -17,6 +17,7 @@ export { OrderError, MessagePrefixType, MessagePrefixOpts, EIP712Parameter, EIP7 export { AbstractBalanceAndProxyAllowanceFetcher } from './abstract/abstract_balance_and_proxy_allowance_fetcher'; export { AbstractOrderFilledCancelledFetcher } from './abstract/abstract_order_filled_cancelled_fetcher'; export { BalanceAndProxyAllowanceLazyStore } from './store/balance_and_proxy_allowance_lazy_store'; +export { OrderFilledCancelledLazyStore } from './store/order_filled_cancelled_lazy_store'; export { RemainingFillableCalculator } from './remaining_fillable_calculator'; export { OrderStateUtils } from './order_state_utils'; export { assetProxyUtils } from './asset_proxy_utils'; diff --git a/packages/order-utils/src/store/balance_and_proxy_allowance_lazy_store.ts b/packages/order-utils/src/store/balance_and_proxy_allowance_lazy_store.ts index e7352119d..8235725ed 100644 --- a/packages/order-utils/src/store/balance_and_proxy_allowance_lazy_store.ts +++ b/packages/order-utils/src/store/balance_and_proxy_allowance_lazy_store.ts @@ -1,8 +1,10 @@ +import { AssetProxyId } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; import { AbstractBalanceAndProxyAllowanceFetcher } from '../abstract/abstract_balance_and_proxy_allowance_fetcher'; import { AbstractBalanceAndProxyAllowanceLazyStore } from '../abstract/abstract_balance_and_proxy_allowance_lazy_store'; +import { assetProxyUtils } from '../asset_proxy_utils'; /** * Copy on read store for balances/proxyAllowances of tokens/accounts @@ -74,6 +76,20 @@ export class BalanceAndProxyAllowanceLazyStore implements AbstractBalanceAndProx } } } + public deleteAllERC721ProxyAllowance(tokenAddress: string, userAddress: string): void { + for (const assetData in this._proxyAllowance) { + if (this._proxyAllowance.hasOwnProperty(assetData)) { + const decodedAssetData = assetProxyUtils.decodeAssetData(assetData); + if ( + decodedAssetData.assetProxyId === AssetProxyId.ERC721 && + decodedAssetData.tokenAddress === tokenAddress && + !_.isUndefined(this._proxyAllowance[assetData][userAddress]) + ) { + delete this._proxyAllowance[assetData][userAddress]; + } + } + } + } public deleteAll(): void { this._balance = {}; this._proxyAllowance = {}; diff --git a/packages/order-utils/src/store/order_filled_cancelled_lazy_store.ts b/packages/order-utils/src/store/order_filled_cancelled_lazy_store.ts new file mode 100644 index 000000000..4b1ad70b6 --- /dev/null +++ b/packages/order-utils/src/store/order_filled_cancelled_lazy_store.ts @@ -0,0 +1,65 @@ +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { AbstractOrderFilledCancelledFetcher } from '../abstract/abstract_order_filled_cancelled_fetcher'; +import { AbstractOrderFilledCancelledLazyStore } from '../abstract/abstract_order_filled_cancelled_lazy_store'; + +/** + * Copy on read store for balances/proxyAllowances of tokens/accounts + */ +export class OrderFilledCancelledLazyStore implements AbstractOrderFilledCancelledLazyStore { + private _orderFilledCancelledFetcher: AbstractOrderFilledCancelledFetcher; + private _filledTakerAmount: { + [orderHash: string]: BigNumber; + }; + private _isCancelled: { + [orderHash: string]: boolean; + }; + constructor(orderFilledCancelledFetcher: AbstractOrderFilledCancelledFetcher) { + this._orderFilledCancelledFetcher = orderFilledCancelledFetcher; + this._filledTakerAmount = {}; + this._isCancelled = {}; + } + public async getFilledTakerAmountAsync(orderHash: string): Promise<BigNumber> { + if (_.isUndefined(this._filledTakerAmount[orderHash])) { + const filledTakerAmount = await this._orderFilledCancelledFetcher.getFilledTakerAmountAsync(orderHash); + this.setFilledTakerAmount(orderHash, filledTakerAmount); + } + const cachedFilledTakerAmount = this._filledTakerAmount[orderHash]; + return cachedFilledTakerAmount; + } + public setFilledTakerAmount(orderHash: string, filledTakerAmount: BigNumber): void { + this._filledTakerAmount[orderHash] = filledTakerAmount; + } + public deleteFilledTakerAmount(orderHash: string): void { + delete this._filledTakerAmount[orderHash]; + } + public async getIsCancelledAsync(orderHash: string): Promise<boolean> { + if (_.isUndefined(this._isCancelled[orderHash])) { + const isCancelled = await this._orderFilledCancelledFetcher.isOrderCancelledAsync(orderHash); + this.setIsCancelled(orderHash, isCancelled); + } + const cachedIsCancelled = this._isCancelled[orderHash]; // tslint:disable-line:boolean-naming + return cachedIsCancelled; + } + public setIsCancelled(orderHash: string, isCancelled: boolean): void { + this._isCancelled[orderHash] = isCancelled; + } + public deleteIsCancelled(orderHash: string): void { + delete this._isCancelled[orderHash]; + } + public deleteAll(): void { + this.deleteAllFilled(); + this.deleteAllIsCancelled(); + } + public deleteAllIsCancelled(): void { + this._isCancelled = {}; + } + public deleteAllFilled(): void { + this._filledTakerAmount = {}; + } + public getZRXAssetData(): string { + const zrxAssetData = this._orderFilledCancelledFetcher.getZRXAssetData(); + return zrxAssetData; + } +} |