aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/src
diff options
context:
space:
mode:
authorBrandon Millman <brandon.millman@gmail.com>2018-10-03 01:17:09 +0800
committerBrandon Millman <brandon.millman@gmail.com>2018-10-03 04:54:57 +0800
commitb3a868da0e77da04b123f4ff397d8f1a3a4d3810 (patch)
tree474af79684faee3cd5be938ca22b08264721390d /packages/asset-buyer/src
parent04dd4ce6d1d0ca9703610579632d6989f9959277 (diff)
downloaddexon-sol-tools-b3a868da0e77da04b123f4ff397d8f1a3a4d3810.tar
dexon-sol-tools-b3a868da0e77da04b123f4ff397d8f1a3a4d3810.tar.gz
dexon-sol-tools-b3a868da0e77da04b123f4ff397d8f1a3a4d3810.tar.bz2
dexon-sol-tools-b3a868da0e77da04b123f4ff397d8f1a3a4d3810.tar.lz
dexon-sol-tools-b3a868da0e77da04b123f4ff397d8f1a3a4d3810.tar.xz
dexon-sol-tools-b3a868da0e77da04b123f4ff397d8f1a3a4d3810.tar.zst
dexon-sol-tools-b3a868da0e77da04b123f4ff397d8f1a3a4d3810.zip
Merge AssetBuyer and AssetBuyerManager
Diffstat (limited to 'packages/asset-buyer/src')
-rw-r--r--packages/asset-buyer/src/asset_buyer.ts208
-rw-r--r--packages/asset-buyer/src/asset_buyer_manager.ts171
-rw-r--r--packages/asset-buyer/src/types.ts4
-rw-r--r--packages/asset-buyer/src/utils/buy_quote_calculator.ts15
-rw-r--r--packages/asset-buyer/src/utils/order_provider_response_processor.ts76
5 files changed, 148 insertions, 326 deletions
diff --git a/packages/asset-buyer/src/asset_buyer.ts b/packages/asset-buyer/src/asset_buyer.ts
index afef0d070..990369615 100644
--- a/packages/asset-buyer/src/asset_buyer.ts
+++ b/packages/asset-buyer/src/asset_buyer.ts
@@ -1,6 +1,8 @@
+import { HttpClient } from '@0xproject/connect';
import { ContractWrappers } from '@0xproject/contract-wrappers';
import { schemas } from '@0xproject/json-schemas';
import { SignedOrder } from '@0xproject/order-utils';
+import { ObjectMap } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import { Provider } from 'ethereum-types';
@@ -12,12 +14,12 @@ import { StandardRelayerAPIOrderProvider } from './order_providers/standard_rela
import {
AssetBuyerError,
AssetBuyerOpts,
- AssetBuyerOrdersAndFillableAmounts,
BuyQuote,
BuyQuoteExecutionOpts,
BuyQuoteRequestOpts,
OrderProvider,
OrderProviderResponse,
+ OrdersAndFillableAmounts,
} from './types';
import { assert } from './utils/assert';
@@ -25,16 +27,39 @@ import { assetDataUtils } from './utils/asset_data_utils';
import { buyQuoteCalculator } from './utils/buy_quote_calculator';
import { orderProviderResponseProcessor } from './utils/order_provider_response_processor';
+interface OrdersEntry {
+ ordersAndFillableAmounts: OrdersAndFillableAmounts;
+ lastRefreshTime: number;
+}
+
export class AssetBuyer {
public readonly provider: Provider;
- public readonly assetData: string;
public readonly orderProvider: OrderProvider;
public readonly networkId: number;
public readonly orderRefreshIntervalMs: number;
public readonly expiryBufferSeconds: number;
private readonly _contractWrappers: ContractWrappers;
- private _lastRefreshTimeIfExists?: number;
- private _currentOrdersAndFillableAmountsIfExists?: AssetBuyerOrdersAndFillableAmounts;
+ // cache of orders along with the time last updated keyed by assetData
+ private readonly _ordersEntryMap: ObjectMap<OrdersEntry> = {};
+ /**
+ * Returns an array of all assetDatas available at the provided sraApiUrl
+ * @param sraApiUrl The standard relayer API base HTTP url you would like to source orders from.
+ * @param pairedWithAssetData Optional filter argument to return assetDatas that only pair with this assetData value.
+ *
+ * @return An array of all assetDatas available at the provider sraApiUrl
+ */
+ public static async getAllAvailableAssetDatasAsync(
+ sraApiUrl: string,
+ pairedWithAssetData?: string,
+ ): Promise<string[]> {
+ const client = new HttpClient(sraApiUrl);
+ const params = {
+ assetDataA: pairedWithAssetData,
+ perPage: constants.MAX_PER_PAGE,
+ };
+ const assetPairsResponse = await client.getAssetPairsAsync(params);
+ return _.uniq(_.map(assetPairsResponse.records, pairsItem => pairsItem.assetDataB.assetData));
+ }
/**
* Instantiates a new AssetBuyer instance given existing liquidity in the form of orders and feeOrders.
* @param provider The Provider instance you would like to use for interacting with the Ethereum network.
@@ -48,7 +73,7 @@ export class AssetBuyer {
provider: Provider,
orders: SignedOrder[],
feeOrders: SignedOrder[] = [],
- options: Partial<AssetBuyerOpts>,
+ options: Partial<AssetBuyerOpts> = {},
): AssetBuyer {
assert.isWeb3Provider('provider', provider);
assert.doesConformToSchema('orders', orders, schemas.signedOrdersSchema);
@@ -56,9 +81,8 @@ export class AssetBuyer {
assert.areValidProvidedOrders('orders', orders);
assert.areValidProvidedOrders('feeOrders', feeOrders);
assert.assert(orders.length !== 0, `Expected orders to contain at least one order`);
- const assetData = orders[0].makerAssetData;
const orderProvider = new BasicOrderProvider(_.concat(orders, feeOrders));
- const assetBuyer = new AssetBuyer(provider, assetData, orderProvider, options);
+ const assetBuyer = new AssetBuyer(provider, orderProvider, options);
return assetBuyer;
}
/**
@@ -70,63 +94,36 @@ export class AssetBuyer {
*
* @return An instance of AssetBuyer
*/
- public static getAssetBuyerForAssetData(
+ public static getAssetBuyerForSraApiUrl(
provider: Provider,
- assetData: string,
sraApiUrl: string,
- options: Partial<AssetBuyerOpts>,
+ options: Partial<AssetBuyerOpts> = {},
): AssetBuyer {
assert.isWeb3Provider('provider', provider);
- assert.isHexString('assetData', assetData);
assert.isWebUri('sraApiUrl', sraApiUrl);
const orderProvider = new StandardRelayerAPIOrderProvider(sraApiUrl);
- const assetBuyer = new AssetBuyer(provider, assetData, orderProvider, options);
- return assetBuyer;
- }
- /**
- * Instantiates a new AssetBuyer instance given the desired ERC20 token address and a [Standard Relayer API](https://github.com/0xProject/standard-relayer-api) endpoint
- * @param provider The Provider instance you would like to use for interacting with the Ethereum network.
- * @param tokenAddress The ERC20 token address that identifies the desired asset to buy.
- * @param sraApiUrl The standard relayer API base HTTP url you would like to source orders from.
- * @param options Initialization options for the AssetBuyer. See type definition for details.
- *
- * @return An instance of AssetBuyer
- */
- public static getAssetBuyerForERC20TokenAddress(
- provider: Provider,
- tokenAddress: string,
- sraApiUrl: string,
- options: Partial<AssetBuyerOpts>,
- ): AssetBuyer {
- assert.isWeb3Provider('provider', provider);
- assert.isETHAddressHex('tokenAddress', tokenAddress);
- assert.isWebUri('sraApiUrl', sraApiUrl);
- const assetData = assetDataUtils.encodeERC20AssetData(tokenAddress);
- const assetBuyer = AssetBuyer.getAssetBuyerForAssetData(provider, assetData, sraApiUrl, options);
+ const assetBuyer = new AssetBuyer(provider, orderProvider, options);
return 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 orderProvider An object that conforms to OrderProvider, see type for definition.
* @param options Initialization options for the AssetBuyer. See type definition for details.
*
* @return An instance of AssetBuyer
*/
- constructor(provider: Provider, assetData: string, orderProvider: OrderProvider, options: Partial<AssetBuyerOpts>) {
+ constructor(provider: Provider, orderProvider: OrderProvider, options: Partial<AssetBuyerOpts> = {}) {
const { networkId, orderRefreshIntervalMs, expiryBufferSeconds } = {
...constants.DEFAULT_ASSET_BUYER_OPTS,
...options,
};
assert.isWeb3Provider('provider', provider);
- assert.isString('assetData', assetData);
assert.isValidOrderProvider('orderProvider', orderProvider);
assert.isNumber('networkId', networkId);
assert.isNumber('orderRefreshIntervalMs', orderRefreshIntervalMs);
assert.isNumber('expiryBufferSeconds', expiryBufferSeconds);
this.provider = provider;
- this.assetData = assetData;
this.orderProvider = orderProvider;
this.networkId = networkId;
this.expiryBufferSeconds = expiryBufferSeconds;
@@ -136,42 +133,36 @@ 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 given a desired assetData.
* You can then pass the `BuyQuote` to `executeBuyQuoteAsync` to execute the buy.
+ * @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 assetBuyAmount The amount of asset to buy.
* @param options Options for the request. See type definition for more information.
*
* @return An object that conforms to BuyQuote that satisfies the request. See type definition for more information.
*/
- public async getBuyQuoteAsync(assetBuyAmount: BigNumber, options: Partial<BuyQuoteRequestOpts>): Promise<BuyQuote> {
+ public async getBuyQuoteAsync(
+ assetData: string,
+ assetBuyAmount: BigNumber,
+ options: Partial<BuyQuoteRequestOpts>,
+ ): Promise<BuyQuote> {
const { feePercentage, shouldForceOrderRefresh, slippagePercentage } = {
...constants.DEFAULT_BUY_QUOTE_REQUEST_OPTS,
...options,
};
+ assert.isString('assetData', assetData);
assert.isBigNumber('assetBuyAmount', assetBuyAmount);
assert.isValidPercentage('feePercentage', feePercentage);
assert.isBoolean('shouldForceOrderRefresh', shouldForceOrderRefresh);
- // we should refresh if:
- // we do not have any orders OR
- // we are forced to OR
- // we have some last refresh time AND that time was sufficiently long ago
- const shouldRefresh =
- _.isUndefined(this._currentOrdersAndFillableAmountsIfExists) ||
- shouldForceOrderRefresh ||
- (!_.isUndefined(this._lastRefreshTimeIfExists) &&
- this._lastRefreshTimeIfExists + this.orderRefreshIntervalMs < Date.now());
- let ordersAndFillableAmounts: AssetBuyerOrdersAndFillableAmounts;
- if (shouldRefresh) {
- ordersAndFillableAmounts = await this._getLatestOrdersAndFillableAmountsAsync();
- this._lastRefreshTimeIfExists = Date.now();
- this._currentOrdersAndFillableAmountsIfExists = ordersAndFillableAmounts;
- } else {
- // it is safe to cast to AssetBuyerOrdersAndFillableAmounts because shouldRefresh catches the undefined case above
- ordersAndFillableAmounts = this
- ._currentOrdersAndFillableAmountsIfExists as AssetBuyerOrdersAndFillableAmounts;
- }
+ const zrxTokenAssetData = this._getZrxTokenAssetDataOrThrow();
+ const [ordersAndFillableAmounts, feeOrdersAndFillableAmounts] = await Promise.all([
+ this._getOrdersAndFillableAmountsAsync(assetData, shouldForceOrderRefresh),
+ this._getOrdersAndFillableAmountsAsync(zrxTokenAssetData, shouldForceOrderRefresh),
+ shouldForceOrderRefresh,
+ ]);
const buyQuote = buyQuoteCalculator.calculate(
ordersAndFillableAmounts,
+ feeOrdersAndFillableAmounts,
assetBuyAmount,
feePercentage,
slippagePercentage,
@@ -179,6 +170,26 @@ export class AssetBuyer {
return buyQuote;
}
/**
+ * Get a `BuyQuote` containing all information relevant to fulfilling a buy given a desired ERC20 token address.
+ * You can then pass the `BuyQuote` to `executeBuyQuoteAsync` to execute the buy.
+ * @param tokenAddress The ERC20 token address.
+ * @param assetBuyAmount The amount of asset to buy.
+ * @param options Options for the request. See type definition for more information.
+ *
+ * @return An object that conforms to BuyQuote that satisfies the request. See type definition for more information.
+ */
+ public async getBuyQuoteForERC20TokenAddressAsync(
+ tokenAddress: string,
+ assetBuyAmount: BigNumber,
+ options: Partial<BuyQuoteRequestOpts>,
+ ): Promise<BuyQuote> {
+ assert.isETHAddressHex('tokenAddress', tokenAddress);
+ assert.isBigNumber('assetBuyAmount', assetBuyAmount);
+ const assetData = assetDataUtils.encodeERC20AssetData(tokenAddress);
+ const buyQuote = this.getBuyQuoteAsync(assetData, assetBuyAmount, options);
+ return buyQuote;
+ }
+ /**
* Given a BuyQuote and desired rate, attempt to execute the buy.
* @param buyQuote An object that conforms to BuyQuote. See type definition for more information.
* @param options Options for the execution of the BuyQuote. See type definition for more information.
@@ -229,39 +240,54 @@ export class AssetBuyer {
return txHash;
}
/**
- * Ask the order Provider for orders and process them.
+ * Grab orders from the cache, if there is a miss or it is time to refresh, fetch and process the orders
*/
- private async _getLatestOrdersAndFillableAmountsAsync(): Promise<AssetBuyerOrdersAndFillableAmounts> {
- const etherTokenAssetData = this._getEtherTokenAssetDataOrThrow();
- const zrxTokenAssetData = this._getZrxTokenAssetDataOrThrow();
- // construct order Provider requests
- const targetOrderProviderRequest = {
- makerAssetData: this.assetData,
- takerAssetData: etherTokenAssetData,
- networkId: this.networkId,
- };
- const feeOrderProviderRequest = {
- makerAssetData: zrxTokenAssetData,
- takerAssetData: etherTokenAssetData,
- networkId: this.networkId,
- };
- const requests = [targetOrderProviderRequest, feeOrderProviderRequest];
- // fetch orders and possible fillable amounts
- const [targetOrderProviderResponse, feeOrderProviderResponse] = await Promise.all(
- _.map(requests, async request => this.orderProvider.getOrdersAsync(request)),
- );
- // since the order provider is an injected dependency, validate that it respects the API
- // ie. it should only return maker/taker assetDatas that are specified
- orderProviderResponseProcessor.throwIfInvalidResponse(targetOrderProviderResponse, targetOrderProviderRequest);
- orderProviderResponseProcessor.throwIfInvalidResponse(feeOrderProviderResponse, feeOrderProviderRequest);
- // process the responses into one object
- const ordersAndFillableAmounts = await orderProviderResponseProcessor.processAsync(
- targetOrderProviderResponse,
- feeOrderProviderResponse,
- zrxTokenAssetData,
- this.expiryBufferSeconds,
- this._contractWrappers.orderValidator,
- );
+ private async _getOrdersAndFillableAmountsAsync(
+ assetData: string,
+ shouldForceOrderRefresh: boolean,
+ ): Promise<OrdersAndFillableAmounts> {
+ // we should refresh if:
+ // we do not have any orders OR
+ // we are forced to OR
+ // we have some last refresh time AND that time was sufficiently long ago
+ const ordersEntryIfExists = this._ordersEntryMap[assetData];
+ const shouldRefresh =
+ _.isUndefined(ordersEntryIfExists) ||
+ shouldForceOrderRefresh ||
+ ordersEntryIfExists.lastRefreshTime + this.orderRefreshIntervalMs < Date.now();
+ let ordersAndFillableAmounts: OrdersAndFillableAmounts;
+ if (shouldRefresh) {
+ const etherTokenAssetData = this._getEtherTokenAssetDataOrThrow();
+ const zrxTokenAssetData = this._getZrxTokenAssetDataOrThrow();
+ // construct orderProvider request
+ const orderProviderRequest = {
+ makerAssetData: assetData,
+ takerAssetData: etherTokenAssetData,
+ networkId: this.networkId,
+ };
+ const request = orderProviderRequest;
+ // get provider response
+ const response = await this.orderProvider.getOrdersAsync(request);
+ // since the order provider is an injected dependency, validate that it respects the API
+ // ie. it should only return maker/taker assetDatas that are specified
+ orderProviderResponseProcessor.throwIfInvalidResponse(response, request);
+ // process the responses into one object
+ const isMakerAssetZrxToken = assetData === zrxTokenAssetData;
+ ordersAndFillableAmounts = await orderProviderResponseProcessor.processAsync(
+ response,
+ isMakerAssetZrxToken,
+ this.expiryBufferSeconds,
+ this._contractWrappers.orderValidator,
+ );
+ const lastRefreshTime = Date.now();
+ const updatedOrdersEntry = {
+ ordersAndFillableAmounts,
+ lastRefreshTime,
+ };
+ this._ordersEntryMap[assetData] = updatedOrdersEntry;
+ } else {
+ ordersAndFillableAmounts = ordersEntryIfExists.ordersAndFillableAmounts;
+ }
return ordersAndFillableAmounts;
}
/**
diff --git a/packages/asset-buyer/src/asset_buyer_manager.ts b/packages/asset-buyer/src/asset_buyer_manager.ts
deleted file mode 100644
index 1bde55eff..000000000
--- a/packages/asset-buyer/src/asset_buyer_manager.ts
+++ /dev/null
@@ -1,171 +0,0 @@
-import { HttpClient } from '@0xproject/connect';
-import { ContractWrappers } from '@0xproject/contract-wrappers';
-import { SignedOrder } from '@0xproject/order-utils';
-import { ObjectMap } from '@0xproject/types';
-import { BigNumber } from '@0xproject/utils';
-import { Provider } from 'ethereum-types';
-import * as _ from 'lodash';
-
-import { AssetBuyer } from './asset_buyer';
-import { constants } from './constants';
-import { BasicOrderProvider } from './order_providers/basic_order_provider';
-import { StandardRelayerAPIOrderProvider } from './order_providers/standard_relayer_api_order_provider';
-import { assert } from './utils/assert';
-import { assetDataUtils } from './utils/asset_data_utils';
-
-import {
- AssetBuyerManagerError,
- AssetBuyerOpts,
- BuyQuote,
- BuyQuoteExecutionOpts,
- BuyQuoteRequestOpts,
- OrderProvider,
-} from './types';
-
-export class AssetBuyerManager {
- // Map of assetData to AssetBuyer for that assetData
- private readonly _assetBuyerMap: ObjectMap<AssetBuyer>;
- /**
- * Returns an array of all assetDatas available at the provided sraApiUrl
- * @param sraApiUrl The standard relayer API base HTTP url you would like to source orders from.
- * @param pairedWithAssetData Optional filter argument to return assetDatas that only pair with this assetData value.
- *
- * @return An array of all assetDatas available at the provider sraApiUrl
- */
- public static async getAllAvailableAssetDatasAsync(
- sraApiUrl: string,
- pairedWithAssetData?: string,
- ): Promise<string[]> {
- const client = new HttpClient(sraApiUrl);
- const params = {
- assetDataA: pairedWithAssetData,
- perPage: constants.MAX_PER_PAGE,
- };
- const assetPairsResponse = await client.getAssetPairsAsync(params);
- return _.uniq(_.map(assetPairsResponse.records, pairsItem => pairsItem.assetDataB.assetData));
- }
- /**
- * Instantiates a new AssetBuyerManager 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 options Initialization options for an AssetBuyer. See type definition for details.
- *
- * @return An promise of an instance of AssetBuyerManager
- */
- public static async getAssetBuyerManagerFromStandardRelayerApiAsync(
- provider: Provider,
- sraApiUrl: string,
- options: Partial<AssetBuyerOpts>,
- ): Promise<AssetBuyerManager> {
- const networkId = options.networkId || constants.MAINNET_NETWORK_ID;
- const contractWrappers = new ContractWrappers(provider, { networkId });
- const etherTokenAssetData = assetDataUtils.getEtherTokenAssetDataOrThrow(contractWrappers);
- const assetDatas = await AssetBuyerManager.getAllAvailableAssetDatasAsync(sraApiUrl, etherTokenAssetData);
- const orderProvider = new StandardRelayerAPIOrderProvider(sraApiUrl);
- return new AssetBuyerManager(provider, assetDatas, orderProvider, options);
- }
- /**
- * Instantiates a new AssetBuyerManager instance given existing liquidity in the form of orders and feeOrders.
- * @param provider The Provider instance you would like to use for interacting with the Ethereum network.
- * @param orders A non-empty array of objects that conform to SignedOrder. All orders must have the same makerAssetData and takerAssetData (WETH).
- * @param feeOrders A array of objects that conform to SignedOrder. All orders must have the same makerAssetData (ZRX) and takerAssetData (WETH). Defaults to an empty array.
- * @param options Initialization options for an AssetBuyer. See type definition for details.
- *
- * @return An instance of AssetBuyerManager
- */
- public static getAssetBuyerManagerFromProvidedOrders(
- provider: Provider,
- orders: SignedOrder[],
- feeOrders: SignedOrder[] = [],
- options: Partial<AssetBuyerOpts>,
- ): AssetBuyerManager {
- const assetDatas = _.map(orders, order => order.makerAssetData);
- const orderProvider = new BasicOrderProvider(_.concat(orders, feeOrders));
- return new AssetBuyerManager(provider, assetDatas, orderProvider, options);
- }
- /**
- * Instantiates a new AssetBuyerManager 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 orderProvider An object that conforms to OrderProvider, see type for definition.
- * @param options Initialization options for an AssetBuyer. See type definition for details.
- *
- * @return An instance of AssetBuyerManager
- */
- constructor(
- provider: Provider,
- assetDatas: string[],
- orderProvider: OrderProvider,
- options: Partial<AssetBuyerOpts>,
- ) {
- assert.assert(assetDatas.length > 0, `Expected 'assetDatas' to be a non-empty array.`);
- this._assetBuyerMap = _.reduce(
- assetDatas,
- (accAssetBuyerMap: ObjectMap<AssetBuyer>, assetData: string) => {
- accAssetBuyerMap[assetData] = new AssetBuyer(provider, assetData, orderProvider, options);
- return accAssetBuyerMap;
- },
- {},
- );
- }
- /**
- * Get an AssetBuyer for the provided assetData
- * @param assetData The desired assetData.
- *
- * @return An instance of AssetBuyer
- */
- public getAssetBuyerFromAssetData(assetData: string): AssetBuyer {
- const assetBuyer = this._assetBuyerMap[assetData];
- if (_.isUndefined(assetBuyer)) {
- throw new Error(`${AssetBuyerManagerError.AssetBuyerNotFound}: For assetData ${assetData}`);
- }
- return assetBuyer;
- }
- /**
- * Get an AssetBuyer for the provided ERC20 tokenAddress
- * @param tokenAddress The desired tokenAddress.
- *
- * @return An instance of AssetBuyer
- */
- public getAssetBuyerFromERC20TokenAddress(tokenAddress: string): AssetBuyer {
- const assetData = assetDataUtils.encodeERC20AssetData(tokenAddress);
- return this.getAssetBuyerFromAssetData(assetData);
- }
- /**
- * Get a list of all the assetDatas that the instance supports
- *
- * @return An array of assetData strings
- */
- public getAssetDatas(): string[] {
- return _.keys(this._assetBuyerMap);
- }
- /**
- * Get a `BuyQuote` containing all information relevant to fulfilling a buy.
- * You can then pass the `BuyQuote` to `executeBuyQuoteAsync` to execute the buy.
- *
- * @param assetData The assetData that identifies the desired asset to buy.
- * @param assetBuyAmount The amount of asset to buy.
- * @param options Options for the execution of the BuyQuote. See type definition for more information.
- *
- * @return An object that conforms to BuyQuote that satisfies the request. See type definition for more information.
- */
- public async getBuyQuoteAsync(
- assetData: string,
- assetBuyAmount: BigNumber,
- options: Partial<BuyQuoteRequestOpts>,
- ): Promise<BuyQuote> {
- return this.getAssetBuyerFromAssetData(assetData).getBuyQuoteAsync(assetBuyAmount, options);
- }
- /**
- * Given a BuyQuote and desired rate, attempt to execute the buy.
- * @param buyQuote An object that conforms to BuyQuote. See type definition for more information.
- * @param rate The desired rate to execute the buy at. Affects the amount of ETH sent with the transaction, defaults to buyQuote.maxRate.
- * @param takerAddress The address to perform the buy. Defaults to the first available address from the provider.
- * @param feeRecipient The address where affiliate fees are sent. Defaults to null address (0x000...000).
- *
- * @return A promise of the txHash.
- */
- public async executeBuyQuoteAsync(buyQuote: BuyQuote, options: Partial<BuyQuoteExecutionOpts>): Promise<string> {
- return this.getAssetBuyerFromAssetData(buyQuote.assetData).executeBuyQuoteAsync(buyQuote, options);
- }
-}
diff --git a/packages/asset-buyer/src/types.ts b/packages/asset-buyer/src/types.ts
index 67baa51c7..074bcd453 100644
--- a/packages/asset-buyer/src/types.ts
+++ b/packages/asset-buyer/src/types.ts
@@ -105,9 +105,7 @@ export enum AssetBuyerManagerError {
AssetBuyerNotFound = 'ASSET_BUYER_NOT_FOUND',
}
-export interface AssetBuyerOrdersAndFillableAmounts {
+export interface OrdersAndFillableAmounts {
orders: SignedOrder[];
- feeOrders: SignedOrder[];
remainingFillableMakerAssetAmounts: BigNumber[];
- remainingFillableFeeAmounts: BigNumber[];
}
diff --git a/packages/asset-buyer/src/utils/buy_quote_calculator.ts b/packages/asset-buyer/src/utils/buy_quote_calculator.ts
index 9946924ef..b706ea143 100644
--- a/packages/asset-buyer/src/utils/buy_quote_calculator.ts
+++ b/packages/asset-buyer/src/utils/buy_quote_calculator.ts
@@ -3,24 +3,23 @@ import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';
import { constants } from '../constants';
-import { AssetBuyerError, AssetBuyerOrdersAndFillableAmounts, BuyQuote } from '../types';
+import { AssetBuyerError, BuyQuote, OrdersAndFillableAmounts } from '../types';
import { orderUtils } from './order_utils';
// Calculates a buy quote for orders that have WETH as the takerAsset
export const buyQuoteCalculator = {
calculate(
- ordersAndFillableAmounts: AssetBuyerOrdersAndFillableAmounts,
+ ordersAndFillableAmounts: OrdersAndFillableAmounts,
+ feeOrdersAndFillableAmounts: OrdersAndFillableAmounts,
assetBuyAmount: BigNumber,
feePercentage: number,
slippagePercentage: number,
): BuyQuote {
- const {
- orders,
- feeOrders,
- remainingFillableMakerAssetAmounts,
- remainingFillableFeeAmounts,
- } = ordersAndFillableAmounts;
+ const orders = ordersAndFillableAmounts.orders;
+ const remainingFillableMakerAssetAmounts = ordersAndFillableAmounts.remainingFillableMakerAssetAmounts;
+ const feeOrders = feeOrdersAndFillableAmounts.orders;
+ const remainingFillableFeeAmounts = feeOrdersAndFillableAmounts.remainingFillableMakerAssetAmounts;
const slippageBufferAmount = assetBuyAmount.mul(slippagePercentage).round();
const {
resultOrders,
diff --git a/packages/asset-buyer/src/utils/order_provider_response_processor.ts b/packages/asset-buyer/src/utils/order_provider_response_processor.ts
index 31fdcc182..74eec162d 100644
--- a/packages/asset-buyer/src/utils/order_provider_response_processor.ts
+++ b/packages/asset-buyer/src/utils/order_provider_response_processor.ts
@@ -8,19 +8,14 @@ import * as _ from 'lodash';
import { constants } from '../constants';
import {
AssetBuyerError,
- AssetBuyerOrdersAndFillableAmounts,
OrderProviderRequest,
OrderProviderResponse,
+ OrdersAndFillableAmounts,
SignedOrderWithRemainingFillableMakerAssetAmount,
} from '../types';
import { orderUtils } from './order_utils';
-interface OrdersAndRemainingFillableMakerAssetAmounts {
- orders: SignedOrder[];
- remainingFillableMakerAssetAmounts: BigNumber[];
-}
-
export const orderProviderResponseProcessor = {
throwIfInvalidResponse(response: OrderProviderResponse, request: OrderProviderRequest): void {
const { makerAssetData, takerAssetData } = request;
@@ -38,65 +33,40 @@ export const orderProviderResponseProcessor = {
* - Sort by rate
*/
async processAsync(
- targetOrderProviderResponse: OrderProviderResponse,
- feeOrderProviderResponse: OrderProviderResponse,
- zrxTokenAssetData: string,
+ orderProviderResponse: OrderProviderResponse,
+ isMakerAssetZrxToken: boolean,
expiryBufferSeconds: number,
orderValidator?: OrderValidatorWrapper,
- ): Promise<AssetBuyerOrdersAndFillableAmounts> {
+ ): Promise<OrdersAndFillableAmounts> {
// drop orders that are expired or not open
- const filteredTargetOrders = filterOutExpiredAndNonOpenOrders(
- targetOrderProviderResponse.orders,
- expiryBufferSeconds,
- );
- const filteredFeeOrders = filterOutExpiredAndNonOpenOrders(
- feeOrderProviderResponse.orders,
- expiryBufferSeconds,
- );
+ const filteredOrders = filterOutExpiredAndNonOpenOrders(orderProviderResponse.orders, expiryBufferSeconds);
// set the orders to be sorted equal to the filtered orders
- let unsortedTargetOrders = filteredTargetOrders;
- let unsortedFeeOrders = filteredFeeOrders;
+ let unsortedOrders = filteredOrders;
// if an orderValidator is provided, use on chain information to calculate remaining fillable makerAsset amounts
if (!_.isUndefined(orderValidator)) {
// TODO(bmillman): improvement
- // try/catch these requests and throw a more domain specific error
- // TODO(bmillman): optimization
- // reduce this to once RPC call buy combining orders into one array and then splitting up the response
- const [targetOrdersAndTradersInfo, feeOrdersAndTradersInfo] = await Promise.all(
- _.map([filteredTargetOrders, filteredFeeOrders], ordersToBeValidated => {
- const takerAddresses = _.map(ordersToBeValidated, () => constants.NULL_ADDRESS);
- return orderValidator.getOrdersAndTradersInfoAsync(ordersToBeValidated, takerAddresses);
- }),
- );
- // take orders + on chain information and find the valid orders and remaining fillable maker asset amounts
- unsortedTargetOrders = getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain(
- filteredTargetOrders,
- targetOrdersAndTradersInfo,
- zrxTokenAssetData,
+ // try/catch this request and throw a more domain specific error
+ const takerAddresses = _.map(filteredOrders, () => constants.NULL_ADDRESS);
+ const ordersAndTradersInfo = await orderValidator.getOrdersAndTradersInfoAsync(
+ filteredOrders,
+ takerAddresses,
);
// take orders + on chain information and find the valid orders and remaining fillable maker asset amounts
- unsortedFeeOrders = getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain(
- filteredFeeOrders,
- feeOrdersAndTradersInfo,
- zrxTokenAssetData,
+ unsortedOrders = getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain(
+ filteredOrders,
+ ordersAndTradersInfo,
+ isMakerAssetZrxToken,
);
}
// sort orders by rate
// TODO(bmillman): optimization
// provide a feeRate to the sorting function to more accurately sort based on the current market for ZRX tokens
- const sortedTargetOrders = sortingUtils.sortOrdersByFeeAdjustedRate(unsortedTargetOrders);
- const sortedFeeOrders = sortingUtils.sortFeeOrdersByFeeAdjustedRate(unsortedFeeOrders);
+ const sortedOrders = isMakerAssetZrxToken
+ ? sortingUtils.sortFeeOrdersByFeeAdjustedRate(unsortedOrders)
+ : sortingUtils.sortOrdersByFeeAdjustedRate(unsortedOrders);
// unbundle orders and fillable amounts and compile final result
- const targetOrdersAndRemainingFillableMakerAssetAmounts = unbundleOrdersWithAmounts(sortedTargetOrders);
- const feeOrdersAndRemainingFillableMakerAssetAmounts = unbundleOrdersWithAmounts(sortedFeeOrders);
- return {
- orders: targetOrdersAndRemainingFillableMakerAssetAmounts.orders,
- feeOrders: feeOrdersAndRemainingFillableMakerAssetAmounts.orders,
- remainingFillableMakerAssetAmounts:
- targetOrdersAndRemainingFillableMakerAssetAmounts.remainingFillableMakerAssetAmounts,
- remainingFillableFeeAmounts:
- feeOrdersAndRemainingFillableMakerAssetAmounts.remainingFillableMakerAssetAmounts,
- };
+ const result = unbundleOrdersWithAmounts(sortedOrders);
+ return result;
},
};
@@ -120,7 +90,7 @@ function filterOutExpiredAndNonOpenOrders(
function getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain(
inputOrders: SignedOrder[],
ordersAndTradersInfo: OrderAndTraderInfo[],
- zrxAssetData: string,
+ isMakerAssetZrxToken: boolean,
): SignedOrderWithRemainingFillableMakerAssetAmount[] {
// iterate through the input orders and find the ones that are still fillable
// for the orders that are still fillable, calculate the remaining fillable maker asset amount
@@ -147,7 +117,7 @@ function getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain(
const remainingFillableCalculator = new RemainingFillableCalculator(
order.makerFee,
order.makerAssetAmount,
- order.makerAssetData === zrxAssetData,
+ isMakerAssetZrxToken,
transferrableAssetAmount,
transferrableFeeAssetAmount,
remainingMakerAssetAmount,
@@ -175,7 +145,7 @@ function getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain(
*/
function unbundleOrdersWithAmounts(
ordersWithAmounts: SignedOrderWithRemainingFillableMakerAssetAmount[],
-): OrdersAndRemainingFillableMakerAssetAmounts {
+): OrdersAndFillableAmounts {
const result = _.reduce(
ordersWithAmounts,
(acc, orderWithAmount) => {