aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/src/order_providers
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-09-26 20:55:52 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-09-26 20:55:52 +0800
commit5d73eebf6abe52763ea6984f85102157abea5b6c (patch)
tree8cec1c58fe22ba62d979dbc1c6ff366e81985b8d /packages/asset-buyer/src/order_providers
parentf3deabccf4e6caec57351a09f82b3f786122b5ea (diff)
parent13aa98f0f3431e4ea4db07794a06304c237e8d45 (diff)
downloaddexon-sol-tools-5d73eebf6abe52763ea6984f85102157abea5b6c.tar
dexon-sol-tools-5d73eebf6abe52763ea6984f85102157abea5b6c.tar.gz
dexon-sol-tools-5d73eebf6abe52763ea6984f85102157abea5b6c.tar.bz2
dexon-sol-tools-5d73eebf6abe52763ea6984f85102157abea5b6c.tar.lz
dexon-sol-tools-5d73eebf6abe52763ea6984f85102157abea5b6c.tar.xz
dexon-sol-tools-5d73eebf6abe52763ea6984f85102157abea5b6c.tar.zst
dexon-sol-tools-5d73eebf6abe52763ea6984f85102157abea5b6c.zip
Merge branch 'development' into feature/ts-ethers
Diffstat (limited to 'packages/asset-buyer/src/order_providers')
-rw-r--r--packages/asset-buyer/src/order_providers/basic_order_provider.ts32
-rw-r--r--packages/asset-buyer/src/order_providers/standard_relayer_api_order_provider.ts79
2 files changed, 111 insertions, 0 deletions
diff --git a/packages/asset-buyer/src/order_providers/basic_order_provider.ts b/packages/asset-buyer/src/order_providers/basic_order_provider.ts
new file mode 100644
index 000000000..9bb2d90ac
--- /dev/null
+++ b/packages/asset-buyer/src/order_providers/basic_order_provider.ts
@@ -0,0 +1,32 @@
+import { schemas } from '@0xproject/json-schemas';
+import { SignedOrder } from '@0xproject/types';
+import * as _ from 'lodash';
+
+import { OrderProvider, OrderProviderRequest, OrderProviderResponse } from '../types';
+import { assert } from '../utils/assert';
+
+export class BasicOrderProvider implements OrderProvider {
+ public readonly orders: SignedOrder[];
+ /**
+ * Instantiates a new BasicOrderProvider instance
+ * @param orders An array of objects that conform to SignedOrder to fetch from.
+ * @return An instance of BasicOrderProvider
+ */
+ constructor(orders: SignedOrder[]) {
+ assert.doesConformToSchema('orders', orders, schemas.signedOrdersSchema);
+ this.orders = orders;
+ }
+ /**
+ * Given an object that conforms to OrderFetcherRequest, return the corresponding OrderProviderResponse that satisfies the request.
+ * @param orderProviderRequest An instance of OrderFetcherRequest. 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 } = orderProviderRequest;
+ const orders = _.filter(this.orders, order => {
+ return order.makerAssetData === makerAssetData && order.takerAssetData === takerAssetData;
+ });
+ return { orders };
+ }
+}
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..31942c25b
--- /dev/null
+++ b/packages/asset-buyer/src/order_providers/standard_relayer_api_order_provider.ts
@@ -0,0 +1,79 @@
+import { HttpClient } from '@0xproject/connect';
+import { APIOrder, OrderbookResponse } from '@0xproject/types';
+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,
+ };
+ }
+}