diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/asset-buyer/src/asset_buyer_manager.ts (renamed from packages/asset-buyer/src/standard_relayer_api_asset_buyer_manager.ts) | 75 | ||||
-rw-r--r-- | packages/asset-buyer/src/index.ts | 4 | ||||
-rw-r--r-- | packages/asset-buyer/src/types.ts | 4 |
3 files changed, 58 insertions, 25 deletions
diff --git a/packages/asset-buyer/src/standard_relayer_api_asset_buyer_manager.ts b/packages/asset-buyer/src/asset_buyer_manager.ts index 947c738a1..3f96a79bb 100644 --- a/packages/asset-buyer/src/standard_relayer_api_asset_buyer_manager.ts +++ b/packages/asset-buyer/src/asset_buyer_manager.ts @@ -1,17 +1,20 @@ import { HttpClient } from '@0xproject/connect'; import { ContractWrappers } from '@0xproject/contract-wrappers'; +import { SignedOrder } from '@0xproject/order-utils'; import { ObjectMap } from '@0xproject/types'; 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 { OrderProvider, StandardRelayerApiAssetBuyerManagerError } from './types'; +import { AssetBuyerManagerError, OrderProvider } from './types'; -export class StandardRelayerAPIAssetBuyerManager { +export class AssetBuyerManager { // Map of assetData to AssetBuyer for that assetData private readonly _assetBuyerMap: ObjectMap<AssetBuyer>; /** @@ -34,45 +37,75 @@ export class StandardRelayerAPIAssetBuyerManager { return _.uniq(_.map(assetPairsResponse.records, pairsItem => pairsItem.assetDataB.assetData)); } /** - * Instantiates a new StandardRelayerAPIAssetBuyerManager instance with all available assetDatas at the provided sraApiUrl + * 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 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). - * @return An promise of an instance of StandardRelayerAPIAssetBuyerManager + * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states. Defaults to 10000ms (10s). + * @param expiryBufferSeconds The number of seconds to add when calculating whether an order is expired or not. Defaults to 15s. + * + * @return An promise of an instance of AssetBuyerManager */ - public static async getAssetBuyerManagerWithAllAvailableAssetDatasAsync( + public static async getAssetBuyerManagerFromStandardRelayerApiAsync( provider: Provider, sraApiUrl: string, - orderProvider: OrderProvider, networkId: number = constants.MAINNET_NETWORK_ID, orderRefreshIntervalMs?: number, - ): Promise<StandardRelayerAPIAssetBuyerManager> { + expiryBufferSeconds?: number, + ): Promise<AssetBuyerManager> { const contractWrappers = new ContractWrappers(provider, { networkId }); const etherTokenAssetData = assetDataUtils.getEtherTokenAssetDataOrThrow(contractWrappers); - const assetDatas = await StandardRelayerAPIAssetBuyerManager.getAllAvailableAssetDatasAsync( - sraApiUrl, - etherTokenAssetData, + const assetDatas = await AssetBuyerManager.getAllAvailableAssetDatasAsync(sraApiUrl, etherTokenAssetData); + const orderProvider = new StandardRelayerAPIOrderProvider(sraApiUrl); + return new AssetBuyerManager( + provider, + assetDatas, + orderProvider, + networkId, + orderRefreshIntervalMs, + expiryBufferSeconds, ); - return new StandardRelayerAPIAssetBuyerManager( + } + /** + * 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 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). + * @param expiryBufferSeconds The number of seconds to add when calculating whether an order is expired or not. Defaults to 15s. + * + * @return An instance of AssetBuyerManager + */ + public static getAssetBuyerManagerFromProvidedOrders( + provider: Provider, + orders: SignedOrder[], + feeOrders: SignedOrder[] = [], + networkId?: number, + orderRefreshIntervalMs?: number, + expiryBufferSeconds?: number, + ): AssetBuyerManager { + const assetDatas = _.map(orders, order => order.makerAssetData); + const orderProvider = new BasicOrderProvider(_.concat(orders, feeOrders)); + return new AssetBuyerManager( provider, assetDatas, orderProvider, networkId, orderRefreshIntervalMs, + expiryBufferSeconds, ); } /** - * Instantiates a new StandardRelayerAPIAssetBuyerManager instance + * 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 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). - * @return An instance of StandardRelayerAPIAssetBuyerManager + * @param orderRefreshIntervalMs The interval in ms that getBuyQuoteAsync should trigger an refresh of orders and order states. Defaults to 10000ms (10s). + * @param expiryBufferSeconds The number of seconds to add when calculating whether an order is expired or not. Defaults to 15s. + * + * @return An instance of AssetBuyerManager */ constructor( provider: Provider, @@ -80,6 +113,7 @@ export class StandardRelayerAPIAssetBuyerManager { orderProvider: OrderProvider, networkId?: number, orderRefreshIntervalMs?: number, + expiryBufferSeconds?: number, ) { assert.assert(assetDatas.length > 0, `Expected 'assetDatas' to be a non-empty array.`); this._assetBuyerMap = _.reduce( @@ -91,6 +125,7 @@ export class StandardRelayerAPIAssetBuyerManager { orderProvider, networkId, orderRefreshIntervalMs, + expiryBufferSeconds, ); return accAssetBuyerMap; }, @@ -106,9 +141,7 @@ export class StandardRelayerAPIAssetBuyerManager { public getAssetBuyerFromAssetData(assetData: string): AssetBuyer { const assetBuyer = this._assetBuyerMap[assetData]; if (_.isUndefined(assetBuyer)) { - throw new Error( - `${StandardRelayerApiAssetBuyerManagerError.AssetBuyerNotFound}: For assetData ${assetData}`, - ); + throw new Error(`${AssetBuyerManagerError.AssetBuyerNotFound}: For assetData ${assetData}`); } return assetBuyer; } diff --git a/packages/asset-buyer/src/index.ts b/packages/asset-buyer/src/index.ts index 8ef529ac0..cedb3bbf4 100644 --- a/packages/asset-buyer/src/index.ts +++ b/packages/asset-buyer/src/index.ts @@ -5,7 +5,7 @@ export { BigNumber } from '@0xproject/utils'; export { AssetBuyer } from './asset_buyer'; 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 { AssetBuyerManager } from './asset_buyer_manager'; export { AssetBuyerError, BuyQuote, @@ -13,5 +13,5 @@ export { OrderProviderRequest, OrderProviderResponse, SignedOrderWithRemainingFillableMakerAssetAmount, - StandardRelayerApiAssetBuyerManagerError, + AssetBuyerManagerError, } from './types'; diff --git a/packages/asset-buyer/src/types.ts b/packages/asset-buyer/src/types.ts index ee6858525..141193b7b 100644 --- a/packages/asset-buyer/src/types.ts +++ b/packages/asset-buyer/src/types.ts @@ -72,9 +72,9 @@ export enum AssetBuyerError { } /** - * Possible errors thrown by an StandardRelayerApiAssetBuyerManager instance or associated static methods. + * Possible errors thrown by an AssetBuyerManager instance or associated static methods. */ -export enum StandardRelayerApiAssetBuyerManagerError { +export enum AssetBuyerManagerError { AssetBuyerNotFound = 'ASSET_BUYER_NOT_FOUND', } |