add grouping query to /l2

This commit is contained in:
wphan
2023-05-31 18:23:16 -07:00
parent db003ef219
commit a92935f253
3 changed files with 41 additions and 21 deletions

View File

@@ -5,7 +5,7 @@
"main": "lib/index.js",
"license": "Apache-2.0",
"dependencies": {
"@drift-labs/sdk": "2.30.0",
"@drift-labs/sdk": "2.31.0-beta.4",
"@opentelemetry/api": "^1.1.0",
"@opentelemetry/auto-instrumentations-node": "^0.31.1",
"@opentelemetry/exporter-prometheus": "^0.31.0",

View File

@@ -29,6 +29,9 @@ import {
SerumSubscriber,
DLOBNode,
isVariant,
BN,
groupL2,
L2OrderBook,
} from '@drift-labs/sdk';
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) => {
try {
const {
@@ -870,6 +891,7 @@ const main = async () => {
includeVamm,
includePhoenix,
includeSerum,
grouping, // undefined or PRICE_PRECISION
} = req.query;
const { normedMarketType, normedMarketIndex, error } = validateDlobQuery(
@@ -884,7 +906,7 @@ const main = async () => {
const isSpot = isVariant(normedMarketType, 'spot');
const l2 = await dlobSubscriber.getL2({
const l2 = dlobSubscriber.getL2({
marketIndex: normedMarketIndex,
marketType: normedMarketType,
depth: depth ? parseInt(depth as string) : 10,
@@ -899,23 +921,21 @@ const main = async () => {
: [],
});
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,
};
if (grouping) {
if (isNaN(parseInt(grouping as string))) {
res
.status(400)
.send('Bad Request: grouping must be a number if supplied');
return;
}
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) {
next(err);
}

View File

@@ -80,10 +80,10 @@
enabled "2.0.x"
kuler "^2.0.0"
"@drift-labs/sdk@2.30.0":
version "2.30.0"
resolved "https://registry.yarnpkg.com/@drift-labs/sdk/-/sdk-2.30.0.tgz#68aa67c5f351d53453c850222c16480350853dd1"
integrity sha512-tyIZ0zi4Ap3KI+tmLecusgcjd/soAzV4gzYKFcRxEuy/o5vbuXMpEsZDvAOhwokP+8AZwFZgp2EhHrxh0obn2w==
"@drift-labs/sdk@2.31.0-beta.4":
version "2.31.0-beta.4"
resolved "https://registry.yarnpkg.com/@drift-labs/sdk/-/sdk-2.31.0-beta.4.tgz#e03d7dc40078f9ccf63cb3899ea07994ef7a2b74"
integrity sha512-3tiNn6LTyUENNu+7TjLJvbPlWXDohRsONYAQOC4DVljXk8jUVFo0tLTyKXkZe9EJsMS+TE3WikIgf9jYJIGHjg==
dependencies:
"@coral-xyz/anchor" "0.26.0"
"@ellipsis-labs/phoenix-sdk" "^1.4.2"