Added phoenix and serum subscribers
This commit is contained in:
16
.vscode/launch.json
vendored
Normal file
16
.vscode/launch.json
vendored
Normal 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,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -42,6 +42,7 @@
|
|||||||
"start": "node lib/index.js",
|
"start": "node lib/index.js",
|
||||||
"dev": "ts-node src/index.ts",
|
"dev": "ts-node src/index.ts",
|
||||||
"dev:inspect": "yarn build && node --inspect ./lib/index.js",
|
"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",
|
"example": "ts-node example/client.ts",
|
||||||
"exampleWithSlot": "ts-node example/clientWithSlot.ts",
|
"exampleWithSlot": "ts-node example/clientWithSlot.ts",
|
||||||
"prettify": "prettier --check './src/**/*.ts'",
|
"prettify": "prettier --check './src/**/*.ts'",
|
||||||
|
|||||||
89
src/index.ts
89
src/index.ts
@@ -24,6 +24,9 @@ import {
|
|||||||
PerpMarkets,
|
PerpMarkets,
|
||||||
DLOBSubscriber,
|
DLOBSubscriber,
|
||||||
MarketType,
|
MarketType,
|
||||||
|
SpotMarketConfig,
|
||||||
|
PhoenixSubscriber,
|
||||||
|
SerumSubscriber,
|
||||||
} from "@drift-labs/sdk";
|
} from "@drift-labs/sdk";
|
||||||
|
|
||||||
import { Mutex } from "async-mutex";
|
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 main = async () => {
|
||||||
const wallet = getWallet();
|
const wallet = getWallet();
|
||||||
const clearingHousePublicKey = new PublicKey(sdkConfig.DRIFT_PROGRAM_ID);
|
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
|
// start http server listening to /health endpoint using http package
|
||||||
app.get("/health", handleResponseTime, async (req, res, next) => {
|
app.get("/health", handleResponseTime, async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
@@ -705,21 +772,21 @@ const main = async () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isSpot = marketType === "spot";
|
||||||
|
|
||||||
const l2 = await dlobSubscriber.getL2({
|
const l2 = await dlobSubscriber.getL2({
|
||||||
marketIndex: normedMarketIndex,
|
marketIndex: normedMarketIndex,
|
||||||
marketType: normedMarketType,
|
marketType: normedMarketType,
|
||||||
depth: depth ? parseInt(depth as string) : 10,
|
depth: depth ? parseInt(depth as string) : 10,
|
||||||
opts: {
|
includeVamm: `${includeVamm}`.toLowerCase() === "true",
|
||||||
includeVammL2: includeVamm
|
fallbackL2Generators: isSpot
|
||||||
? `${includeVamm}`.toLowerCase() === "true"
|
? [
|
||||||
: false,
|
`${includePhoenix}`.toLowerCase() === "true" &&
|
||||||
includePhoenixL2: includeSerum
|
MARKET_SUBSCRIBERS[normedMarketIndex].phoenix,
|
||||||
? `${includeSerum}`.toLowerCase() === "true"
|
`${includeSerum}`.toLowerCase() === "true" &&
|
||||||
: false,
|
MARKET_SUBSCRIBERS[normedMarketIndex].serum,
|
||||||
includeSerumL2: includePhoenix
|
].filter((a) => !!a)
|
||||||
? `${includePhoenix}`.toLowerCase() === "true"
|
: [],
|
||||||
: false,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const key of Object.keys(l2)) {
|
for (const key of Object.keys(l2)) {
|
||||||
|
|||||||
Reference in New Issue
Block a user