Rename OrderFetcher to OrderProvider and other small improvements
This commit is contained in:
@@ -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<AssetBuyerOrdersAndFillableAmounts> {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,9 +9,9 @@ export { StandardRelayerAPIAssetBuyerManager } from './standard_relayer_api_asse
|
||||
export {
|
||||
AssetBuyerError,
|
||||
BuyQuote,
|
||||
OrderFetcher,
|
||||
OrderFetcherRequest,
|
||||
OrderFetcherResponse,
|
||||
OrderProvider,
|
||||
OrderProviderRequest,
|
||||
OrderProviderResponse,
|
||||
SignedOrderWithRemainingFillableMakerAssetAmount,
|
||||
StandardRelayerApiAssetBuyerManagerError,
|
||||
} from './types';
|
||||
|
||||
@@ -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<OrderFetcherResponse> {
|
||||
public async getOrdersAsync(orderFetchRequest: OrderProviderRequest): Promise<OrderProviderResponse> {
|
||||
assert.isValidOrderFetcherRequest('orderFetchRequest', orderFetchRequest);
|
||||
const { makerAssetData, takerAssetData } = orderFetchRequest;
|
||||
const orders = _.filter(this.providedOrders, order => {
|
||||
|
||||
@@ -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<OrderFetcherResponse> {
|
||||
public async getOrdersAsync(orderFetchRequest: OrderProviderRequest): Promise<OrderProviderResponse> {
|
||||
assert.isValidOrderFetcherRequest('orderFetchRequest', orderFetchRequest);
|
||||
const { makerAssetData, takerAssetData, networkId } = orderFetchRequest;
|
||||
const orderbookRequest = { baseAssetData: makerAssetData, quoteAssetData: takerAssetData };
|
||||
|
||||
@@ -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<AssetBuyer>;
|
||||
@@ -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<StandardRelayerAPIAssetBuyerManager> {
|
||||
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
|
||||
|
||||
@@ -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<OrderFetcherResponse>;
|
||||
export interface OrderProvider {
|
||||
getOrdersAsync: (orderProviderRequest: OrderProviderRequest) => Promise<OrderProviderResponse>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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<AssetBuyerOrdersAndFillableAmounts> {
|
||||
|
||||
Reference in New Issue
Block a user