This commit is contained in:
wphan
2022-11-29 16:29:46 -08:00
commit 74554249e4
13 changed files with 5497 additions and 0 deletions

4
.env.example Normal file
View File

@@ -0,0 +1,4 @@
ANCHOR_PRIVATE_KEY=246,79,83,235,227,63,148,45,236,118,164,3,0,99,197,152,7,161,4,247,132,15,56,14,71,41,175,39,108,68,32,37,233,229,35,89,133,166,36,228,162,196,142,255,237,118,168,210,61,163,132,32,11,89,22,89,116,119,126,116,203,65,29,77
ENDPOINT=https://api.devnet.solana.com
ENV=devnet
PORT=6969

37
.eslintrc.json Normal file
View File

@@ -0,0 +1,37 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"env": {
"browser": true
},
"ignorePatterns": ["**/lib", "**/node_modules", "migrations"],
"plugins": [],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/ban-ts-ignore": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": [
2,
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_"
}
],
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/no-empty-function": 0,
"no-mixed-spaces-and-tabs": [2, "smart-tabs"],
"no-prototype-builtins": "off",
"semi": 2
},
"settings": {
"react": {
"version": "detect"
}
}
}

142
.gitignore vendored Normal file
View File

@@ -0,0 +1,142 @@
# Created by https://www.gitignore.io/api/osx,node,linux,windows
### Linux ###
*~
# npm tree
package-lock.json
# compiled files
built/
# moved files
src/id.json
src/package.json
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
### Node ###
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
### OSX ###
*.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### Windows ###
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msm
*.msp
# Windows shortcuts
*.lnk
# End of https://www.gitignore.io/api/osx,node,linux,windows
src/**.js
src/**.js.map
.idea
.env
lib

14
Dockerfile Normal file
View File

@@ -0,0 +1,14 @@
FROM public.ecr.aws/bitnami/node:16
RUN apt-get install git
ENV NODE_ENV=production
RUN npm install -g yarn
RUN npm install -g typescript
WORKDIR /app
COPY . .
RUN yarn
RUN yarn build
EXPOSE 9464
CMD [ "yarn", "start:all" ]

72
README.md Normal file
View File

@@ -0,0 +1,72 @@
<div align="center">
<img height="120x" src="https://uploads-ssl.webflow.com/611580035ad59b20437eb024/616f97a42f5637c4517d0193_Logo%20(1)%20(1).png" />
<h1 style="margin-top:20px;">DLOB Serverfor Drift Protocol v2</h1>
<p>
<a href="https://docs.drift.trade/tutorial-keeper-bots"><img alt="Docs" src="https://img.shields.io/badge/docs-tutorials-blueviolet" /></a>
<a href="https://discord.com/channels/849494028176588802/878700556904980500"><img alt="Discord Chat" src="https://img.shields.io/discord/889577356681945098?color=blueviolet" /></a>
<a href="https://opensource.org/licenses/Apache-2.0"><img alt="License" src="https://img.shields.io/github/license/project-serum/anchor?color=blueviolet" /></a>
</p>
</div>
# Run the example client
Modify the variables at the top of `example/client.ts` to match your environment.
```shell
yarn run example
dlob orders count: 38
maxSlot from DLOB init orders: 165107666
Initialized DLOb from server
DLOB for market 0:
Asks
[13] limit 21
[12] limit 20.32
[11] limit 17.73
[10] limit 15.63
[9] limit 15.32
[8] limit 14.35
[7] limit 14.32
[6] limit 14.3
[5] limit 14.23
[4] limit 14.2
[3] vAMMNode 13.896583
[2] limit 13.896194
[1] triggerMarket 0
Bids
[0] limit 13.879792
[1] vAMMNode 13.771213
[2] limit 13.37
[3] limit 13
[4] limit 13
[5] limit 12.45
[6] limit 12
[7] limit 10.37
[8] limit 10
[9] limit 9.97
[10] limit 9.42
[11] limit 9
[12] limit 8.89
[13] limit 8
```
# Setting up
## Setup Environment
```shell
cp .env.example .env
```
Update values in `.env` accordingly
* `ANCHOR_PRIVATE_KEY`: can be a number array (as in example), or a path to a `keypair.json` as generated by `solana-keygen`, it needs to be an initialized user in order to load `DriftClient`.
* `ENV` should be a drift env: `devnet` or `mainnet-beta`
## Run
```shell
yarn run dev
```

178
example/client.ts Normal file
View File

@@ -0,0 +1,178 @@
import fetch from "node-fetch";
import {
BulkAccountLoader,
calculateAskPrice,
calculateBidPrice,
convertToNumber,
DLOB,
DLOBOrders,
DLOBOrdersCoder,
DriftClient,
EventSubscriber,
getVariant,
initialize,
MarketType,
OrderActionRecord,
OrderRecord,
PRICE_PRECISION,
Wallet,
WrappedEvent,
} from "@drift-labs/sdk";
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
/********** SET THESE **********/
const dlobServerURL = "http://localhost:6969/orders/idl";
// const dlobServerURL = "https://dlob.drift.trade/orders/idl";
// const rpcEndpoint = "https://solana-api.syndica.io/access-token/eqLMxrnnLgz5eSGKsPIaFK0dSvJeYCNviWhnIgaAH3YdM0K6C6tQdctk94Rb9mCX/rpc";
// const driftEnv = "mainnet-beta";
// const dlobServerURL = "https://master.dlob.drift.trade/orders/idl";
const rpcEndpoint = "https://api.devnet.solana.com";
const driftEnv = "devnet";
const initializedKey = [1, 2, 3, 4, 5]; // private key to initialize driftClient
/*******************************/
//@ts-ignore
const sdkConfig = initialize({ env: driftEnv });
const stateCommitment = 'confirmed';
const keypair = Keypair.fromSecretKey(Uint8Array.from(initializedKey));
const wallet = new Wallet(keypair);
const connection = new Connection(rpcEndpoint, stateCommitment);
const driftClientPublicKey = new PublicKey(sdkConfig.DRIFT_PROGRAM_ID);
/********** initializing driftClient as usual **********/
const bulkAccountLoader = new BulkAccountLoader(
connection,
stateCommitment,
1000
);
const driftClient = new DriftClient({
connection,
wallet,
programID: driftClientPublicKey,
accountSubscription: {
type: 'polling',
accountLoader: bulkAccountLoader,
},
env: driftEnv,
userStats: true,
});
const eventSubscriber = new EventSubscriber(connection, driftClient.program, {
maxTx: 8192,
maxEventsPerType: 8192,
orderBy: 'blockchain',
orderDir: 'desc',
commitment: stateCommitment,
logProviderConfig: {
type: 'polling',
frequency: 1000,
// type: 'websocket',
},
});
let maxDlobSlot = 0;
const dlob = new DLOB();
driftClient.subscribe().then((success) => {
if (!success) {
throw new Error("DriftClient subscription failed");
}
eventSubscriber.subscribe().then((success) => {
if (!success) {
throw new Error("EventSubscriber subscription failed");
}
/********** Example for updating dlob on events **********/
eventSubscriber.eventEmitter.on('newEvent', async (event: WrappedEvent<any>) => {
// NOTE: only apply events if they're newer than the last
// only apply new events if they're newer than the last slot we've seen
if (event.eventType === "OrderRecord") {
const record = event as OrderRecord;
if (maxDlobSlot > 0 && event.slot > maxDlobSlot) {
dlob.handleOrderRecord(record);
console.log(`Handled OrderRecord event for market ${record.order.marketIndex}`);
printDlob(record.order.marketIndex, dlob);
}
} else if (event.eventType === "OrderActionRecord") {
const record = event as OrderActionRecord;
if (maxDlobSlot > 0 && event.slot > maxDlobSlot) {
dlob.handleOrderActionRecord(record);
console.log(`Handled OrderActionRecord event for market ${record.marketIndex}, action: ${getVariant(record.action)} ${getVariant(record.actionExplanation)}`);
printDlob(record.marketIndex, dlob);
}
}
});
doDlobDemo();
});
});
/********** Example for initializing dlob **********/
function getLatestSlotFromOrders(dlobOrders: DLOBOrders): number {
return Math.max(...dlobOrders.map((dlobOrder) => dlobOrder.order.slot.toNumber()));
}
function printDlob(marketIndex: number, dlob: DLOB) {
const currSlot = bulkAccountLoader.mostRecentSlot;
const marketAccount = driftClient.getPerpMarketAccount(marketIndex);
const oracle = driftClient.getOracleDataForPerpMarket(marketIndex);
// you can also set vBid and vAsk to undefined to omit the vAMM price node from bids and asks
const vBid = calculateBidPrice(marketAccount!, oracle);
const vAsk = calculateAskPrice(marketAccount!, oracle);
const dlobBids = dlob.getBids(marketIndex, vBid, currSlot, MarketType.PERP, oracle);
const dlobAsks = dlob.getAsks(marketIndex, vAsk, currSlot, MarketType.PERP, oracle);
console.log(`DLOB for market ${marketIndex}:`);
console.log("Asks");
const dlobAsksArray = Array(...dlobAsks).reverse();
let countAsks = dlobAsksArray.length;
for (const ask of dlobAsksArray) {
const isVamm = ask.isVammNode();
console.log(` [${countAsks}] ${isVamm ? "vAMMNode" : getVariant(ask.order?.orderType)} ${convertToNumber(ask.getPrice(oracle, currSlot), PRICE_PRECISION)}`);
countAsks--;
}
console.log("Bids");
let countBids = 0;
for (const bid of dlobBids) {
const isVamm = bid.isVammNode();
console.log(` [${countBids}] ${isVamm ? "vAMMNode" : getVariant(bid.order?.orderType)} ${convertToNumber(bid.getPrice(oracle, currSlot), PRICE_PRECISION)}`);
countBids++;
}
console.log("");
}
function doDlobDemo() {
const dlobCoder = DLOBOrdersCoder.create();
fetch(dlobServerURL)
.then(
(r) => {
r.arrayBuffer().then((b) => {
const dlobOrders = dlobCoder.decode(Buffer.from(b));
// console.log(JSON.stringify(dlobOrders, null, 2));
console.log(`dlob orders count: ${dlobOrders.length}`);
dlob.initFromOrders(dlobOrders);
maxDlobSlot = getLatestSlotFromOrders(dlobOrders);
console.log(`maxSlot from DLOB init orders: ${maxDlobSlot}`);
console.log("Initialized DLOb from server");
printDlob(0, dlob);
printDlob(1, dlob);
printDlob(2, dlob);
});
});
}

