diff options
-rw-r--r-- | packages/asset-buyer/src/asset_buyer.ts | 40 | ||||
-rw-r--r-- | packages/asset-buyer/src/index.ts | 4 | ||||
-rw-r--r-- | packages/asset-buyer/src/order_fetchers/provided_order_fetcher.ts | 32 | ||||
-rw-r--r-- | packages/asset-buyer/src/order_providers/basic_order_provider.ts | 32 | ||||
-rw-r--r-- | packages/asset-buyer/src/order_providers/standard_relayer_api_order_provider.ts (renamed from packages/asset-buyer/src/order_fetchers/standard_relayer_api_order_fetcher.ts) | 20 | ||||
-rw-r--r-- | packages/asset-buyer/src/types.ts | 2 | ||||
-rw-r--r-- | packages/asset-buyer/src/utils/assert.ts | 4 | ||||
-rw-r--r-- | packages/asset-buyer/src/utils/buy_quote_calculator.ts | 1 |
8 files changed, 68 insertions, 67 deletions
diff --git a/packages/asset-buyer/src/asset_buyer.ts b/packages/asset-buyer/src/asset_buyer.ts index 74ced0e51..9774a8d39 100644 --- a/packages/asset-buyer/src/asset_buyer.ts +++ b/packages/asset-buyer/src/asset_buyer.ts @@ -7,8 +7,8 @@ import { Provider } from 'ethereum-types'; import * as _ from 'lodash'; import { constants } from './constants'; -import { ProvidedOrderFetcher } from './order_fetchers/provided_order_fetcher'; -import { StandardRelayerAPIOrderFetcher } from './order_fetchers/standard_relayer_api_order_fetcher'; +import { BasicOrderProvider } from './order_providers/basic_order_provider'; +import { StandardRelayerAPIOrderProvider } from './order_providers/standard_relayer_api_order_provider'; import { AssetBuyerError, AssetBuyerOrdersAndFillableAmounts, @@ -26,7 +26,7 @@ import { orderProviderResponseProcessor } from './utils/order_provider_response_ export class AssetBuyer { public readonly provider: Provider; public readonly assetData: string; - public readonly orderFetcher: OrderProvider; + public readonly orderProvider: OrderProvider; public readonly networkId: number; public readonly orderRefreshIntervalMs: number; private readonly _contractWrappers: ContractWrappers; @@ -58,8 +58,8 @@ export class AssetBuyer { assert.areValidProvidedOrders('feeOrders', feeOrders); assert.assert(orders.length !== 0, `Expected orders to contain at least one order`); const assetData = orders[0].makerAssetData; - const orderFetcher = new ProvidedOrderFetcher(_.concat(orders, feeOrders)); - const assetBuyer = new AssetBuyer(provider, assetData, orderFetcher, networkId, orderRefreshIntervalMs); + const orderProvider = new BasicOrderProvider(_.concat(orders, feeOrders)); + const assetBuyer = new AssetBuyer(provider, assetData, orderProvider, networkId, orderRefreshIntervalMs); return assetBuyer; } /** @@ -84,8 +84,8 @@ export class AssetBuyer { assert.isWebUri('sraApiUrl', sraApiUrl); assert.isNumber('networkId', networkId); assert.isNumber('orderRefreshIntervalMs', orderRefreshIntervalMs); - const orderFetcher = new StandardRelayerAPIOrderFetcher(sraApiUrl); - const assetBuyer = new AssetBuyer(provider, assetData, orderFetcher, networkId, orderRefreshIntervalMs); + const orderProvider = new StandardRelayerAPIOrderProvider(sraApiUrl); + const assetBuyer = new AssetBuyer(provider, assetData, orderProvider, networkId, orderRefreshIntervalMs); return assetBuyer; } /** @@ -124,7 +124,7 @@ export class AssetBuyer { * Instantiates a new AssetBuyer instance * @param provider The Provider instance you would like to use for interacting with the Ethereum network. * @param assetData The assetData of the desired asset 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). @@ -133,18 +133,18 @@ export class AssetBuyer { constructor( provider: Provider, assetData: string, - orderFetcher: OrderProvider, + orderProvider: OrderProvider, networkId: number = constants.MAINNET_NETWORK_ID, orderRefreshIntervalMs: number = constants.DEFAULT_ORDER_REFRESH_INTERVAL_MS, ) { assert.isWeb3Provider('provider', provider); assert.isString('assetData', assetData); - assert.isValidOrderFetcher('orderFetcher', orderFetcher); + assert.isValidOrderProvider('orderProvider', orderProvider); assert.isNumber('networkId', networkId); assert.isNumber('orderRefreshIntervalMs', orderRefreshIntervalMs); this.provider = provider; this.assetData = assetData; - this.orderFetcher = orderFetcher; + this.orderProvider = orderProvider; this.networkId = networkId; this.orderRefreshIntervalMs = orderRefreshIntervalMs; this._contractWrappers = new ContractWrappers(this.provider, { @@ -248,31 +248,31 @@ export class AssetBuyer { return txHash; } /** - * Ask the order fetcher for orders and process them. + * Ask the order Provider for orders and process them. */ private async _getLatestOrdersAndFillableAmountsAsync(): Promise<AssetBuyerOrdersAndFillableAmounts> { const etherTokenAssetData = this._getEtherTokenAssetDataOrThrow(); const zrxTokenAssetData = this._getZrxTokenAssetDataOrThrow(); - // construct order fetcher requests - const targetOrderFetcherRequest = { + // construct order Provider requests + const targetOrderProviderRequest = { makerAssetData: this.assetData, takerAssetData: etherTokenAssetData, networkId: this.networkId, }; - const feeOrderFetcherRequest = { + const feeOrderProviderRequest = { makerAssetData: zrxTokenAssetData, takerAssetData: etherTokenAssetData, networkId: this.networkId, }; - const requests = [targetOrderFetcherRequest, feeOrderFetcherRequest]; + const requests = [targetOrderProviderRequest, feeOrderProviderRequest]; // fetch orders and possible fillable amounts - const [targetOrderFetcherResponse, feeOrderFetcherResponse] = await Promise.all( - _.map(requests, async request => this.orderFetcher.getOrdersAsync(request)), + const [targetOrderProviderResponse, feeOrderProviderResponse] = await Promise.all( + _.map(requests, async request => this.orderProvider.getOrdersAsync(request)), ); // process the responses into one object const ordersAndFillableAmounts = await orderProviderResponseProcessor.processAsync( - targetOrderFetcherResponse, - feeOrderFetcherResponse, + targetOrderProviderResponse, + feeOrderProviderResponse, zrxTokenAssetData, this._contractWrappers.orderValidator, ); diff --git a/packages/asset-buyer/src/index.ts b/packages/asset-buyer/src/index.ts index aa10c3af2..8ef529ac0 100644 --- a/packages/asset-buyer/src/index.ts +++ b/packages/asset-buyer/src/index.ts @@ -3,8 +3,8 @@ export { SignedOrder } from '@0xproject/types'; export { BigNumber } from '@0xproject/utils'; export { AssetBuyer } from './asset_buyer'; -export { ProvidedOrderFetcher } from './order_fetchers/provided_order_fetcher'; -export { StandardRelayerAPIOrderFetcher } from './order_fetchers/standard_relayer_api_order_fetcher'; +export { BasicOrderProvider } from './order_providers/basic_order_provider'; +export { StandardRelayerAPIOrderProvider } from './order_providers/standard_relayer_api_order_provider'; export { StandardRelayerAPIAssetBuyerManager } from './standard_relayer_api_asset_buyer_manager'; export { AssetBuyerError, diff --git a/packages/asset-buyer/src/order_fetchers/provided_order_fetcher.ts b/packages/asset-buyer/src/order_fetchers/provided_order_fetcher.ts deleted file mode 100644 index 397f296d7..000000000 --- a/packages/asset-buyer/src/order_fetchers/provided_order_fetcher.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { schemas } from '@0xproject/json-schemas'; -import { SignedOrder } from '@0xproject/types'; -import * as _ from 'lodash'; - -import { OrderProvider, OrderProviderRequest, OrderProviderResponse } from '../types'; -import { assert } from '../utils/assert'; - -export class ProvidedOrderFetcher implements OrderProvider { - 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 getOrdersAsync(orderFetchRequest: OrderProviderRequest): Promise<OrderProviderResponse> { - 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_providers/basic_order_provider.ts b/packages/asset-buyer/src/order_providers/basic_order_provider.ts new file mode 100644 index 000000000..9bb2d90ac --- /dev/null +++ b/packages/asset-buyer/src/order_providers/basic_order_provider.ts @@ -0,0 +1,32 @@ +import { schemas } from '@0xproject/json-schemas'; +import { SignedOrder } from '@0xproject/types'; +import * as _ from 'lodash'; + +import { OrderProvider, OrderProviderRequest, OrderProviderResponse } from '../types'; +import { assert } from '../utils/assert'; + +export class BasicOrderProvider implements OrderProvider { + public readonly orders: SignedOrder[]; + /** + * Instantiates a new BasicOrderProvider instance + * @param orders An array of objects that conform to SignedOrder to fetch from. + * @return An instance of BasicOrderProvider + */ + constructor(orders: SignedOrder[]) { + assert.doesConformToSchema('orders', orders, schemas.signedOrdersSchema); + this.orders = orders; + } + /** + * Given an object that conforms to OrderFetcherRequest, return the corresponding OrderProviderResponse that satisfies the request. + * @param orderProviderRequest An instance of OrderFetcherRequest. See type for more information. + * @return An instance of OrderProviderResponse. See type for more information. + */ + public async getOrdersAsync(orderProviderRequest: OrderProviderRequest): Promise<OrderProviderResponse> { + assert.isValidOrderProviderRequest('orderProviderRequest', orderProviderRequest); + const { makerAssetData, takerAssetData } = orderProviderRequest; + const orders = _.filter(this.orders, 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_providers/standard_relayer_api_order_provider.ts index 730eaf4e0..be3e9da50 100644 --- a/packages/asset-buyer/src/order_fetchers/standard_relayer_api_order_fetcher.ts +++ b/packages/asset-buyer/src/order_providers/standard_relayer_api_order_provider.ts @@ -11,7 +11,7 @@ import { import { assert } from '../utils/assert'; import { orderUtils } from '../utils/order_utils'; -export class StandardRelayerAPIOrderFetcher implements OrderProvider { +export class StandardRelayerAPIOrderProvider implements OrderProvider { public readonly apiUrl: string; private readonly _sraClient: HttpClient; /** @@ -42,9 +42,9 @@ export class StandardRelayerAPIOrderFetcher implements OrderProvider { return result; } /** - * Instantiates a new StandardRelayerAPIOrderFetcher instance + * Instantiates a new StandardRelayerAPIOrderProvider instance * @param apiUrl The standard relayer API base HTTP url you would like to source orders from. - * @return An instance of StandardRelayerAPIOrderFetcher + * @return An instance of StandardRelayerAPIOrderProvider */ constructor(apiUrl: string) { assert.isWebUri('apiUrl', apiUrl); @@ -52,13 +52,13 @@ export class StandardRelayerAPIOrderFetcher implements OrderProvider { this._sraClient = new HttpClient(apiUrl); } /** - * 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. + * Given an object that conforms to OrderProviderRequest, return the corresponding OrderProviderResponse that satisfies the request. + * @param orderProviderRequest An instance of OrderProviderRequest. See type for more information. + * @return An instance of OrderProviderResponse. See type for more information. */ - public async getOrdersAsync(orderFetchRequest: OrderProviderRequest): Promise<OrderProviderResponse> { - assert.isValidOrderFetcherRequest('orderFetchRequest', orderFetchRequest); - const { makerAssetData, takerAssetData, networkId } = orderFetchRequest; + public async getOrdersAsync(orderProviderRequest: OrderProviderRequest): Promise<OrderProviderResponse> { + assert.isValidOrderProviderRequest('orderProviderRequest', orderProviderRequest); + const { makerAssetData, takerAssetData, networkId } = orderProviderRequest; const orderbookRequest = { baseAssetData: makerAssetData, quoteAssetData: takerAssetData }; const requestOpts = { networkId }; let orderbook: OrderbookResponse; @@ -68,7 +68,7 @@ export class StandardRelayerAPIOrderFetcher implements OrderProvider { throw new Error(AssetBuyerError.StandardRelayerApiError); } const apiOrders = orderbook.asks.records; - const orders = StandardRelayerAPIOrderFetcher._getSignedOrderWithRemainingFillableMakerAssetAmountFromApi( + const orders = StandardRelayerAPIOrderProvider._getSignedOrderWithRemainingFillableMakerAssetAmountFromApi( apiOrders, ); return { diff --git a/packages/asset-buyer/src/types.ts b/packages/asset-buyer/src/types.ts index 09319fca4..5df8e632a 100644 --- a/packages/asset-buyer/src/types.ts +++ b/packages/asset-buyer/src/types.ts @@ -27,7 +27,7 @@ export interface SignedOrderWithRemainingFillableMakerAssetAmount extends Signed remainingFillableMakerAssetAmount?: BigNumber; } /** - * Given an OrderFetchRequest, get an OrderFetchResponse. + * Given an OrderProviderRequest, get an OrderProviderResponse. */ export interface OrderProvider { getOrdersAsync: (orderProviderRequest: OrderProviderRequest) => Promise<OrderProviderResponse>; diff --git a/packages/asset-buyer/src/utils/assert.ts b/packages/asset-buyer/src/utils/assert.ts index 1d57ae9b8..2c3426894 100644 --- a/packages/asset-buyer/src/utils/assert.ts +++ b/packages/asset-buyer/src/utils/assert.ts @@ -18,10 +18,10 @@ export const assert = { sharedAssert.isNumber(`${variableName}.feePercentage`, buyQuote.feePercentage); } }, - isValidOrderFetcher(variableName: string, orderFetcher: OrderProvider): void { + isValidOrderProvider(variableName: string, orderFetcher: OrderProvider): void { sharedAssert.isFunction(`${variableName}.fetchOrdersAsync`, orderFetcher.getOrdersAsync); }, - isValidOrderFetcherRequest(variableName: string, orderFetcherRequest: OrderProviderRequest): void { + isValidOrderProviderRequest(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/buy_quote_calculator.ts b/packages/asset-buyer/src/utils/buy_quote_calculator.ts index 3d90a6ead..9946924ef 100644 --- a/packages/asset-buyer/src/utils/buy_quote_calculator.ts +++ b/packages/asset-buyer/src/utils/buy_quote_calculator.ts @@ -7,6 +7,7 @@ import { AssetBuyerError, AssetBuyerOrdersAndFillableAmounts, BuyQuote } from '. import { orderUtils } from './order_utils'; +// Calculates a buy quote for orders that have WETH as the takerAsset export const buyQuoteCalculator = { calculate( ordersAndFillableAmounts: AssetBuyerOrdersAndFillableAmounts, |