Added phoenix and serum subscribers

This commit is contained in:
Luke Steyn
2023-05-31 16:02:17 +09:00
parent 413f6f1898
commit 92f78dd540
3 changed files with 145 additions and 61 deletions

16
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Debug",
"port": 2230,
"timeout": 3000,
"restart": true,
},
]
}

View File

@@ -1,52 +1,53 @@
{
"name": "@drift-labs/dlob-server",
"version": "0.1.0",
"author": "wphan",
"main": "lib/index.js",
"license": "Apache-2.0",
"dependencies": {
"@drift-labs/sdk": "2.30.0",
"@opentelemetry/api": "^1.1.0",
"@opentelemetry/auto-instrumentations-node": "^0.31.1",
"@opentelemetry/exporter-prometheus": "^0.31.0",
"@opentelemetry/sdk-node": "^0.31.0",
"@project-serum/anchor": "^0.19.1-beta.1",
"@project-serum/serum": "^0.13.65",
"@solana/web3.js": "^1.22.0",
"async-mutex": "^0.4.0",
"commander": "^9.4.0",
"compression": "^1.7.4",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.18.2",
"express-rate-limit": "^6.7.0",
"morgan": "^1.10.0",
"response-time": "^2.3.2",
"typescript": "4.5.4",
"winston": "^3.8.1"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"eslint": "^7.29.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"husky": "^7.0.4",
"prettier": "^2.4.1",
"ts-node": "^10.9.1"
},
"scripts": {
"prepare": "husky install",
"build": "yarn clean && tsc",
"clean": "rm -rf lib",
"start": "node lib/index.js",
"dev": "ts-node src/index.ts",
"dev:inspect": "yarn build && node --inspect ./lib/index.js",
"example": "ts-node example/client.ts",
"exampleWithSlot": "ts-node example/clientWithSlot.ts",
"prettify": "prettier --check './src/**/*.ts'",
"prettify:fix": "prettier --write './src/**/*.ts'",
"lint": "eslint . --ext ts --quiet",
"lint:fix": "eslint . --ext ts --fix"
}
"name": "@drift-labs/dlob-server",
"version": "0.1.0",
"author": "wphan",
"main": "lib/index.js",
"license": "Apache-2.0",
"dependencies": {
"@drift-labs/sdk": "2.30.0",
"@opentelemetry/api": "^1.1.0",
"@opentelemetry/auto-instrumentations-node": "^0.31.1",
"@opentelemetry/exporter-prometheus": "^0.31.0",
"@opentelemetry/sdk-node": "^0.31.0",
"@project-serum/anchor": "^0.19.1-beta.1",
"@project-serum/serum": "^0.13.65",
"@solana/web3.js": "^1.22.0",
"async-mutex": "^0.4.0",
"commander": "^9.4.0",
"compression": "^1.7.4",
"cors": "^2.8.5",
"dotenv": "^10.0.0",
"express": "^4.18.2",
"express-rate-limit": "^6.7.0",
"morgan": "^1.10.0",
"response-time": "^2.3.2",
"typescript": "4.5.4",
"winston": "^3.8.1"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"eslint": "^7.29.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"husky": "^7.0.4",
"prettier": "^2.4.1",
"ts-node": "^10.9.1"
},
"scripts": {
"prepare": "husky install",
"build": "yarn clean && tsc",
"clean": "rm -rf lib",
"start": "node lib/index.js",
"dev": "ts-node src/index.ts",
"dev:inspect": "yarn build && node --inspect ./lib/index.js",
"dev:debug": "yarn build && node --inspect-brk --inspect=2230 ./lib/index.js",
"example": "ts-node example/client.ts",
"exampleWithSlot": "ts-node example/clientWithSlot.ts",
"prettify": "prettier --check './src/**/*.ts'",
"prettify:fix": "prettier --write './src/**/*.ts'",
"lint": "eslint . --ext ts --quiet",
"lint:fix": "eslint . --ext ts --fix"
}
}

View File

@@ -24,6 +24,9 @@ import {
PerpMarkets,
DLOBSubscriber,
MarketType,
SpotMarketConfig,
PhoenixSubscriber,
SerumSubscriber,
} from "@drift-labs/sdk";
import { Mutex } from "async-mutex";
@@ -215,6 +218,68 @@ const endpointResponseTimeHistogram = meter.createHistogram(
}
);
const getPhoenixSubscriber = (
driftClient: DriftClient,
marketConfig: SpotMarketConfig
) => {
return new PhoenixSubscriber({
connection: driftClient.connection,
programId: new PublicKey(sdkConfig.PHOENIX),
marketAddress: marketConfig.phoenixMarket,
accountSubscription: {
type: "websocket",
},
});
};
const getSerumSubscriber = (
driftClient: DriftClient,
marketConfig: SpotMarketConfig
) => {
return new SerumSubscriber({
connection: driftClient.connection,
programId: new PublicKey(sdkConfig.SERUM_V3),
marketAddress: marketConfig.serumMarket,
accountSubscription: {
type: "websocket",
},
});
};
type SubscriberLookup = {
[marketIndex: number]: {
phoenix?: PhoenixSubscriber;
serum?: SerumSubscriber;
};
};
let MARKET_SUBSCRIBERS: SubscriberLookup = {};
const initializeAllMarketSubscribers = async (driftClient: DriftClient) => {
const markets: SubscriberLookup = {};
for (const market of sdkConfig.SPOT_MARKETS) {
markets[market.marketIndex] = {
phoenix: undefined,
serum: undefined,
};
if (market.phoenixMarket) {
const phoenixSubscriber = getPhoenixSubscriber(driftClient, market);
await phoenixSubscriber.subscribe();
markets[market.marketIndex].phoenix = phoenixSubscriber;
}
if (market.serumMarket) {
const serumSubscriber = getSerumSubscriber(driftClient, market);
await serumSubscriber.subscribe();
markets[market.marketIndex].serum = serumSubscriber;
}
}
return markets;
};
const main = async () => {
const wallet = getWallet();
const clearingHousePublicKey = new PublicKey(sdkConfig.DRIFT_PROGRAM_ID);
@@ -312,6 +377,8 @@ const main = async () => {
});
});
MARKET_SUBSCRIBERS = await initializeAllMarketSubscribers(driftClient);
// start http server listening to /health endpoint using http package
app.get("/health", handleResponseTime, async (req, res, next) => {
try {
@@ -705,21 +772,21 @@ const main = async () => {
return;
}
const isSpot = marketType === "spot";
const l2 = await dlobSubscriber.getL2({
marketIndex: normedMarketIndex,
marketType: normedMarketType,
depth: depth ? parseInt(depth as string) : 10,
opts: {
includeVammL2: includeVamm
? `${includeVamm}`.toLowerCase() === "true"
: false,
includePhoenixL2: includeSerum
? `${includeSerum}`.toLowerCase() === "true"
: false,
includeSerumL2: includePhoenix
? `${includePhoenix}`.toLowerCase() === "true"
: false,
},
includeVamm: `${includeVamm}`.toLowerCase() === "true",
fallbackL2Generators: isSpot
? [
`${includePhoenix}`.toLowerCase() === "true" &&
MARKET_SUBSCRIBERS[normedMarketIndex].phoenix,
`${includeSerum}`.toLowerCase() === "true" &&
MARKET_SUBSCRIBERS[normedMarketIndex].serum,
].filter((a) => !!a)
: [],
});
for (const key of Object.keys(l2)) {