203
example/clientWithSlot.ts Normal file
View File

@@ -0,0 +1,203 @@
/**
* Usage:
* ts-node clientWithSlot.ts
*/
import fetch from "node-fetch";
import {
BulkAccountLoader,
calculateAskPrice,
calculateBidPrice,
convertToNumber,
DLOB,
DLOBOrders,
DLOBOrdersCoder,
DriftClient,
DriftClientSubscriptionConfig,
EventSubscriber,
getVariant,
initialize,
LogProviderConfig,
MarketType,
OrderActionRecord,
OrderRecord,
PRICE_PRECISION,
Wallet,
WrappedEvent,
} from "@drift-labs/sdk";
import { Connection, Keypair, PublicKey } from "@solana/web3.js";
/********** SET THESE **********/
// const dlobServerURL = "http://localhost:6969/orders/idlWithSlot";
const dlobServerURL = "https://dlob.drift.trade/orders/idlWithSlot";
const rpcEndpoint = "https://api.mainnet-beta.solana.com";
const driftEnv = "mainnet-beta";
// const dlobServerURL = "https://master.dlob.drift.trade/orders/idlWithSlot";
// const rpcEndpoint = "https://api.devnet.solana.com";
// const driftEnv = "devnet";
const initializedKey = [1, 2, 3, 4, 5]; // private key to initialize driftClient
const useWebsocket = true;
const levelsToPrint = 10;
/*******************************/
//@ts-ignore
const sdkConfig = initialize({ env: driftEnv });
const stateCommitment = 'confirmed';
const keypair = Keypair.fromSecretKey(Uint8Array.from(initializedKey));
const wallet = new Wallet(keypair);
const connection = new Connection(rpcEndpoint, stateCommitment);
const driftClientPublicKey = new PublicKey(sdkConfig.DRIFT_PROGRAM_ID);
let lastSeenSlot = 0;
/********** initializing driftClient as usual **********/
var accountSubscription: DriftClientSubscriptionConfig;
var logProviderConfig: LogProviderConfig;
if (useWebsocket) {
accountSubscription = {
type: 'websocket',
};
logProviderConfig = {
type: 'websocket',
};
} else {
accountSubscription = {
type: 'polling',
accountLoader: new BulkAccountLoader(
connection,
stateCommitment,
10000
),
};
logProviderConfig = {
type: 'polling',
frequency: 10000,
};
}
const driftClient = new DriftClient({
connection,
wallet,
programID: driftClientPublicKey,
accountSubscription,
env: driftEnv,
userStats: true,
});
const eventSubscriber = new EventSubscriber(connection, driftClient.program, {
maxTx: 8192,
maxEventsPerType: 8192,
orderBy: 'blockchain',
orderDir: 'desc',
commitment: stateCommitment,
logProviderConfig,
});
const maxDlobSlot = 0;
const dlob = new DLOB();
driftClient.subscribe().then((success) => {
if (!success) {
throw new Error("DriftClient subscription failed");
}
eventSubscriber.subscribe().then((success) => {
if (!success) {
throw new Error("EventSubscriber subscription failed");
}
/********** Example for updating dlob on events **********/
eventSubscriber.eventEmitter.on('newEvent', async (event: WrappedEvent<any>) => {
// NOTE: only apply events if they're newer than the last
// only apply new events if they're newer than the last slot we've seen
lastSeenSlot = event.slot;
if (event.eventType === "OrderRecord") {
const record = event as OrderRecord;
if (maxDlobSlot > 0 && event.slot > maxDlobSlot) {
dlob.handleOrderRecord(record, event.slot);
console.log(`Handled OrderRecord event for market ${record.order.marketIndex}`);
printDlob(record.order.marketIndex, dlob);
}
} else if (event.eventType === "OrderActionRecord") {
const record = event as OrderActionRecord;
if (maxDlobSlot > 0 && event.slot > maxDlobSlot) {
dlob.handleOrderActionRecord(record, event.slot);
console.log(`Handled OrderActionRecord event for market ${record.marketIndex}, action: ${getVariant(record.action)} ${getVariant(record.actionExplanation)}`);
printDlob(record.marketIndex, dlob);
}
}
});
setInterval(doDlobDemo, 10000);
});
});
/********** Example for initializing dlob **********/
function _getLatestSlotFromOrders(dlobOrders: DLOBOrders): number {
return Math.max(...dlobOrders.map((dlobOrder) => dlobOrder.order.slot.toNumber()));
}
function printDlob(marketIndex: number, dlob: DLOB) {
const currSlot = lastSeenSlot;
if (lastSeenSlot === 0) {
console.log('waiting for first slot update...');
return;
}
const marketAccount = driftClient.getPerpMarketAccount(marketIndex);
const oracle = driftClient.getOracleDataForPerpMarket(marketIndex);
// you can also set vBid and vAsk to undefined to omit the vAMM price node from bids and asks
const vBid = calculateBidPrice(marketAccount!, oracle);
const vAsk = calculateAskPrice(marketAccount!, oracle);
const dlobBids = dlob.getBids(marketIndex, vBid, lastSeenSlot, MarketType.PERP, oracle);
const dlobAsks = dlob.getAsks(marketIndex, vAsk, lastSeenSlot, MarketType.PERP, oracle);
console.log(`DLOB for market ${marketIndex}:`);
console.log("Asks");
const dlobAsksArray = Array(...dlobAsks).reverse();
let countAsks = dlobAsksArray.length;
for (const ask of dlobAsksArray) {
const isVamm = ask.isVammNode();
if (countAsks < levelsToPrint) {
console.log(` [${countAsks - 1}] ${isVamm ? "vAMMNode" : getVariant(ask.order?.orderType)} ${convertToNumber(ask.getPrice(oracle, currSlot), PRICE_PRECISION)} (oraclePriceOffset: ${ask.order?.oraclePriceOffset || 0 * PRICE_PRECISION})`);
}
countAsks--;
}
console.log("Bids");
let countBids = 0;
for (const bid of dlobBids) {
const isVamm = bid.isVammNode();
if (countBids < levelsToPrint) {
console.log(` [${countBids}] ${isVamm ? "vAMMNode" : getVariant(bid.order?.orderType)} ${convertToNumber(bid.getPrice(oracle, currSlot), PRICE_PRECISION)} (oraclePriceOffset: ${bid.order?.oraclePriceOffset || 0 * PRICE_PRECISION})`);
}
countBids++;
}
console.log("");
}
function doDlobDemo() {
const dlobCoder = DLOBOrdersCoder.create();
fetch(dlobServerURL)
.then(
async (r) => {
console.log(`dlob server response: ${r.status} ${r.statusText}`);
const resp = await r.json();
lastSeenSlot = resp['slot'];
console.log(`Latest dlob slot: ${lastSeenSlot}`);
const dlobOrdersBuffer = Buffer.from(resp['data'], 'base64');
const dlobOrders = dlobCoder.decode(Buffer.from(dlobOrdersBuffer));
console.log(`Total Dlob orders count: ${dlobOrders.length}`);
dlob.initFromOrders(dlobOrders, lastSeenSlot);
const marketIndex = 0;
console.log("Initialized DLOb from server");
printDlob(marketIndex, dlob);
});
}

