diff --git a/package.json b/package.json index f81f3ba..6007d5e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "lib/index.js", "license": "Apache-2.0", "dependencies": { - "@drift-labs/sdk": "2.41.0-beta.2", + "@drift-labs/sdk": "2.42.0-beta.3", "@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 5f71842..6d73ced 100644 --- a/src/index.ts +++ b/src/index.ts @@ -33,6 +33,7 @@ import { groupL2, L2OrderBook, Wallet, + UserStatsMap, } from '@drift-labs/sdk'; import { Mutex } from 'async-mutex'; @@ -391,6 +392,10 @@ const main = async () => { false ); await userMap.subscribe(); + const userStatsMap = new UserStatsMap( + driftClient, + driftClient.userStatsAccountSubscriptionConfig + ); const dlobSubscriber = new DLOBSubscriber({ driftClient, @@ -787,6 +792,7 @@ const main = async () => { marketType, side, // bid or ask limit, // number of unique makers to return, if undefined will return all + includeUserStats, } = req.query; const { normedMarketType, normedMarketIndex, error } = validateDlobQuery( @@ -817,7 +823,7 @@ const main = async () => { normedLimit = parseInt(limit as string); } - const topMakers: Set = new Set(); + const topMakers = new Set(); let foundMakers = 0; const findMakers = (sideGenerator: Generator) => { for (const side of sideGenerator) { @@ -829,7 +835,15 @@ const main = async () => { if (topMakers.has(maker)) { continue; } else { - topMakers.add(side.userAccount.toBase58()); + if (`${includeUserStats}`.toLowerCase() === 'true') { + const userAccount = side.userAccount.toBase58(); + const userStats = userStatsMap + .get(userAccount) + .userStatsAccountPublicKey.toBase58(); + topMakers.add([userAccount, userStats]); + } else { + topMakers.add(side.userAccount.toBase58()); + } foundMakers++; } } else { @@ -887,6 +901,21 @@ const main = async () => { return l2; }; + const getOracleForMarket = ( + marketType: MarketType, + marketIndex: number + ): number => { + if (isVariant(marketType, 'spot')) { + return driftClient + .getOracleDataForSpotMarket(marketIndex) + .price.toNumber(); + } else if (isVariant(marketType, 'perp')) { + return driftClient + .getOracleDataForPerpMarket(marketIndex) + .price.toNumber(); + } + }; + app.get('/l2', async (req, res, next) => { try { const { @@ -899,6 +928,7 @@ const main = async () => { includePhoenix, includeSerum, grouping, // undefined or PRICE_PRECISION + includeOracle, } = req.query; const { normedMarketType, normedMarketIndex, error } = validateDlobQuery( @@ -945,14 +975,30 @@ const main = async () => { return; } const groupingBN = new BN(parseInt(grouping as string)); - res.writeHead(200); - res.end( - JSON.stringify(l2WithBNToStrings(groupL2(l2, groupingBN, finalDepth))) + const l2Formatted = l2WithBNToStrings( + groupL2(l2, groupingBN, finalDepth) ); + if (`${includeOracle}`.toLowerCase() === 'true') { + l2Formatted['oracle'] = getOracleForMarket( + normedMarketType, + normedMarketIndex + ); + } + + res.writeHead(200); + res.end(JSON.stringify(l2Formatted)); } else { // make the BNs into strings + const l2Formatted = l2WithBNToStrings(l2); + if (`${includeOracle}`.toLowerCase() === 'true') { + l2Formatted['oracle'] = getOracleForMarket( + normedMarketType, + normedMarketIndex + ); + } + res.writeHead(200); - res.end(JSON.stringify(l2WithBNToStrings(l2))); + res.end(JSON.stringify(l2Formatted)); } } catch (err) { next(err); @@ -1109,10 +1155,26 @@ const main = async () => { parseInt(normedParam['grouping'] as string) ); - return l2WithBNToStrings(groupL2(l2, groupingBN, finalDepth)); + const l2Formatted = l2WithBNToStrings( + groupL2(l2, groupingBN, finalDepth) + ); + if (`${normedParam['includeOracle']}`.toLowerCase() === 'true') { + l2Formatted['oracle'] = getOracleForMarket( + normedMarketType, + normedMarketIndex + ); + } + return l2Formatted; } else { // make the BNs into strings - return l2WithBNToStrings(l2); + const l2Formatted = l2WithBNToStrings(l2); + if (`${normedParam['includeOracle']}`.toLowerCase() === 'true') { + l2Formatted['oracle'] = getOracleForMarket( + normedMarketType, + normedMarketIndex + ); + } + return l2Formatted; } }); @@ -1125,7 +1187,7 @@ const main = async () => { app.get('/l3', async (req, res, next) => { try { - const { marketName, marketIndex, marketType } = req.query; + const { marketName, marketIndex, marketType, includeOracle } = req.query; const { normedMarketType, normedMarketIndex, error } = validateDlobQuery( marketType as string, @@ -1153,6 +1215,10 @@ const main = async () => { } } + if (`${includeOracle}`.toLowerCase() === 'true') { + l3['oracle'] = getOracleForMarket(normedMarketType, normedMarketIndex); + } + res.writeHead(200); res.end(JSON.stringify(l3)); } catch (err) { diff --git a/yarn.lock b/yarn.lock index 9ba5e2c..c8ae4d3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -80,10 +80,10 @@ enabled "2.0.x" kuler "^2.0.0" -"@drift-labs/sdk@2.41.0-beta.2": - version "2.41.0-beta.2" - resolved "https://registry.yarnpkg.com/@drift-labs/sdk/-/sdk-2.41.0-beta.2.tgz#a91cac5bd5ae80ac3e7350bdb6fa337f67fab462" - integrity sha512-NdG4ip1af28i8539aZ3bY5Lb42dcPX6WfzG+vjqROzXiYP9s+Wm05fFYOQD3AoP0ZECWO5t5vrUzTSUvEAXSZA== +"@drift-labs/sdk@2.42.0-beta.3": + version "2.42.0-beta.3" + resolved "https://registry.yarnpkg.com/@drift-labs/sdk/-/sdk-2.42.0-beta.3.tgz#23be10343631a85f63a9a9c0ee0f9349992c838b" + integrity sha512-el2ylBRXk+MRqR6q4YjRd/7xRLsQUwOJPX5E8BL8S1iHf83wy80XDITeatDWefcbo0zeFyXOick0oK24JtLHdg== dependencies: "@coral-xyz/anchor" "0.28.1-beta.2" "@ellipsis-labs/phoenix-sdk" "^1.4.2"