diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-09-21 21:58:04 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-09-21 21:58:04 +0800 |
commit | c64dcec772d8f5bfa7a32c5e60bfe808229d4d5e (patch) | |
tree | c1fe70d472f4e8df391d2a1f686beb835b03a2b0 /packages/asset-buyer/src/order_providers/standard_relayer_api_order_provider.ts | |
parent | 8dea48ebeff775f7437c45f5a6d39dfc7dbf704e (diff) | |
download | dexon-sol-tools-c64dcec772d8f5bfa7a32c5e60bfe808229d4d5e.tar dexon-sol-tools-c64dcec772d8f5bfa7a32c5e60bfe808229d4d5e.tar.gz dexon-sol-tools-c64dcec772d8f5bfa7a32c5e60bfe808229d4d5e.tar.bz2 dexon-sol-tools-c64dcec772d8f5bfa7a32c5e60bfe808229d4d5e.tar.lz dexon-sol-tools-c64dcec772d8f5bfa7a32c5e60bfe808229d4d5e.tar.xz dexon-sol-tools-c64dcec772d8f5bfa7a32c5e60bfe808229d4d5e.tar.zst dexon-sol-tools-c64dcec772d8f5bfa7a32c5e60bfe808229d4d5e.zip |
More renaming
Diffstat (limited to 'packages/asset-buyer/src/order_providers/standard_relayer_api_order_provider.ts')
-rw-r--r-- | packages/asset-buyer/src/order_providers/standard_relayer_api_order_provider.ts | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/packages/asset-buyer/src/order_providers/standard_relayer_api_order_provider.ts b/packages/asset-buyer/src/order_providers/standard_relayer_api_order_provider.ts new file mode 100644 index 000000000..be3e9da50 --- /dev/null +++ b/packages/asset-buyer/src/order_providers/standard_relayer_api_order_provider.ts @@ -0,0 +1,78 @@ +import { APIOrder, HttpClient, OrderbookResponse } from '@0xproject/connect'; +import * as _ from 'lodash'; + +import { + AssetBuyerError, + OrderProvider, + OrderProviderRequest, + OrderProviderResponse, + SignedOrderWithRemainingFillableMakerAssetAmount, +} from '../types'; +import { assert } from '../utils/assert'; +import { orderUtils } from '../utils/order_utils'; + +export class StandardRelayerAPIOrderProvider implements OrderProvider { + public readonly apiUrl: string; + private readonly _sraClient: HttpClient; + /** + * Given an array of APIOrder objects from a standard relayer api, return an array + * of SignedOrderWithRemainingFillableMakerAssetAmounts + */ + private static _getSignedOrderWithRemainingFillableMakerAssetAmountFromApi( + apiOrders: APIOrder[], + ): SignedOrderWithRemainingFillableMakerAssetAmount[] { + const result = _.map(apiOrders, apiOrder => { + const { order, metaData } = apiOrder; + // calculate remainingFillableMakerAssetAmount from api metadata, else assume order is completely fillable + const remainingFillableTakerAssetAmount = _.get( + metaData, + 'remainingTakerAssetAmount', + order.takerAssetAmount, + ); + const remainingFillableMakerAssetAmount = orderUtils.calculateRemainingMakerAssetAmount( + order, + remainingFillableTakerAssetAmount, + ); + const newOrder = { + ...order, + remainingFillableMakerAssetAmount, + }; + return newOrder; + }); + return result; + } + /** + * 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 StandardRelayerAPIOrderProvider + */ + constructor(apiUrl: string) { + assert.isWebUri('apiUrl', apiUrl); + this.apiUrl = apiUrl; + this._sraClient = new HttpClient(apiUrl); + } + /** + * 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(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; + try { + orderbook = await this._sraClient.getOrderbookAsync(orderbookRequest, requestOpts); + } catch (err) { + throw new Error(AssetBuyerError.StandardRelayerApiError); + } + const apiOrders = orderbook.asks.records; + const orders = StandardRelayerAPIOrderProvider._getSignedOrderWithRemainingFillableMakerAssetAmountFromApi( + apiOrders, + ); + return { + orders, + }; + } +} |