aboutsummaryrefslogtreecommitdiffstats
path: root/packages/pipeline/src/parsers/paradex_orders/index.ts
diff options
context:
space:
mode:
authorzkao <zichongkao@gmail.com>2018-12-05 05:21:46 +0800
committerAlex Browne <stephenalexbrowne@gmail.com>2018-12-05 06:25:42 +0800
commit87ffa5d7ab19d2288bf68131a7e7ec77578c564c (patch)
tree68bb07481756ba578a89303f33d5c0ce8656f3a4 /packages/pipeline/src/parsers/paradex_orders/index.ts
parent7198b441e0d85785eec7244dd60bcd92269d954e (diff)
downloaddexon-sol-tools-87ffa5d7ab19d2288bf68131a7e7ec77578c564c.tar
dexon-sol-tools-87ffa5d7ab19d2288bf68131a7e7ec77578c564c.tar.gz
dexon-sol-tools-87ffa5d7ab19d2288bf68131a7e7ec77578c564c.tar.bz2
dexon-sol-tools-87ffa5d7ab19d2288bf68131a7e7ec77578c564c.tar.lz
dexon-sol-tools-87ffa5d7ab19d2288bf68131a7e7ec77578c564c.tar.xz
dexon-sol-tools-87ffa5d7ab19d2288bf68131a7e7ec77578c564c.tar.zst
dexon-sol-tools-87ffa5d7ab19d2288bf68131a7e7ec77578c564c.zip
Token_orderbook_snapshots for Ddex and Paradex(#1354)
* Implements the TokenOrderbookSnapshot Table * Scripts, Data Sources and Entities to pull Ddex and Paradex API data.
Diffstat (limited to 'packages/pipeline/src/parsers/paradex_orders/index.ts')
-rw-r--r--packages/pipeline/src/parsers/paradex_orders/index.ts66
1 files changed, 66 insertions, 0 deletions
diff --git a/packages/pipeline/src/parsers/paradex_orders/index.ts b/packages/pipeline/src/parsers/paradex_orders/index.ts
new file mode 100644
index 000000000..7966658a7
--- /dev/null
+++ b/packages/pipeline/src/parsers/paradex_orders/index.ts
@@ -0,0 +1,66 @@
+import { BigNumber } from '@0x/utils';
+
+import { ParadexMarket, ParadexOrder, ParadexOrderbookResponse } from '../../data_sources/paradex';
+import { TokenOrderbookSnapshot as TokenOrder } from '../../entities';
+import { OrderType } from '../../types';
+
+/**
+ * Marque function of this file.
+ * 1) Takes in orders from an orderbook (orders are already aggregated by price point),
+ * 2) For each aggregated order, forms a TokenOrder entity with market data and
+ * other information attached.
+ * @param paradexOrderbookResponse An orderbook response from the Paradex API.
+ * @param paradexMarket An object containing market data also directly from the API.
+ * @param observedTimestamp Time at which the orders for the market were pulled.
+ * @param source The exchange where these orders are placed. In this case 'paradex'.
+ */
+export function parseParadexOrders(
+ paradexOrderbookResponse: ParadexOrderbookResponse,
+ paradexMarket: ParadexMarket,
+ observedTimestamp: number,
+ source: string,
+): TokenOrder[] {
+ const parsedBids = paradexOrderbookResponse.bids.map(order =>
+ parseParadexOrder(paradexMarket, observedTimestamp, 'bid', source, order),
+ );
+ const parsedAsks = paradexOrderbookResponse.asks.map(order =>
+ parseParadexOrder(paradexMarket, observedTimestamp, 'ask', source, order),
+ );
+ return parsedBids.concat(parsedAsks);
+}
+
+/**
+ * Parse a single aggregated Ddex order in order to form a tokenOrder entity
+ * which can be saved into the database.
+ * @param paradexMarket An object containing information about the market where these
+ * orders have been placed.
+ * @param observedTimestamp The time when the API response returned back to us.
+ * @param orderType 'bid' or 'ask' enum.
+ * @param source Exchange where these orders were placed.
+ * @param paradexOrder A ParadexOrder object; basically price, amount tuple.
+ */
+export function parseParadexOrder(
+ paradexMarket: ParadexMarket,
+ observedTimestamp: number,
+ orderType: OrderType,
+ source: string,
+ paradexOrder: ParadexOrder,
+): TokenOrder {
+ const tokenOrder = new TokenOrder();
+ const price = new BigNumber(paradexOrder.price);
+ const amount = new BigNumber(paradexOrder.amount);
+
+ tokenOrder.source = source;
+ tokenOrder.observedTimestamp = observedTimestamp;
+ tokenOrder.orderType = orderType;
+ tokenOrder.price = price;
+
+ tokenOrder.baseAssetSymbol = paradexMarket.baseToken;
+ tokenOrder.baseAssetAddress = paradexMarket.baseTokenAddress as string;
+ tokenOrder.baseVolume = price.times(amount);
+
+ tokenOrder.quoteAssetSymbol = paradexMarket.quoteToken;
+ tokenOrder.quoteAssetAddress = paradexMarket.quoteTokenAddress as string;
+ tokenOrder.quoteVolume = amount;
+ return tokenOrder;
+}