add grouping query to /l2
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"license": "Apache-2.0",
|
"license": "Apache-2.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@drift-labs/sdk": "2.30.0",
|
"@drift-labs/sdk": "2.31.0-beta.4",
|
||||||
"@opentelemetry/api": "^1.1.0",
|
"@opentelemetry/api": "^1.1.0",
|
||||||
"@opentelemetry/auto-instrumentations-node": "^0.31.1",
|
"@opentelemetry/auto-instrumentations-node": "^0.31.1",
|
||||||
"@opentelemetry/exporter-prometheus": "^0.31.0",
|
"@opentelemetry/exporter-prometheus": "^0.31.0",
|
||||||
|
|||||||
52
src/index.ts
52
src/index.ts
@@ -29,6 +29,9 @@ import {
|
|||||||
SerumSubscriber,
|
SerumSubscriber,
|
||||||
DLOBNode,
|
DLOBNode,
|
||||||
isVariant,
|
isVariant,
|
||||||
|
BN,
|
||||||
|
groupL2,
|
||||||
|
L2OrderBook,
|
||||||
} from '@drift-labs/sdk';
|
} from '@drift-labs/sdk';
|
||||||
|
|
||||||
import { Mutex } from 'async-mutex';
|
import { Mutex } from 'async-mutex';
|
||||||
@@ -860,6 +863,24 @@ const main = async () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const l2WithBNToStrings = (l2: L2OrderBook): any => {
|
||||||
|
for (const key of Object.keys(l2)) {
|
||||||
|
for (const idx in l2[key]) {
|
||||||
|
const level = l2[key][idx];
|
||||||
|
const sources = level['sources'];
|
||||||
|
for (const sourceKey of Object.keys(sources)) {
|
||||||
|
sources[sourceKey] = sources[sourceKey].toString();
|
||||||
|
}
|
||||||
|
l2[key][idx] = {
|
||||||
|
price: level.price.toString(),
|
||||||
|
size: level.size.toString(),
|
||||||
|
sources,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return l2;
|
||||||
|
};
|
||||||
|
|
||||||
app.get('/l2', handleResponseTime, async (req, res, next) => {
|
app.get('/l2', handleResponseTime, async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
const {
|
const {
|
||||||
@@ -870,6 +891,7 @@ const main = async () => {
|
|||||||
includeVamm,
|
includeVamm,
|
||||||
includePhoenix,
|
includePhoenix,
|
||||||
includeSerum,
|
includeSerum,
|
||||||
|
grouping, // undefined or PRICE_PRECISION
|
||||||
} = req.query;
|
} = req.query;
|
||||||
|
|
||||||
const { normedMarketType, normedMarketIndex, error } = validateDlobQuery(
|
const { normedMarketType, normedMarketIndex, error } = validateDlobQuery(
|
||||||
@@ -884,7 +906,7 @@ const main = async () => {
|
|||||||
|
|
||||||
const isSpot = isVariant(normedMarketType, 'spot');
|
const isSpot = isVariant(normedMarketType, 'spot');
|
||||||
|
|
||||||
const l2 = await dlobSubscriber.getL2({
|
const l2 = dlobSubscriber.getL2({
|
||||||
marketIndex: normedMarketIndex,
|
marketIndex: normedMarketIndex,
|
||||||
marketType: normedMarketType,
|
marketType: normedMarketType,
|
||||||
depth: depth ? parseInt(depth as string) : 10,
|
depth: depth ? parseInt(depth as string) : 10,
|
||||||
@@ -899,23 +921,21 @@ const main = async () => {
|
|||||||
: [],
|
: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const key of Object.keys(l2)) {
|
if (grouping) {
|
||||||
for (const idx in l2[key]) {
|
if (isNaN(parseInt(grouping as string))) {
|
||||||
const level = l2[key][idx];
|
res
|
||||||
const sources = level['sources'];
|
.status(400)
|
||||||
for (const sourceKey of Object.keys(sources)) {
|
.send('Bad Request: grouping must be a number if supplied');
|
||||||
sources[sourceKey] = sources[sourceKey].toString();
|
return;
|
||||||
}
|
|
||||||
l2[key][idx] = {
|
|
||||||
price: level.price.toString(),
|
|
||||||
size: level.size.toString(),
|
|
||||||
sources,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
const groupingBN = new BN(parseInt(grouping as string));
|
||||||
|
res.writeHead(200);
|
||||||
|
res.end(JSON.stringify(l2WithBNToStrings(groupL2(l2, groupingBN))));
|
||||||
|
} else {
|
||||||
|
// make the BNs into strings
|
||||||
|
res.writeHead(200);
|
||||||
|
res.end(JSON.stringify(l2WithBNToStrings(l2)));
|
||||||
}
|
}
|
||||||
|
|
||||||
res.writeHead(200);
|
|
||||||
res.end(JSON.stringify(l2));
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
next(err);
|
next(err);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,10 +80,10 @@
|
|||||||
enabled "2.0.x"
|
enabled "2.0.x"
|
||||||
kuler "^2.0.0"
|
kuler "^2.0.0"
|
||||||
|
|
||||||
"@drift-labs/sdk@2.30.0":
|
"@drift-labs/sdk@2.31.0-beta.4":
|
||||||
version "2.30.0"
|
version "2.31.0-beta.4"
|
||||||
resolved "https://registry.yarnpkg.com/@drift-labs/sdk/-/sdk-2.30.0.tgz#68aa67c5f351d53453c850222c16480350853dd1"
|
resolved "https://registry.yarnpkg.com/@drift-labs/sdk/-/sdk-2.31.0-beta.4.tgz#e03d7dc40078f9ccf63cb3899ea07994ef7a2b74"
|
||||||
integrity sha512-tyIZ0zi4Ap3KI+tmLecusgcjd/soAzV4gzYKFcRxEuy/o5vbuXMpEsZDvAOhwokP+8AZwFZgp2EhHrxh0obn2w==
|
integrity sha512-3tiNn6LTyUENNu+7TjLJvbPlWXDohRsONYAQOC4DVljXk8jUVFo0tLTyKXkZe9EJsMS+TE3WikIgf9jYJIGHjg==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@coral-xyz/anchor" "0.26.0"
|
"@coral-xyz/anchor" "0.26.0"
|
||||||
"@ellipsis-labs/phoenix-sdk" "^1.4.2"
|
"@ellipsis-labs/phoenix-sdk" "^1.4.2"
|
||||||
|
|||||||
Reference in New Issue
Block a user