52
package.json Normal file
View File

@@ -0,0 +1,52 @@
{
"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"
}
}

759
src/index.ts Normal file
View File

@@ -0,0 +1,759 @@
import { program, Option } from "commander";
import responseTime = require("response-time");
import express from "express";
import rateLimit from "express-rate-limit";
import compression from "compression";
import morgan from "morgan";
import cors from "cors";
import { Connection, Commitment, PublicKey } from "@solana/web3.js";
import {
getVariant,
BulkAccountLoader,
DriftClient,
initialize,
DriftEnv,
SlotSubscriber,
UserMap,
DLOBOrder,
DLOBOrders,
DLOBOrdersCoder,
SpotMarkets,
PerpMarkets,
DLOBSubscriber,
MarketType,
} from "@drift-labs/sdk";
import { Mutex } from "async-mutex";
import { getWallet } from "./utils";
import { logger, setLogLevel } from "./logger";
import { PrometheusExporter } from "@opentelemetry/exporter-prometheus";
import {
ExplicitBucketHistogramAggregation,
InstrumentType,
MeterProvider,
View,
} from "@opentelemetry/sdk-metrics-base";
import { ObservableResult } from "@opentelemetry/api";
require("dotenv").config();
const driftEnv = (process.env.ENV || "devnet") as DriftEnv;
const commitHash = process.env.COMMIT;
//@ts-ignore
const sdkConfig = initialize({ env: process.env.ENV });
const stateCommitment: Commitment = "confirmed";
const serverPort = process.env.PORT || 6969;
const bulkAccountLoaderPollingInterval = process.env
.BULK_ACCOUNT_LOADER_POLLING_INTERVAL
? parseInt(process.env.BULK_ACCOUNT_LOADER_POLLING_INTERVAL)
: 5000;
const healthCheckInterval = bulkAccountLoaderPollingInterval * 2;
const rateLimitCallsPerSecond = process.env.RATE_LIMIT_CALLS_PER_SECOND
? parseInt(process.env.RATE_LIMIT_CALLS_PER_SECOND)
: 10;
const logFormat =
':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent" :req[x-forwarded-for]';
const logHttp = morgan(logFormat, {
skip: (_req, res) => res.statusCode < 400,
});
function errorHandler(err, _req, res, _next) {
logger.error(err.stack);
res.status(500).send("Internal error");
}
const app = express();
app.use(cors({ origin: "*" }));
app.use(compression());
app.set("trust proxy", 1);
app.use(logHttp);
app.use(
rateLimit({
windowMs: 1000, // 1 second
max: rateLimitCallsPerSecond,
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
})
);
// strip off /dlob, if the request comes from exchange history server LB
app.use((req, _res, next) => {
if (req.url.startsWith("/dlob")) {
req.url = req.url.replace("/dlob", "");
if (req.url === "") {
req.url = "/";
}
}
next();
});
program
.option("-d, --dry-run", "Dry run, do not send transactions on chain")
.option("--test-liveness", "Purposefully fail liveness test after 1 minute")
.addOption(
new Option(
"-p, --private-key <string>",
"private key, supports path to id.json, or list of comma separate numbers"
).env("ANCHOR_PRIVATE_KEY")
)
.option("--debug", "Enable debug logging")
.parse();
const opts = program.opts();
setLogLevel(opts.debug ? "debug" : "info");
const endpoint = process.env.ENDPOINT;
const wsEndpoint = process.env.WS_ENDPOINT;
logger.info(`RPC endpoint: ${endpoint}`);
logger.info(`WS endpoint: ${wsEndpoint}`);
logger.info(`DriftEnv: ${driftEnv}`);
logger.info(`Commit: ${commitHash}`);
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Creates {count} buckets of size {increment} starting from {start}. Each bucket stores the count of values within its "size".
* @param start
* @param increment
* @param count
* @returns
*/
const createHistogramBuckets = (
start: number,
increment: number,
count: number
) => {
return new ExplicitBucketHistogramAggregation(
Array.from(new Array(count), (_, i) => start + i * increment)
);
};
enum METRIC_TYPES {
runtime_specs = "runtime_specs",
endpoint_response_times_histogram = "endpoint_response_times_histogram",
health_status = "health_status",
}
export enum HEALTH_STATUS {
Ok = 0,
StaleBulkAccountLoader,
UnhealthySlotSubscriber,
LivenessTesting,
}
const metricsPort =
parseInt(process.env.METRICS_PORT) || PrometheusExporter.DEFAULT_OPTIONS.port;
const { endpoint: defaultEndpoint } = PrometheusExporter.DEFAULT_OPTIONS;
const exporter = new PrometheusExporter(
{
port: metricsPort,
endpoint: defaultEndpoint,
},
() => {
logger.info(
`prometheus scrape endpoint started: http://localhost:${metricsPort}${defaultEndpoint}`
);
}
);
const meterName = "dlob-meter";
const meterProvider = new MeterProvider({
views: [
new View({
instrumentName: METRIC_TYPES.endpoint_response_times_histogram,
instrumentType: InstrumentType.HISTOGRAM,
meterName,
aggregation: createHistogramBuckets(0, 50, 30),
}),
],
});
meterProvider.addMetricReader(exporter);
const meter = meterProvider.getMeter(meterName);
const runtimeSpecsGauge = meter.createObservableGauge(
METRIC_TYPES.runtime_specs,
{
description: "Runtime sepcification of this program",
}
);
const bootTimeMs = Date.now();
runtimeSpecsGauge.addCallback((obs) => {
obs.observe(bootTimeMs, {
commit: commitHash,
driftEnv,
rpcEndpoint: endpoint,
wsEndpoint: wsEndpoint,
});
});
let healthStatus: HEALTH_STATUS = HEALTH_STATUS.Ok;
const healthStatusGauge = meter.createObservableGauge(
METRIC_TYPES.health_status,
{
description: "Health status of this program",
}
);
healthStatusGauge.addCallback((obs: ObservableResult) => {
obs.observe(healthStatus, {});
});
const endpointResponseTimeHistogram = meter.createHistogram(
METRIC_TYPES.endpoint_response_times_histogram,
{
description: "Duration of endpoint responses",
unit: "ms",
}
);
const main = async () => {
const wallet = getWallet();
const clearingHousePublicKey = new PublicKey(sdkConfig.DRIFT_PROGRAM_ID);
const connection = new Connection(endpoint, {
wsEndpoint: wsEndpoint,
commitment: stateCommitment,
});
const bulkAccountLoader = new BulkAccountLoader(
connection,
stateCommitment,
bulkAccountLoaderPollingInterval
);
const lastBulkAccountLoaderSlotMutex = new Mutex();
let lastBulkAccountLoaderSlot = bulkAccountLoader.mostRecentSlot;
let lastBulkAccountLoaderSlotUpdated = Date.now();
const driftClient = new DriftClient({
connection,
wallet,
programID: clearingHousePublicKey,
perpMarketIndexes: PerpMarkets[driftEnv].map((mkt) => mkt.marketIndex),
spotMarketIndexes: SpotMarkets[driftEnv].map((mkt) => mkt.marketIndex),
oracleInfos: PerpMarkets[driftEnv].map((mkt) => {
return { publicKey: mkt.oracle, source: mkt.oracleSource };
}),
accountSubscription: {
type: "polling",
accountLoader: bulkAccountLoader,
},
env: driftEnv,
userStats: true,
});
const dlobCoder = DLOBOrdersCoder.create();
const slotSubscriber = new SlotSubscriber(connection, {});
const lastSlotReceivedMutex = new Mutex();
let lastSlotReceived: number;
let lastHealthCheckSlot = -1;
let lastHealthCheckSlotUpdated = Date.now();
const startupTime = Date.now();
const lamportsBalance = await connection.getBalance(wallet.publicKey);
logger.info(
`DriftClient ProgramId: ${driftClient.program.programId.toBase58()}`
);
logger.info(`Wallet pubkey: ${wallet.publicKey.toBase58()}`);
logger.info(` . SOL balance: ${lamportsBalance / 10 ** 9}`);
await driftClient.subscribe();
driftClient.eventEmitter.on("error", (e) => {
logger.info("clearing house error");
logger.error(e);
});
await slotSubscriber.subscribe();
slotSubscriber.eventEmitter.on("newSlot", async (slot: number) => {
await lastSlotReceivedMutex.runExclusive(async () => {
lastSlotReceived = slot;
});
});
if (!(await driftClient.getUser().exists())) {
logger.error(`User for ${wallet.publicKey} does not exist`);
if (opts.initUser) {
logger.info(`Creating User for ${wallet.publicKey}`);
const [txSig] = await driftClient.initializeUserAccount();
logger.info(`Initialized user account in transaction: ${txSig}`);
} else {
throw new Error("Run with '--init-user' flag to initialize a User");
}
}
const userMap = new UserMap(
driftClient,
driftClient.userAccountSubscriptionConfig,
false
);
await userMap.subscribe();
const dlobSubscriber = new DLOBSubscriber({
driftClient,
dlobSource: userMap,
slotSource: slotSubscriber,
updateFrequency: bulkAccountLoaderPollingInterval,
});
await dlobSubscriber.subscribe();
const handleResponseTime = responseTime((req: Request, _res, time) => {
const endpoint = req.url;
const responseTimeMs = time;
endpointResponseTimeHistogram.record(responseTimeMs, {
endpoint,
});
});
// start http server listening to /health endpoint using http package
app.get("/health", handleResponseTime, async (req, res, next) => {
try {
if (req.url === "/health") {
if (opts.testLiveness) {
if (Date.now() > startupTime + 60 * 1000) {
healthStatus = HEALTH_STATUS.LivenessTesting;
res.writeHead(500);
res.end("Testing liveness test fail");
return;
}
}
// check if a slot was received recently
let healthySlotSubscriber = false;
await lastSlotReceivedMutex.runExclusive(async () => {
const slotChanged = lastSlotReceived > lastHealthCheckSlot;
const slotChangedRecently =
Date.now() - lastHealthCheckSlotUpdated < healthCheckInterval;
healthySlotSubscriber = slotChanged || slotChangedRecently;
logger.debug(
`Slotsubscriber health check: lastSlotReceived: ${lastSlotReceived}, lastHealthCheckSlot: ${lastHealthCheckSlot}, slotChanged: ${slotChanged}, slotChangedRecently: ${slotChangedRecently}`
);
if (slotChanged) {
lastHealthCheckSlot = lastSlotReceived;
lastHealthCheckSlotUpdated = Date.now();
}
});
if (!healthySlotSubscriber) {
healthStatus = HEALTH_STATUS.UnhealthySlotSubscriber;
logger.error(`SlotSubscriber is not healthy`);
res.writeHead(500);
res.end(`SlotSubscriber is not healthy`);
return;
}
if (bulkAccountLoader) {
let healthyBulkAccountLoader = false;
await lastBulkAccountLoaderSlotMutex.runExclusive(async () => {
const slotChanged =
bulkAccountLoader.mostRecentSlot > lastBulkAccountLoaderSlot;
const slotChangedRecently =
Date.now() - lastBulkAccountLoaderSlotUpdated <
healthCheckInterval;
healthyBulkAccountLoader = slotChanged || slotChangedRecently;
logger.debug(
`BulkAccountLoader health check: bulkAccountLoader.mostRecentSlot: ${bulkAccountLoader.mostRecentSlot}, lastBulkAccountLoaderSlot: ${lastBulkAccountLoaderSlot}, slotChanged: ${slotChanged}, slotChangedRecently: ${slotChangedRecently}`
);
if (slotChanged) {
lastBulkAccountLoaderSlot = bulkAccountLoader.mostRecentSlot;
lastBulkAccountLoaderSlotUpdated = Date.now();
}
});
if (!healthyBulkAccountLoader) {
healthStatus = HEALTH_STATUS.StaleBulkAccountLoader;
logger.error(
`Health check failed due to stale bulkAccountLoader.mostRecentSlot`
);
res.writeHead(501);
res.end(`bulkAccountLoader.mostRecentSlot is not healthy`);
return;
}
}
// liveness check passed
healthStatus = HEALTH_STATUS.Ok;
res.writeHead(200);
res.end("OK");
} else {
res.writeHead(404);
res.end("Not found");
}
} catch (e) {
next(e);
}
});
app.get("/orders/json/raw", handleResponseTime, async (_req, res, next) => {
try {
// object with userAccount key and orders object serialized
const orders: Array<any> = [];
const oracles: Array<any> = [];
const slot = bulkAccountLoader.mostRecentSlot;
for (const market of driftClient.getPerpMarketAccounts()) {
const oracle = driftClient.getOracleDataForPerpMarket(
market.marketIndex
);
oracles.push({
marketIndex: market.marketIndex,
...oracle,
});
}
for (const user of userMap.values()) {
const userAccount = user.getUserAccount();
for (const order of userAccount.orders) {
if (getVariant(order.status) === "init") {
continue;
}
orders.push({
user: user.getUserAccountPublicKey().toBase58(),
order: order,
});
}
}
// respond with orders
res.writeHead(200);
res.end(
JSON.stringify({
slot,
oracles,
orders,
})
);
} catch (e) {
next(e);
}
});
app.get("/orders/json", handleResponseTime, async (_req, res, next) => {
try {
// object with userAccount key and orders object serialized
const slot = bulkAccountLoader.mostRecentSlot;
const orders: Array<any> = [];
const oracles: Array<any> = [];
for (const market of driftClient.getPerpMarketAccounts()) {
const oracle = driftClient.getOracleDataForPerpMarket(
market.marketIndex
);
const oracleHuman = {
marketIndex: market.marketIndex,
price: oracle.price.toString(),
slot: oracle.slot.toString(),
confidence: oracle.confidence.toString(),
hasSufficientNumberOfDataPoints:
oracle.hasSufficientNumberOfDataPoints,
};
if (oracle.twap) {
oracleHuman["twap"] = oracle.twap.toString();
}
if (oracle.twapConfidence) {
oracleHuman["twapConfidence"] = oracle.twapConfidence.toString();
}
oracles.push(oracleHuman);
}
for (const user of userMap.values()) {
const userAccount = user.getUserAccount();
for (const order of userAccount.orders) {
if (getVariant(order.status) === "init") {
continue;
}
const orderHuman = {
status: getVariant(order.status),
orderType: getVariant(order.orderType),
marketType: getVariant(order.marketType),
slot: order.slot.toString(),
orderId: order.orderId,
userOrderId: order.userOrderId,
marketIndex: order.marketIndex,
price: order.price.toString(),
baseAssetAmount: order.baseAssetAmount.toString(),
baseAssetAmountFilled: order.baseAssetAmountFilled.toString(),
quoteAssetAmountFilled: order.quoteAssetAmountFilled.toString(),
direction: getVariant(order.direction),
reduceOnly: order.reduceOnly,
triggerPrice: order.triggerPrice.toString(),
triggerCondition: getVariant(order.triggerCondition),
existingPositionDirection: getVariant(
order.existingPositionDirection
),
postOnly: order.postOnly,
immediateOrCancel: order.immediateOrCancel,
oraclePriceOffset: order.oraclePriceOffset,
auctionDuration: order.auctionDuration,
auctionStartPrice: order.auctionStartPrice.toString(),
auctionEndPrice: order.auctionEndPrice.toString(),
maxTs: order.maxTs.toString(),
};
if (order.quoteAssetAmount) {
orderHuman["quoteAssetAmount"] = order.quoteAssetAmount.toString();
}
orders.push({
user: user.getUserAccountPublicKey().toBase58(),
order: orderHuman,
});
}
}
// respond with orders
res.writeHead(200);
res.end(
JSON.stringify({
slot,
oracles,
orders,
})
);
} catch (err) {
next(err);
}
});
app.get("/orders/idl", handleResponseTime, async (_req, res, next) => {
try {
const dlobOrders: DLOBOrders = [];
for (const user of userMap.values()) {
const userAccount = user.getUserAccount();
for (const order of userAccount.orders) {
if (getVariant(order.status) === "init") {
continue;
}
dlobOrders.push({
user: user.getUserAccountPublicKey(),
order,
} as DLOBOrder);
}
}
res.writeHead(200);
res.end(dlobCoder.encode(dlobOrders));
} catch (err) {
next(err);
}
});
app.get(
"/orders/idlWithSlot",
handleResponseTime,
async (_req, res, next) => {
try {
const dlobOrders: DLOBOrders = [];
for (const user of userMap.values()) {
const userAccount = user.getUserAccount();
for (const order of userAccount.orders) {
if (getVariant(order.status) === "init") {
continue;
}
dlobOrders.push({
user: user.getUserAccountPublicKey(),
order,
} as DLOBOrder);
}
}
res.writeHead(200);
res.end(
JSON.stringify({
slot: bulkAccountLoader.mostRecentSlot,
data: dlobCoder.encode(dlobOrders).toString("base64"),
})
);
} catch (err) {
next(err);
}
}
);
const validateDlobQuery = (
marketType?: string,
marketIndex?: string,
marketName?: string
): {
normedMarketType?: MarketType;
normedMarketIndex?: number;
error?: string;
} => {
let normedMarketType: MarketType = undefined;
let normedMarketIndex: number = undefined;
let normedMarketName: string = undefined;
if (marketName === undefined) {
if (marketIndex === undefined || marketType === undefined) {
return {
error:
"Bad Request: (marketName) or (marketIndex and marketType) must be supplied",
};
}
// validate marketType
switch ((marketType as string).toLowerCase()) {
case "spot": {
normedMarketType = MarketType.SPOT;
normedMarketIndex = parseInt(marketIndex as string);
const spotMarketIndicies = SpotMarkets[driftEnv].map(
(mkt) => mkt.marketIndex
);
if (!spotMarketIndicies.includes(normedMarketIndex)) {
return {
error: "Bad Request: invalid marketIndex",
};
}
break;
}
case "perp": {
normedMarketType = MarketType.PERP;
normedMarketIndex = parseInt(marketIndex as string);
const perpMarketIndicies = PerpMarkets[driftEnv].map(
(mkt) => mkt.marketIndex
);
if (!perpMarketIndicies.includes(normedMarketIndex)) {
return {
error: "Bad Request: invalid marketIndex",
};
}
break;
}
default:
return {
error: 'Bad Request: marketType must be either "spot" or "perp"',
};
}
} else {
// validate marketName
normedMarketName = (marketName as string).toUpperCase();
const derivedMarketInfo =
driftClient.getMarketIndexAndType(normedMarketName);
if (!derivedMarketInfo) {
return {
error: "Bad Request: unrecognized marketName",
};
}
normedMarketType = derivedMarketInfo.marketType;
normedMarketIndex = derivedMarketInfo.marketIndex;
}
return {
normedMarketType,
normedMarketIndex,
};
};
app.get("/l2", handleResponseTime, async (req, res, next) => {
try {
const { marketName, marketIndex, marketType, depth, includeVamm } =
req.query;
const { normedMarketType, normedMarketIndex, error } = validateDlobQuery(
marketType as string,
marketIndex as string,
marketName as string
);
if (error) {
res.status(400).send(error);
return;
}
const l2 = dlobSubscriber.getL2({
marketIndex: normedMarketIndex,
marketType: normedMarketType,
depth: depth ? parseInt(depth as string) : 10,
includeVamm: includeVamm
? (includeVamm as string).toLowerCase() === "true"
: false,
});
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,
};
}
}
res.writeHead(200);
res.end(JSON.stringify(l2));
} catch (err) {
next(err);
}
});
app.get("/l3", handleResponseTime, async (req, res, next) => {
try {
const { marketName, marketIndex, marketType } = req.query;
const { normedMarketType, normedMarketIndex, error } = validateDlobQuery(
marketType as string,
marketIndex as string,
marketName as string
);
if (error) {
res.status(400).send(error);
return;
}
const l3 = dlobSubscriber.getL3({
marketIndex: normedMarketIndex,
marketType: normedMarketType,
});
for (const key of Object.keys(l3)) {
for (const idx in l3[key]) {
const level = l3[key][idx];
l3[key][idx] = {
...level,
price: level.price.toString(),
size: level.size.toString(),
};
}
}
res.writeHead(200);
res.end(JSON.stringify(l3));
} catch (err) {
next(err);
}
});
app.use(errorHandler);
app.listen(serverPort, () => {
logger.info(`DLOB server listening on port http://localhost:${serverPort}`);
});
};
async function recursiveTryCatch(f: () => void) {
try {
await f();
} catch (e) {
console.error(e);
await sleep(15000);
await recursiveTryCatch(f);
}
}
recursiveTryCatch(() => main());

