diff options
Diffstat (limited to 'packages/order-utils')
5 files changed, 95 insertions, 1 deletions
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/asset_data_utils.ts b/packages/order-utils/src/asset_data_utils.ts index cb02d7d42..a9601e9ea 100644 --- a/packages/order-utils/src/asset_data_utils.ts +++ b/packages/order-utils/src/asset_data_utils.ts @@ -117,7 +117,7 @@ export const assetDataUtils = { * @param assetData Hex encoded assetData string to decode * @return Either a ERC20 or ERC721 assetData object */ - decodeAssetData(assetData: string): ERC20AssetData | ERC721AssetData { + decodeAssetDataOrThrow(assetData: string): ERC20AssetData | ERC721AssetData { const assetProxyId = assetDataUtils.decodeAssetProxyId(assetData); switch (assetProxyId) { case AssetProxyId.ERC20: diff --git a/packages/order-utils/src/index.ts b/packages/order-utils/src/index.ts index 8d748a2dc..76be63bb8 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 { assetDataUtils } from './asset_data_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 6d5a02adf..5a2c1d7ff 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 { assetDataUtils } from '../asset_data_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 = assetDataUtils.decodeAssetDataOrThrow(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..336c6d0ba --- /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 readonly _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; + } +} |