diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-09-15 20:08:19 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-09-15 20:14:55 +0800 |
commit | 4e59be9afce49fca8b396c305d5271c8299c85c1 (patch) | |
tree | 845ccbb9d0d68c59d45b7b05c32adae626757560 | |
parent | 190bf2599c1327fffd03d8a9d50bc7568190cf33 (diff) | |
download | dexon-sol-tools-4e59be9afce49fca8b396c305d5271c8299c85c1.tar.gz dexon-sol-tools-4e59be9afce49fca8b396c305d5271c8299c85c1.tar.zst dexon-sol-tools-4e59be9afce49fca8b396c305d5271c8299c85c1.zip |
Implement ProvidedOrderFetcher
3 files changed, 48 insertions, 1 deletions
diff --git a/packages/asset-buyer/src/order_fetchers/provided_order_fetcher.ts b/packages/asset-buyer/src/order_fetchers/provided_order_fetcher.ts new file mode 100644 index 000000000..29b03f1c4 --- /dev/null +++ b/packages/asset-buyer/src/order_fetchers/provided_order_fetcher.ts @@ -0,0 +1,41 @@ +import { schemas } from '@0xproject/json-schemas'; +import { SignedOrder } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { constants } from '../constants'; +import { + AssetBuyerError, + OrderFetcher, + OrderFetcherRequest, + OrderFetcherResponse, + SignedOrderWithRemainingFillableMakerAssetAmount, +} from '../types'; +import { assert } from '../utils/assert'; +import { orderUtils } from '../utils/order_utils'; + +export class ProvidedOrderFetcher implements OrderFetcher { + public readonly providedOrders: SignedOrder[]; + /** + * Instantiates a new ProvidedOrderFetcher instance + * @param providedOrders An array of objects that conform to SignedOrder to fetch from. + * @return An instance of ProvidedOrderFetcher + */ + constructor(providedOrders: SignedOrder[]) { + assert.doesConformToSchema('providedOrders', providedOrders, schemas.signedOrdersSchema); + this.providedOrders = providedOrders; + } + /** + * Given an object that conforms to OrderFetcherRequest, return the corresponding OrderFetcherResponse that satisfies the request. + * @param orderFetchRequest An instance of OrderFetcherRequest. See type for more information. + * @return An instance of OrderFetcherResponse. See type for more information. + */ + public async fetchOrdersAsync(orderFetchRequest: OrderFetcherRequest): Promise<OrderFetcherResponse> { + assert.isValidOrderFetcherRequest('orderFetchRequest', orderFetchRequest); + const { makerAssetData, takerAssetData } = orderFetchRequest; + const orders = _.filter(this.providedOrders, order => { + return order.makerAssetData === makerAssetData && order.takerAssetData === takerAssetData; + }); + return { orders }; + } +} diff --git a/packages/asset-buyer/src/order_fetchers/standard_relayer_api_order_fetcher.ts b/packages/asset-buyer/src/order_fetchers/standard_relayer_api_order_fetcher.ts index bf177b93b..5688daf69 100644 --- a/packages/asset-buyer/src/order_fetchers/standard_relayer_api_order_fetcher.ts +++ b/packages/asset-buyer/src/order_fetchers/standard_relayer_api_order_fetcher.ts @@ -60,6 +60,7 @@ export class StandardRelayerAPIOrderFetcher implements OrderFetcher { * @return An instance of OrderFetcherResponse. See type for more information. */ public async fetchOrdersAsync(orderFetchRequest: OrderFetcherRequest): Promise<OrderFetcherResponse> { + assert.isValidOrderFetcherRequest('orderFetchRequest', orderFetchRequest); const { makerAssetData, takerAssetData, networkId } = orderFetchRequest; const orderbookRequest = { baseAssetData: makerAssetData, quoteAssetData: takerAssetData }; const requestOpts = { networkId }; diff --git a/packages/asset-buyer/src/utils/assert.ts b/packages/asset-buyer/src/utils/assert.ts index c4d611477..0085ca41e 100644 --- a/packages/asset-buyer/src/utils/assert.ts +++ b/packages/asset-buyer/src/utils/assert.ts @@ -2,7 +2,7 @@ import { assert as sharedAssert } from '@0xproject/assert'; import { schemas } from '@0xproject/json-schemas'; import * as _ from 'lodash'; -import { BuyQuote, OrderFetcher } from '../types'; +import { BuyQuote, OrderFetcher, OrderFetcherRequest } from '../types'; export const assert = { ...sharedAssert, @@ -20,4 +20,9 @@ export const assert = { isValidOrderFetcher(variableName: string, orderFetcher: OrderFetcher): void { sharedAssert.isFunction(`${variableName}.fetchOrdersAsync`, orderFetcher.fetchOrdersAsync); }, + isValidOrderFetcherRequest(variableName: string, orderFetcherRequest: OrderFetcherRequest): void { + sharedAssert.isHexString(`${variableName}.makerAssetData`, orderFetcherRequest.makerAssetData); + sharedAssert.isHexString(`${variableName}.takerAssetData`, orderFetcherRequest.takerAssetData); + sharedAssert.isNumber(`${variableName}.networkId`, orderFetcherRequest.networkId); + }, }; |