diff --git a/package.json b/package.json index d1ceec9..e9a4e99 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 7b9a5aa..46c0e6e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); } diff --git a/yarn.lock b/yarn.lock index eaac1fa..e76475f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"