From 03e18ff7c60749fd33228b25077891da26e81d41 Mon Sep 17 00:00:00 2001 From: fragosti Date: Thu, 20 Sep 2018 15:10:15 +0200 Subject: Rename OrderFetcher to OrderProvider and other small improvements --- packages/asset-buyer/src/asset_buyer.ts | 28 ++++++++++------------ packages/asset-buyer/src/index.ts | 6 ++--- .../src/order_fetchers/provided_order_fetcher.ts | 6 ++--- .../standard_relayer_api_order_fetcher.ts | 10 ++++---- .../standard_relayer_api_asset_buyer_manager.ts | 21 ++++++++-------- packages/asset-buyer/src/types.ts | 8 +++---- packages/asset-buyer/src/utils/assert.ts | 8 +++---- packages/asset-buyer/src/utils/asset_data_utils.ts | 4 ++-- .../src/utils/order_fetcher_response_processor.ts | 6 ++--- 9 files changed, 48 insertions(+), 49 deletions(-) (limited to 'packages/asset-buyer') diff --git a/packages/asset-buyer/src/asset_buyer.ts b/packages/asset-buyer/src/asset_buyer.ts index 2b25c681c..e74b8eac1 100644 --- a/packages/asset-buyer/src/asset_buyer.ts +++ b/packages/asset-buyer/src/asset_buyer.ts @@ -14,8 +14,8 @@ import { AssetBuyerOrdersAndFillableAmounts, BuyQuote, BuyQuoteRequestOpts, - OrderFetcher, - OrderFetcherResponse, + OrderProvider, + OrderProviderResponse, } from './types'; import { assert } from './utils/assert'; @@ -26,7 +26,7 @@ import { orderFetcherResponseProcessor } from './utils/order_fetcher_response_pr export class AssetBuyer { public readonly provider: Provider; public readonly assetData: string; - public readonly orderFetcher: OrderFetcher; + public readonly orderFetcher: OrderProvider; public readonly networkId: number; public readonly orderRefreshIntervalMs: number; private readonly _contractWrappers: ContractWrappers; @@ -133,7 +133,7 @@ export class AssetBuyer { constructor( provider: Provider, assetData: string, - orderFetcher: OrderFetcher, + orderFetcher: OrderProvider, networkId: number = constants.MAINNET_NETWORK_ID, orderRefreshIntervalMs: number = constants.DEFAULT_ORDER_REFRESH_INTERVAL_MS, ) { @@ -152,8 +152,8 @@ export class AssetBuyer { }); } /** - * Get a BuyQuote containing all information relevant to fulfilling a buy. - * Pass the BuyQuote to executeBuyQuoteAsync to execute the buy. + * Get a `BuyQuote` containing all information relevant to fulfilling a buy. + * You can then pass the `BuyQuote` to `executeBuyQuoteAsync` to execute the buy. * @param assetBuyAmount The amount of asset to buy. * @param feePercentage The affiliate fee percentage. Defaults to 0. * @param forceOrderRefresh If set to true, new orders and state will be fetched instead of waiting for @@ -251,10 +251,8 @@ export class AssetBuyer { * Ask the order fetcher for orders and process them. */ private async _getLatestOrdersAndFillableAmountsAsync(): Promise { - // find ether token asset data - const etherTokenAssetData = this._getEtherTokenAssetData(); - // find zrx token asset data - const zrxTokenAssetData = this._getZrxTokenAssetData(); + const etherTokenAssetData = this._getEtherTokenAssetDataOrThrow(); + const zrxTokenAssetData = this._getZrxTokenAssetDataOrThrow(); // construct order fetcher requests const targetOrderFetcherRequest = { makerAssetData: this.assetData, @@ -269,7 +267,7 @@ export class AssetBuyer { const requests = [targetOrderFetcherRequest, feeOrderFetcherRequest]; // fetch orders and possible fillable amounts const [targetOrderFetcherResponse, feeOrderFetcherResponse] = await Promise.all( - _.map(requests, async request => this.orderFetcher.fetchOrdersAsync(request)), + _.map(requests, async request => this.orderFetcher.getOrdersAsync(request)), ); // process the responses into one object const ordersAndFillableAmounts = await orderFetcherResponseProcessor.processAsync( @@ -284,14 +282,14 @@ export class AssetBuyer { * Get the assetData that represents the WETH token. * Will throw if WETH does not exist for the current network. */ - private _getEtherTokenAssetData(): string { - return assetDataUtils.getEtherTokenAssetData(this._contractWrappers); + private _getEtherTokenAssetDataOrThrow(): string { + return assetDataUtils.getEtherTokenAssetDataOrThrow(this._contractWrappers); } /** * Get the assetData that represents the ZRX token. * Will throw if ZRX does not exist for the current network. */ - private _getZrxTokenAssetData(): string { - return assetDataUtils.getZrxTokenAssetData(this._contractWrappers); + private _getZrxTokenAssetDataOrThrow(): string { + return assetDataUtils.getZrxTokenAssetDataOrThrow(this._contractWrappers); } } diff --git a/packages/asset-buyer/src/index.ts b/packages/asset-buyer/src/index.ts index 2156b7e96..aa10c3af2 100644 --- a/packages/asset-buyer/src/index.ts +++ b/packages/asset-buyer/src/index.ts @@ -9,9 +9,9 @@ export { StandardRelayerAPIAssetBuyerManager } from './standard_relayer_api_asse export { AssetBuyerError, BuyQuote, - OrderFetcher, - OrderFetcherRequest, - OrderFetcherResponse, + OrderProvider, + OrderProviderRequest, + OrderProviderResponse, SignedOrderWithRemainingFillableMakerAssetAmount, StandardRelayerApiAssetBuyerManagerError, } from './types'; diff --git a/packages/asset-buyer/src/order_fetchers/provided_order_fetcher.ts b/packages/asset-buyer/src/order_fetchers/provided_order_fetcher.ts index 7a7ffcdfe..397f296d7 100644 --- a/packages/asset-buyer/src/order_fetchers/provided_order_fetcher.ts +++ b/packages/asset-buyer/src/order_fetchers/provided_order_fetcher.ts @@ -2,10 +2,10 @@ import { schemas } from '@0xproject/json-schemas'; import { SignedOrder } from '@0xproject/types'; import * as _ from 'lodash'; -import { OrderFetcher, OrderFetcherRequest, OrderFetcherResponse } from '../types'; +import { OrderProvider, OrderProviderRequest, OrderProviderResponse } from '../types'; import { assert } from '../utils/assert'; -export class ProvidedOrderFetcher implements OrderFetcher { +export class ProvidedOrderFetcher implements OrderProvider { public readonly providedOrders: SignedOrder[]; /** * Instantiates a new ProvidedOrderFetcher instance @@ -21,7 +21,7 @@ export class ProvidedOrderFetcher implements OrderFetcher { * @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 { + public async getOrdersAsync(orderFetchRequest: OrderProviderRequest): Promise { assert.isValidOrderFetcherRequest('orderFetchRequest', orderFetchRequest); const { makerAssetData, takerAssetData } = orderFetchRequest; const orders = _.filter(this.providedOrders, order => { 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 3b082b68d..730eaf4e0 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 @@ -3,15 +3,15 @@ import * as _ from 'lodash'; import { AssetBuyerError, - OrderFetcher, - OrderFetcherRequest, - OrderFetcherResponse, + OrderProvider, + OrderProviderRequest, + OrderProviderResponse, SignedOrderWithRemainingFillableMakerAssetAmount, } from '../types'; import { assert } from '../utils/assert'; import { orderUtils } from '../utils/order_utils'; -export class StandardRelayerAPIOrderFetcher implements OrderFetcher { +export class StandardRelayerAPIOrderFetcher implements OrderProvider { public readonly apiUrl: string; private readonly _sraClient: HttpClient; /** @@ -56,7 +56,7 @@ export class StandardRelayerAPIOrderFetcher implements OrderFetcher { * @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 { + public async getOrdersAsync(orderFetchRequest: OrderProviderRequest): Promise { assert.isValidOrderFetcherRequest('orderFetchRequest', orderFetchRequest); const { makerAssetData, takerAssetData, networkId } = orderFetchRequest; const orderbookRequest = { baseAssetData: makerAssetData, quoteAssetData: takerAssetData }; diff --git a/packages/asset-buyer/src/standard_relayer_api_asset_buyer_manager.ts b/packages/asset-buyer/src/standard_relayer_api_asset_buyer_manager.ts index 6cd96fab2..866bee46e 100644 --- a/packages/asset-buyer/src/standard_relayer_api_asset_buyer_manager.ts +++ b/packages/asset-buyer/src/standard_relayer_api_asset_buyer_manager.ts @@ -9,8 +9,9 @@ import { constants } from './constants'; import { assert } from './utils/assert'; import { assetDataUtils } from './utils/asset_data_utils'; -import { OrderFetcher, StandardRelayerApiAssetBuyerManagerError } from './types'; +import { OrderProvider, StandardRelayerApiAssetBuyerManagerError } from './types'; +// TODO: Read-only list of available assetDatas export class StandardRelayerAPIAssetBuyerManager { // Map of assetData to AssetBuyer for that assetData public readonly assetBuyerMap: ObjectMap; @@ -37,7 +38,7 @@ export class StandardRelayerAPIAssetBuyerManager { * Instantiates a new StandardRelayerAPIAssetBuyerManager instance with all available assetDatas at the provided sraApiUrl * @param provider The Provider instance you would like to use for interacting with the Ethereum network. * @param sraApiUrl The standard relayer API base HTTP url you would like to source orders from. - * @param orderFetcher An object that conforms to OrderFetcher, see type for definition. + * @param orderProvider An object that conforms to OrderProvider, see type for definition. * @param networkId The ethereum network id. Defaults to 1 (mainnet). * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states. * Defaults to 10000ms (10s). @@ -46,12 +47,12 @@ export class StandardRelayerAPIAssetBuyerManager { public static async getAssetBuyerManagerWithAllAvailableAssetDatasAsync( provider: Provider, sraApiUrl: string, - orderFetcher: OrderFetcher, + orderProvider: OrderProvider, networkId: number = constants.MAINNET_NETWORK_ID, orderRefreshIntervalMs?: number, ): Promise { const contractWrappers = new ContractWrappers(provider, { networkId }); - const etherTokenAssetData = assetDataUtils.getEtherTokenAssetData(contractWrappers); + const etherTokenAssetData = assetDataUtils.getEtherTokenAssetDataOrThrow(contractWrappers); const assetDatas = await StandardRelayerAPIAssetBuyerManager.getAllAvailableAssetDatasAsync( sraApiUrl, etherTokenAssetData, @@ -59,7 +60,7 @@ export class StandardRelayerAPIAssetBuyerManager { return new StandardRelayerAPIAssetBuyerManager( provider, assetDatas, - orderFetcher, + orderProvider, networkId, orderRefreshIntervalMs, ); @@ -68,7 +69,7 @@ export class StandardRelayerAPIAssetBuyerManager { * Instantiates a new StandardRelayerAPIAssetBuyerManager instance * @param provider The Provider instance you would like to use for interacting with the Ethereum network. * @param assetDatas The assetDatas of the desired assets to buy (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md). - * @param orderFetcher An object that conforms to OrderFetcher, see type for definition. + * @param orderProvider An object that conforms to OrderProvider, see type for definition. * @param networkId The ethereum network id. Defaults to 1 (mainnet). * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states. * Defaults to 10000ms (10s). @@ -77,7 +78,7 @@ export class StandardRelayerAPIAssetBuyerManager { constructor( provider: Provider, assetDatas: string[], - orderFetcher: OrderFetcher, + orderProvider: OrderProvider, networkId?: number, orderRefreshIntervalMs?: number, ) { @@ -88,7 +89,7 @@ export class StandardRelayerAPIAssetBuyerManager { accAssetBuyerMap[assetData] = new AssetBuyer( provider, assetData, - orderFetcher, + orderProvider, networkId, orderRefreshIntervalMs, ); @@ -98,7 +99,7 @@ export class StandardRelayerAPIAssetBuyerManager { ); } /** - * Get a AssetBuyer for the provided assetData + * Get an AssetBuyer for the provided assetData * @param assetData The desired assetData. * * @return An instance of AssetBuyer @@ -113,7 +114,7 @@ export class StandardRelayerAPIAssetBuyerManager { return assetBuyer; } /** - * Get a AssetBuyer for the provided ERC20 tokenAddress + * Get an AssetBuyer for the provided ERC20 tokenAddress * @param tokenAddress The desired tokenAddress. * * @return An instance of AssetBuyer diff --git a/packages/asset-buyer/src/types.ts b/packages/asset-buyer/src/types.ts index c30bdf4bb..09319fca4 100644 --- a/packages/asset-buyer/src/types.ts +++ b/packages/asset-buyer/src/types.ts @@ -6,7 +6,7 @@ import { BigNumber } from '@0xproject/utils'; * takerAssetData: The assetData representing the desired takerAsset. * networkId: The networkId that the desired orders should be for. */ -export interface OrderFetcherRequest { +export interface OrderProviderRequest { makerAssetData: string; takerAssetData: string; networkId: number; @@ -15,7 +15,7 @@ export interface OrderFetcherRequest { /** * orders: An array of orders with optional remaining fillable makerAsset amounts. See type for more info. */ -export interface OrderFetcherResponse { +export interface OrderProviderResponse { orders: SignedOrderWithRemainingFillableMakerAssetAmount[]; } @@ -29,8 +29,8 @@ export interface SignedOrderWithRemainingFillableMakerAssetAmount extends Signed /** * Given an OrderFetchRequest, get an OrderFetchResponse. */ -export interface OrderFetcher { - fetchOrdersAsync: (orderFetchRequest: OrderFetcherRequest) => Promise; +export interface OrderProvider { + getOrdersAsync: (orderProviderRequest: OrderProviderRequest) => Promise; } /** diff --git a/packages/asset-buyer/src/utils/assert.ts b/packages/asset-buyer/src/utils/assert.ts index 745ba726f..1d57ae9b8 100644 --- a/packages/asset-buyer/src/utils/assert.ts +++ b/packages/asset-buyer/src/utils/assert.ts @@ -3,7 +3,7 @@ import { schemas } from '@0xproject/json-schemas'; import { SignedOrder } from '@0xproject/types'; import * as _ from 'lodash'; -import { BuyQuote, OrderFetcher, OrderFetcherRequest } from '../types'; +import { BuyQuote, OrderProvider, OrderProviderRequest } from '../types'; export const assert = { ...sharedAssert, @@ -18,10 +18,10 @@ export const assert = { sharedAssert.isNumber(`${variableName}.feePercentage`, buyQuote.feePercentage); } }, - isValidOrderFetcher(variableName: string, orderFetcher: OrderFetcher): void { - sharedAssert.isFunction(`${variableName}.fetchOrdersAsync`, orderFetcher.fetchOrdersAsync); + isValidOrderFetcher(variableName: string, orderFetcher: OrderProvider): void { + sharedAssert.isFunction(`${variableName}.fetchOrdersAsync`, orderFetcher.getOrdersAsync); }, - isValidOrderFetcherRequest(variableName: string, orderFetcherRequest: OrderFetcherRequest): void { + isValidOrderFetcherRequest(variableName: string, orderFetcherRequest: OrderProviderRequest): void { sharedAssert.isHexString(`${variableName}.makerAssetData`, orderFetcherRequest.makerAssetData); sharedAssert.isHexString(`${variableName}.takerAssetData`, orderFetcherRequest.takerAssetData); sharedAssert.isNumber(`${variableName}.networkId`, orderFetcherRequest.networkId); diff --git a/packages/asset-buyer/src/utils/asset_data_utils.ts b/packages/asset-buyer/src/utils/asset_data_utils.ts index 10ff31057..d05ff2504 100644 --- a/packages/asset-buyer/src/utils/asset_data_utils.ts +++ b/packages/asset-buyer/src/utils/asset_data_utils.ts @@ -6,7 +6,7 @@ import { AssetBuyerError } from '../types'; export const assetDataUtils = { ...sharedAssetDataUtils, - getEtherTokenAssetData(contractWrappers: ContractWrappers): string { + getEtherTokenAssetDataOrThrow(contractWrappers: ContractWrappers): string { const etherTokenAddressIfExists = contractWrappers.etherToken.getContractAddressIfExists(); if (_.isUndefined(etherTokenAddressIfExists)) { throw new Error(AssetBuyerError.NoEtherTokenContractFound); @@ -14,7 +14,7 @@ export const assetDataUtils = { const etherTokenAssetData = sharedAssetDataUtils.encodeERC20AssetData(etherTokenAddressIfExists); return etherTokenAssetData; }, - getZrxTokenAssetData(contractWrappers: ContractWrappers): string { + getZrxTokenAssetDataOrThrow(contractWrappers: ContractWrappers): string { let zrxTokenAssetData: string; try { zrxTokenAssetData = contractWrappers.exchange.getZRXAssetData(); diff --git a/packages/asset-buyer/src/utils/order_fetcher_response_processor.ts b/packages/asset-buyer/src/utils/order_fetcher_response_processor.ts index f700cadf9..98ab6b9d1 100644 --- a/packages/asset-buyer/src/utils/order_fetcher_response_processor.ts +++ b/packages/asset-buyer/src/utils/order_fetcher_response_processor.ts @@ -8,7 +8,7 @@ import * as _ from 'lodash'; import { constants } from '../constants'; import { AssetBuyerOrdersAndFillableAmounts, - OrderFetcherResponse, + OrderProviderResponse, SignedOrderWithRemainingFillableMakerAssetAmount, } from '../types'; @@ -28,8 +28,8 @@ export const orderFetcherResponseProcessor = { * - Sort by rate */ async processAsync( - targetOrderFetcherResponse: OrderFetcherResponse, - feeOrderFetcherResponse: OrderFetcherResponse, + targetOrderFetcherResponse: OrderProviderResponse, + feeOrderFetcherResponse: OrderProviderResponse, zrxTokenAssetData: string, orderValidator?: OrderValidatorWrapper, ): Promise { -- cgit v1.2.3