16
src/logger.ts Normal file
View File

@@ -0,0 +1,16 @@
import { createLogger, transports, format } from "winston";
export const logger = createLogger({
transports: [new transports.Console()],
format: format.combine(
format.colorize(),
format.timestamp(),
format.printf(({ timestamp, level, message }) => {
return `[${timestamp}] ${level}: ${message}`;
})
),
});
export const setLogLevel = (logLevel: string) => {
logger.level = logLevel;
};

36
src/utils.ts Normal file
View File

@@ -0,0 +1,36 @@
import fs from "fs";
import { Keypair } from "@solana/web3.js";
import { bs58 } from "@project-serum/anchor/dist/cjs/utils/bytes";
import { Wallet } from "@drift-labs/sdk";
import { logger } from "./logger";
export function getWallet(): Wallet {
const privateKey = process.env.ANCHOR_PRIVATE_KEY;
if (!privateKey) {
throw new Error(
"Must set environment variable ANCHOR_PRIVATE_KEY with the path to a id.json or a list of commma separated numbers"
);
}
// try to load privateKey as a filepath
let loadedKey: Uint8Array;
if (fs.existsSync(privateKey)) {
logger.info(`loading private key from ${privateKey}`);
loadedKey = new Uint8Array(
JSON.parse(fs.readFileSync(privateKey).toString())
);
} else {
if (privateKey.includes(",")) {
logger.info(`Trying to load private key as comma separated numbers`);
loadedKey = Uint8Array.from(
privateKey.split(",").map((val) => Number(val))
);
} else {
logger.info(`Trying to load private key as base58 string`);
loadedKey = new Uint8Array(bs58.decode(privateKey));
}
}
const keypair = Keypair.fromSecretKey(Uint8Array.from(loadedKey));
return new Wallet(keypair);
}

19
tsconfig.json Normal file
View File

@@ -0,0 +1,19 @@
{
"compilerOptions": {
"types": [],
"module": "commonjs",
"target": "es2019",
"esModuleInterop": true,
"preserveConstEnums": true,
"sourceMap": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"baseUrl": ".",
"rootDir": "src",
"outDir": "./lib"
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}

3965
yarn.lock Normal file

File diff suppressed because it is too large Load Diff