Rename OrderFetcher to OrderProvider and other small improvements

This commit is contained in:
fragosti
2018-09-20 15:10:15 +02:00
parent d23487bda9
commit 03e18ff7c6
9 changed files with 48 additions and 49 deletions

View File

@@ -14,8 +14,8 @@ import {
AssetBuyerOrdersAndFillableAmounts, AssetBuyerOrdersAndFillableAmounts,
BuyQuote, BuyQuote,
BuyQuoteRequestOpts, BuyQuoteRequestOpts,
OrderFetcher, OrderProvider,
OrderFetcherResponse, OrderProviderResponse,
} from './types'; } from './types';
import { assert } from './utils/assert'; import { assert } from './utils/assert';
@@ -26,7 +26,7 @@ import { orderFetcherResponseProcessor } from './utils/order_fetcher_response_pr
export class AssetBuyer { export class AssetBuyer {
public readonly provider: Provider; public readonly provider: Provider;
public readonly assetData: string; public readonly assetData: string;
public readonly orderFetcher: OrderFetcher; public readonly orderFetcher: OrderProvider;
public readonly networkId: number; public readonly networkId: number;
public readonly orderRefreshIntervalMs: number; public readonly orderRefreshIntervalMs: number;
private readonly _contractWrappers: ContractWrappers; private readonly _contractWrappers: ContractWrappers;
@@ -133,7 +133,7 @@ export class AssetBuyer {
constructor( constructor(
provider: Provider, provider: Provider,
assetData: string, assetData: string,
orderFetcher: OrderFetcher, orderFetcher: OrderProvider,
networkId: number = constants.MAINNET_NETWORK_ID, networkId: number = constants.MAINNET_NETWORK_ID,
orderRefreshIntervalMs: number = constants.DEFAULT_ORDER_REFRESH_INTERVAL_MS, 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. * Get a `BuyQuote` containing all information relevant to fulfilling a buy.
* Pass the BuyQuote to executeBuyQuoteAsync to execute the buy. * You can then pass the `BuyQuote` to `executeBuyQuoteAsync` to execute the buy.
* @param assetBuyAmount The amount of asset to buy. * @param assetBuyAmount The amount of asset to buy.
* @param feePercentage The affiliate fee percentage. Defaults to 0. * @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 * @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. * Ask the order fetcher for orders and process them.
*/ */
private async _getLatestOrdersAndFillableAmountsAsync(): Promise<AssetBuyerOrdersAndFillableAmounts> { private async _getLatestOrdersAndFillableAmountsAsync(): Promise<AssetBuyerOrdersAndFillableAmounts> {
// find ether token asset data const etherTokenAssetData = this._getEtherTokenAssetDataOrThrow();
const etherTokenAssetData = this._getEtherTokenAssetData(); const zrxTokenAssetData = this._getZrxTokenAssetDataOrThrow();
// find zrx token asset data
const zrxTokenAssetData = this._getZrxTokenAssetData();
// construct order fetcher requests // construct order fetcher requests
const targetOrderFetcherRequest = { const targetOrderFetcherRequest = {
makerAssetData: this.assetData, makerAssetData: this.assetData,
@@ -269,7 +267,7 @@ export class AssetBuyer {
const requests = [targetOrderFetcherRequest, feeOrderFetcherRequest]; const requests = [targetOrderFetcherRequest, feeOrderFetcherRequest];
// fetch orders and possible fillable amounts // fetch orders and possible fillable amounts
const [targetOrderFetcherResponse, feeOrderFetcherResponse] = await Promise.all( 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 // process the responses into one object
const ordersAndFillableAmounts = await orderFetcherResponseProcessor.processAsync( const ordersAndFillableAmounts = await orderFetcherResponseProcessor.processAsync(
@@ -284,14 +282,14 @@ export class AssetBuyer {
* Get the assetData that represents the WETH token. * Get the assetData that represents the WETH token.
* Will throw if WETH does not exist for the current network. * Will throw if WETH does not exist for the current network.
*/ */
private _getEtherTokenAssetData(): string { private _getEtherTokenAssetDataOrThrow(): string {
return assetDataUtils.getEtherTokenAssetData(this._contractWrappers); return assetDataUtils.getEtherTokenAssetDataOrThrow(this._contractWrappers);
} }
/** /**
* Get the assetData that represents the ZRX token. * Get the assetData that represents the ZRX token.
* Will throw if ZRX does not exist for the current network. * Will throw if ZRX does not exist for the current network.
*/ */
private _getZrxTokenAssetData(): string { private _getZrxTokenAssetDataOrThrow(): string {
return assetDataUtils.getZrxTokenAssetData(this._contractWrappers); return assetDataUtils.getZrxTokenAssetDataOrThrow(this._contractWrappers);
} }
} }

View File

@@ -9,9 +9,9 @@ export { StandardRelayerAPIAssetBuyerManager } from './standard_relayer_api_asse
export { export {
AssetBuyerError, AssetBuyerError,
BuyQuote, BuyQuote,
OrderFetcher, OrderProvider,
OrderFetcherRequest, OrderProviderRequest,
OrderFetcherResponse, OrderProviderResponse,
SignedOrderWithRemainingFillableMakerAssetAmount, SignedOrderWithRemainingFillableMakerAssetAmount,
StandardRelayerApiAssetBuyerManagerError, StandardRelayerApiAssetBuyerManagerError,
} from './types'; } from './types';

View File

@@ -2,10 +2,10 @@ import { schemas } from '@0xproject/json-schemas';
import { SignedOrder } from '@0xproject/types'; import { SignedOrder } from '@0xproject/types';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { OrderFetcher, OrderFetcherRequest, OrderFetcherResponse } from '../types'; import { OrderProvider, OrderProviderRequest, OrderProviderResponse } from '../types';
import { assert } from '../utils/assert'; import { assert } from '../utils/assert';
export class ProvidedOrderFetcher implements OrderFetcher { export class ProvidedOrderFetcher implements OrderProvider {
public readonly providedOrders: SignedOrder[]; public readonly providedOrders: SignedOrder[];
/** /**
* Instantiates a new ProvidedOrderFetcher instance * 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. * @param orderFetchRequest An instance of OrderFetcherRequest. See type for more information.
* @return An instance of OrderFetcherResponse. See type for more information. * @return An instance of OrderFetcherResponse. See type for more information.
*/ */
public async fetchOrdersAsync(orderFetchRequest: OrderFetcherRequest): Promise<OrderFetcherResponse> { public async getOrdersAsync(orderFetchRequest: OrderProviderRequest): Promise<OrderProviderResponse> {
assert.isValidOrderFetcherRequest('orderFetchRequest', orderFetchRequest); assert.isValidOrderFetcherRequest('orderFetchRequest', orderFetchRequest);
const { makerAssetData, takerAssetData } = orderFetchRequest; const { makerAssetData, takerAssetData } = orderFetchRequest;
const orders = _.filter(this.providedOrders, order => { const orders = _.filter(this.providedOrders, order => {

View File

@@ -3,15 +3,15 @@ import * as _ from 'lodash';
import { import {
AssetBuyerError, AssetBuyerError,
OrderFetcher, OrderProvider,
OrderFetcherRequest, OrderProviderRequest,
OrderFetcherResponse, OrderProviderResponse,
SignedOrderWithRemainingFillableMakerAssetAmount, SignedOrderWithRemainingFillableMakerAssetAmount,
} from '../types'; } from '../types';
import { assert } from '../utils/assert'; import { assert } from '../utils/assert';
import { orderUtils } from '../utils/order_utils'; import { orderUtils } from '../utils/order_utils';
export class StandardRelayerAPIOrderFetcher implements OrderFetcher { export class StandardRelayerAPIOrderFetcher implements OrderProvider {
public readonly apiUrl: string; public readonly apiUrl: string;
private readonly _sraClient: HttpClient; private readonly _sraClient: HttpClient;
/** /**
@@ -56,7 +56,7 @@ export class StandardRelayerAPIOrderFetcher implements OrderFetcher {
* @param orderFetchRequest An instance of OrderFetcherRequest. See type for more information. * @param orderFetchRequest An instance of OrderFetcherRequest. See type for more information.
* @return An instance of OrderFetcherResponse. See type for more information. * @return An instance of OrderFetcherResponse. See type for more information.
*/ */
public async fetchOrdersAsync(orderFetchRequest: OrderFetcherRequest): Promise<OrderFetcherResponse> { public async getOrdersAsync(orderFetchRequest: OrderProviderRequest): Promise<OrderProviderResponse> {
assert.isValidOrderFetcherRequest('orderFetchRequest', orderFetchRequest); assert.isValidOrderFetcherRequest('orderFetchRequest', orderFetchRequest);
const { makerAssetData, takerAssetData, networkId } = orderFetchRequest; const { makerAssetData, takerAssetData, networkId } = orderFetchRequest;
const orderbookRequest = { baseAssetData: makerAssetData, quoteAssetData: takerAssetData }; const orderbookRequest = { baseAssetData: makerAssetData, quoteAssetData: takerAssetData };

View File

@@ -9,8 +9,9 @@ import { constants } from './constants';
import { assert } from './utils/assert'; import { assert } from './utils/assert';
import { assetDataUtils } from './utils/asset_data_utils'; 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 { export class StandardRelayerAPIAssetBuyerManager {
// Map of assetData to AssetBuyer for that assetData // Map of assetData to AssetBuyer for that assetData
public readonly assetBuyerMap: ObjectMap<AssetBuyer>; public readonly assetBuyerMap: ObjectMap<AssetBuyer>;
@@ -37,7 +38,7 @@ export class StandardRelayerAPIAssetBuyerManager {
* Instantiates a new StandardRelayerAPIAssetBuyerManager instance with all available assetDatas at the provided sraApiUrl * 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 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 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 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. * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states.
* Defaults to 10000ms (10s). * Defaults to 10000ms (10s).
@@ -46,12 +47,12 @@ export class StandardRelayerAPIAssetBuyerManager {
public static async getAssetBuyerManagerWithAllAvailableAssetDatasAsync( public static async getAssetBuyerManagerWithAllAvailableAssetDatasAsync(
provider: Provider, provider: Provider,
sraApiUrl: string, sraApiUrl: string,
orderFetcher: OrderFetcher, orderProvider: OrderProvider,
networkId: number = constants.MAINNET_NETWORK_ID, networkId: number = constants.MAINNET_NETWORK_ID,
orderRefreshIntervalMs?: number, orderRefreshIntervalMs?: number,
): Promise<StandardRelayerAPIAssetBuyerManager> { ): Promise<StandardRelayerAPIAssetBuyerManager> {
const contractWrappers = new ContractWrappers(provider, { networkId }); const contractWrappers = new ContractWrappers(provider, { networkId });
const etherTokenAssetData = assetDataUtils.getEtherTokenAssetData(contractWrappers); const etherTokenAssetData = assetDataUtils.getEtherTokenAssetDataOrThrow(contractWrappers);
const assetDatas = await StandardRelayerAPIAssetBuyerManager.getAllAvailableAssetDatasAsync( const assetDatas = await StandardRelayerAPIAssetBuyerManager.getAllAvailableAssetDatasAsync(
sraApiUrl, sraApiUrl,
etherTokenAssetData, etherTokenAssetData,
@@ -59,7 +60,7 @@ export class StandardRelayerAPIAssetBuyerManager {
return new StandardRelayerAPIAssetBuyerManager( return new StandardRelayerAPIAssetBuyerManager(
provider, provider,
assetDatas, assetDatas,
orderFetcher, orderProvider,
networkId, networkId,
orderRefreshIntervalMs, orderRefreshIntervalMs,
); );
@@ -68,7 +69,7 @@ export class StandardRelayerAPIAssetBuyerManager {
* Instantiates a new StandardRelayerAPIAssetBuyerManager instance * Instantiates a new StandardRelayerAPIAssetBuyerManager instance
* @param provider The Provider instance you would like to use for interacting with the Ethereum network. * @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 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 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. * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states.
* Defaults to 10000ms (10s). * Defaults to 10000ms (10s).
@@ -77,7 +78,7 @@ export class StandardRelayerAPIAssetBuyerManager {
constructor( constructor(
provider: Provider, provider: Provider,
assetDatas: string[], assetDatas: string[],
orderFetcher: OrderFetcher, orderProvider: OrderProvider,
networkId?: number, networkId?: number,
orderRefreshIntervalMs?: number, orderRefreshIntervalMs?: number,
) { ) {
@@ -88,7 +89,7 @@ export class StandardRelayerAPIAssetBuyerManager {
accAssetBuyerMap[assetData] = new AssetBuyer( accAssetBuyerMap[assetData] = new AssetBuyer(
provider, provider,
assetData, assetData,
orderFetcher, orderProvider,
networkId, networkId,
orderRefreshIntervalMs, 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. * @param assetData The desired assetData.
* *
* @return An instance of AssetBuyer * @return An instance of AssetBuyer
@@ -113,7 +114,7 @@ export class StandardRelayerAPIAssetBuyerManager {
return assetBuyer; return assetBuyer;
} }
/** /**
* Get a AssetBuyer for the provided ERC20 tokenAddress * Get an AssetBuyer for the provided ERC20 tokenAddress
* @param tokenAddress The desired tokenAddress. * @param tokenAddress The desired tokenAddress.
* *
* @return An instance of AssetBuyer * @return An instance of AssetBuyer

View File

@@ -6,7 +6,7 @@ import { BigNumber } from '@0xproject/utils';
* takerAssetData: The assetData representing the desired takerAsset. * takerAssetData: The assetData representing the desired takerAsset.
* networkId: The networkId that the desired orders should be for. * networkId: The networkId that the desired orders should be for.
*/ */
export interface OrderFetcherRequest { export interface OrderProviderRequest {
makerAssetData: string; makerAssetData: string;
takerAssetData: string; takerAssetData: string;
networkId: number; 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. * orders: An array of orders with optional remaining fillable makerAsset amounts. See type for more info.
*/ */
export interface OrderFetcherResponse { export interface OrderProviderResponse {
orders: SignedOrderWithRemainingFillableMakerAssetAmount[]; orders: SignedOrderWithRemainingFillableMakerAssetAmount[];
} }
@@ -29,8 +29,8 @@ export interface SignedOrderWithRemainingFillableMakerAssetAmount extends Signed
/** /**
* Given an OrderFetchRequest, get an OrderFetchResponse. * Given an OrderFetchRequest, get an OrderFetchResponse.
*/ */
export interface OrderFetcher { export interface OrderProvider {
fetchOrdersAsync: (orderFetchRequest: OrderFetcherRequest) => Promise<OrderFetcherResponse>; getOrdersAsync: (orderProviderRequest: OrderProviderRequest) => Promise<OrderProviderResponse>;
} }
/** /**

View File

@@ -3,7 +3,7 @@ import { schemas } from '@0xproject/json-schemas';
import { SignedOrder } from '@0xproject/types'; import { SignedOrder } from '@0xproject/types';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { BuyQuote, OrderFetcher, OrderFetcherRequest } from '../types'; import { BuyQuote, OrderProvider, OrderProviderRequest } from '../types';
export const assert = { export const assert = {
...sharedAssert, ...sharedAssert,
@@ -18,10 +18,10 @@ export const assert = {
sharedAssert.isNumber(`${variableName}.feePercentage`, buyQuote.feePercentage); sharedAssert.isNumber(`${variableName}.feePercentage`, buyQuote.feePercentage);
} }
}, },
isValidOrderFetcher(variableName: string, orderFetcher: OrderFetcher): void { isValidOrderFetcher(variableName: string, orderFetcher: OrderProvider): void {
sharedAssert.isFunction(`${variableName}.fetchOrdersAsync`, orderFetcher.fetchOrdersAsync); 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}.makerAssetData`, orderFetcherRequest.makerAssetData);
sharedAssert.isHexString(`${variableName}.takerAssetData`, orderFetcherRequest.takerAssetData); sharedAssert.isHexString(`${variableName}.takerAssetData`, orderFetcherRequest.takerAssetData);
sharedAssert.isNumber(`${variableName}.networkId`, orderFetcherRequest.networkId); sharedAssert.isNumber(`${variableName}.networkId`, orderFetcherRequest.networkId);

View File

@@ -6,7 +6,7 @@ import { AssetBuyerError } from '../types';
export const assetDataUtils = { export const assetDataUtils = {
...sharedAssetDataUtils, ...sharedAssetDataUtils,
getEtherTokenAssetData(contractWrappers: ContractWrappers): string { getEtherTokenAssetDataOrThrow(contractWrappers: ContractWrappers): string {
const etherTokenAddressIfExists = contractWrappers.etherToken.getContractAddressIfExists(); const etherTokenAddressIfExists = contractWrappers.etherToken.getContractAddressIfExists();
if (_.isUndefined(etherTokenAddressIfExists)) { if (_.isUndefined(etherTokenAddressIfExists)) {
throw new Error(AssetBuyerError.NoEtherTokenContractFound); throw new Error(AssetBuyerError.NoEtherTokenContractFound);
@@ -14,7 +14,7 @@ export const assetDataUtils = {
const etherTokenAssetData = sharedAssetDataUtils.encodeERC20AssetData(etherTokenAddressIfExists); const etherTokenAssetData = sharedAssetDataUtils.encodeERC20AssetData(etherTokenAddressIfExists);
return etherTokenAssetData; return etherTokenAssetData;
}, },
getZrxTokenAssetData(contractWrappers: ContractWrappers): string { getZrxTokenAssetDataOrThrow(contractWrappers: ContractWrappers): string {
let zrxTokenAssetData: string; let zrxTokenAssetData: string;
try { try {
zrxTokenAssetData = contractWrappers.exchange.getZRXAssetData(); zrxTokenAssetData = contractWrappers.exchange.getZRXAssetData();

View File

@@ -8,7 +8,7 @@ import * as _ from 'lodash';
import { constants } from '../constants'; import { constants } from '../constants';
import { import {
AssetBuyerOrdersAndFillableAmounts, AssetBuyerOrdersAndFillableAmounts,
OrderFetcherResponse, OrderProviderResponse,
SignedOrderWithRemainingFillableMakerAssetAmount, SignedOrderWithRemainingFillableMakerAssetAmount,
} from '../types'; } from '../types';
@@ -28,8 +28,8 @@ export const orderFetcherResponseProcessor = {
* - Sort by rate * - Sort by rate
*/ */
async processAsync( async processAsync(
targetOrderFetcherResponse: OrderFetcherResponse, targetOrderFetcherResponse: OrderProviderResponse,
feeOrderFetcherResponse: OrderFetcherResponse, feeOrderFetcherResponse: OrderProviderResponse,
zrxTokenAssetData: string, zrxTokenAssetData: string,
orderValidator?: OrderValidatorWrapper, orderValidator?: OrderValidatorWrapper,
): Promise<AssetBuyerOrdersAndFillableAmounts> { ): Promise<AssetBuyerOrdersAndFillableAmounts> {