Skip to content

Instantly share code, notes, and snippets.

@wphan
Last active March 27, 2024 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wphan/1850fa3831d25162f131451efd53b64a to your computer and use it in GitHub Desktop.
Save wphan/1850fa3831d25162f131451efd53b64a to your computer and use it in GitHub Desktop.
Load Drift V2 DLOB with Typescript SDK
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
import {
BASE_PRECISION,
BulkAccountLoader,
configs,
convertToNumber,
DLOBSubscriber,
DriftClient,
getMarketsAndOraclesForSubscription,
MarketType,
PRICE_PRECISION,
SlotSubscriber,
UserMap,
Wallet,
} from './src/index';
import {
DLOBApiClient,
} from './src/dlob/DLOBApiClient';
async function main() {
const driftConfig = configs['mainnet-beta'];
const connection = new Connection('https://api.mainnet-beta.solana.com');
const accountLoader = new BulkAccountLoader(
connection,
'confirmed',
10000
);
const { perpMarketIndexes, spotMarketIndexes, oracleInfos } = getMarketsAndOraclesForSubscription('mainnet-beta');
const driftClient = new DriftClient({
connection: connection,
wallet: new Wallet(new Keypair()),
programID: new PublicKey(driftConfig.DRIFT_PROGRAM_ID),
accountSubscription: {
type: 'polling',
accountLoader: accountLoader,
},
perpMarketIndexes,
spotMarketIndexes,
oracleInfos,
userStats: true,
env: 'mainnet-beta',
});
console.log(`driftClientSubscribed: ${await driftClient.subscribe()}`);
const slotSubscriber = new SlotSubscriber(connection);
await slotSubscriber.subscribe();
// Alternatively, you can also use the UserMap class, which loads the DLOB via RPC calls.
// const userMap = new UserMap(driftClient, driftClient.userAccountSubscriptionConfig, false);
// This loads the DLOB from a server hosted by Drift.
const dlobAPI = new DLOBApiClient({
url: 'https://dlob.drift.trade/orders/idlWithSlot',
});
const dlobSubscriber = new DLOBSubscriber({
dlobSource: dlobAPI,
slotSource: slotSubscriber,
driftClient: driftClient,
updateFrequency: 10000,
});
await dlobSubscriber.subscribe();
const l2 = dlobSubscriber.getL2({
marketIndex: 0,
marketType: MarketType.PERP,
});
console.log("Level 2 order book:");
console.log("Asks:");
const asks = l2.asks.slice().reverse();
for (let i = 0; i < asks.length; i++) {
const ask = asks[i];
console.log(` [${asks.length - i - 1}] ${convertToNumber(ask.size, BASE_PRECISION)} @ $${convertToNumber(ask.price, PRICE_PRECISION)}`);
}
console.log("Bids:");
const bids = l2.bids;
for (let i = 0; i < bids.length; i++) {
const bid = bids[i];
console.log(` [${i}] ${convertToNumber(bid.size, BASE_PRECISION)} @ $${convertToNumber(bid.price, PRICE_PRECISION)}`);
}
console.log("");
console.log("");
/**
Level 2 order book:
Asks:
[9] 664.8 @ $21.3248
[8] 3.5 @ $21.288301
[7] 3.5 @ $21.287237
[6] 3.5 @ $21.283129
[5] 0.1 @ $21.2825
[4] 1937.6 @ $21.2748
[3] 1 @ $21.26
[2] 2015 @ $21.2589
[1] 503.7 @ $21.2483
[0] 377.8 @ $21.243
Bids:
[0] 1168.2 @ $21.2464
[1] 136.7 @ $21.2461
[2] 2570.2 @ $21.2426
[3] 0.1 @ $21.2419
[4] 47 @ $21.216188
[5] 327.8 @ $21.211
[6] 437 @ $21.2057
[7] 47 @ $21.205592
[8] 1748.3 @ $21.1951
[9] 187 @ $21.1792
*/
const l3 = dlobSubscriber.getL3({
marketIndex: 0,
marketType: MarketType.PERP,
});
console.log("Level 3 order book:");
console.log("Asks:");
const l3asks = l3.asks.slice().reverse();
for (let i = 0; i < l3asks.length; i++) {
const ask = l3asks[i];
console.log(` [${l3asks.length - i - 1}] ${ask.maker.toBase58()} ${convertToNumber(ask.size, BASE_PRECISION)} @ $${convertToNumber(ask.price, PRICE_PRECISION)}`);
}
console.log("Bids:");
const l3bids = l3.bids;
for (let i = 0; i < l3bids.length; i++) {
const bid = l3bids[i];
console.log(` [${i}] ${bid.maker.toBase58()} ${convertToNumber(bid.size, BASE_PRECISION)} @ $${convertToNumber(bid.price, PRICE_PRECISION)}`);
}
/**
Level 3 order book:
Asks:
...
[3] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 1 @ $21.26
[2] C13FZykQfLXKuMAMh2iuG7JxhQqd8otujNRAgVETU6id 2015 @ $21.2589
[1] C13FZykQfLXKuMAMh2iuG7JxhQqd8otujNRAgVETU6id 503.7 @ $21.2483
[0] C13FZykQfLXKuMAMh2iuG7JxhQqd8otujNRAgVETU6id 377.8 @ $21.243
Bids:
[0] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 1168.2 @ $21.2464
[1] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 2570.2 @ $21.2464
[2] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 136.7 @ $21.2461
[3] FrEFAwxdrzHxgc7S4cuFfsfLmcg8pfbxnkCQW83euyCS 2570.2 @ $21.2426
...
*/
}
main().catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment