prettify:fix
This commit is contained in:
@@ -1,16 +1,19 @@
|
|||||||
import {DLOB, OrderSubscriber, UserAccount, UserMap} from "@drift-labs/sdk";
|
import { DLOB, OrderSubscriber, UserAccount, UserMap } from '@drift-labs/sdk';
|
||||||
import {PublicKey} from "@solana/web3.js";
|
import { PublicKey } from '@solana/web3.js';
|
||||||
|
|
||||||
export type DLOBProvider = {
|
export type DLOBProvider = {
|
||||||
subscribe() : Promise<void>;
|
subscribe(): Promise<void>;
|
||||||
getDLOB(slot: number): Promise<DLOB>;
|
getDLOB(slot: number): Promise<DLOB>;
|
||||||
getUniqueAuthorities(): PublicKey[];
|
getUniqueAuthorities(): PublicKey[];
|
||||||
getUserAccounts(): Generator<{userAccount: UserAccount, publicKey: PublicKey}>;
|
getUserAccounts(): Generator<{
|
||||||
|
userAccount: UserAccount;
|
||||||
|
publicKey: PublicKey;
|
||||||
|
}>;
|
||||||
getUserAccount(publicKey: PublicKey): UserAccount | undefined;
|
getUserAccount(publicKey: PublicKey): UserAccount | undefined;
|
||||||
size(): number;
|
size(): number;
|
||||||
}
|
};
|
||||||
|
|
||||||
export function getDLOBProviderFromUserMap(userMap:UserMap) : DLOBProvider {
|
export function getDLOBProviderFromUserMap(userMap: UserMap): DLOBProvider {
|
||||||
return {
|
return {
|
||||||
subscribe: async () => {
|
subscribe: async () => {
|
||||||
await userMap.subscribe();
|
await userMap.subscribe();
|
||||||
@@ -21,9 +24,12 @@ export function getDLOBProviderFromUserMap(userMap:UserMap) : DLOBProvider {
|
|||||||
getUniqueAuthorities: () => {
|
getUniqueAuthorities: () => {
|
||||||
return userMap.getUniqueAuthorities();
|
return userMap.getUniqueAuthorities();
|
||||||
},
|
},
|
||||||
getUserAccounts: function*() {
|
getUserAccounts: function* () {
|
||||||
for (const user of userMap.values()) {
|
for (const user of userMap.values()) {
|
||||||
yield {userAccount: user.getUserAccount(), publicKey: user.getUserAccountPublicKey()};
|
yield {
|
||||||
|
userAccount: user.getUserAccount(),
|
||||||
|
publicKey: user.getUserAccountPublicKey(),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getUserAccount: (publicKey) => {
|
getUserAccount: (publicKey) => {
|
||||||
@@ -31,11 +37,13 @@ export function getDLOBProviderFromUserMap(userMap:UserMap) : DLOBProvider {
|
|||||||
},
|
},
|
||||||
size: () => {
|
size: () => {
|
||||||
return userMap.size();
|
return userMap.size();
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDLOBProviderFromOrderSubscriber(orderSubscriber: OrderSubscriber) : DLOBProvider {
|
export function getDLOBProviderFromOrderSubscriber(
|
||||||
|
orderSubscriber: OrderSubscriber
|
||||||
|
): DLOBProvider {
|
||||||
return {
|
return {
|
||||||
subscribe: async () => {
|
subscribe: async () => {
|
||||||
await orderSubscriber.subscribe();
|
await orderSubscriber.subscribe();
|
||||||
@@ -50,16 +58,20 @@ export function getDLOBProviderFromOrderSubscriber(orderSubscriber: OrderSubscri
|
|||||||
}
|
}
|
||||||
return Array.from(authorities.values());
|
return Array.from(authorities.values());
|
||||||
},
|
},
|
||||||
getUserAccounts: function*() {
|
getUserAccounts: function* () {
|
||||||
for (const [key, {userAccount}] of orderSubscriber.usersAccounts.entries()) {
|
for (const [
|
||||||
yield {userAccount: userAccount, publicKey: new PublicKey(key)};
|
key,
|
||||||
|
{ userAccount },
|
||||||
|
] of orderSubscriber.usersAccounts.entries()) {
|
||||||
|
yield { userAccount: userAccount, publicKey: new PublicKey(key) };
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
getUserAccount: (publicKey) => {
|
getUserAccount: (publicKey) => {
|
||||||
return orderSubscriber.usersAccounts.get(publicKey.toString())?.userAccount;
|
return orderSubscriber.usersAccounts.get(publicKey.toString())
|
||||||
|
?.userAccount;
|
||||||
},
|
},
|
||||||
size(): number {
|
size(): number {
|
||||||
return orderSubscriber.usersAccounts.size;
|
return orderSubscriber.usersAccounts.size;
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
33
src/index.ts
33
src/index.ts
@@ -47,7 +47,11 @@ import {
|
|||||||
sleep,
|
sleep,
|
||||||
validateDlobQuery,
|
validateDlobQuery,
|
||||||
} from './utils/utils';
|
} from './utils/utils';
|
||||||
import {DLOBProvider, getDLOBProviderFromOrderSubscriber, getDLOBProviderFromUserMap} from "./dlobProvider";
|
import {
|
||||||
|
DLOBProvider,
|
||||||
|
getDLOBProviderFromOrderSubscriber,
|
||||||
|
getDLOBProviderFromUserMap,
|
||||||
|
} from './dlobProvider';
|
||||||
|
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const driftEnv = (process.env.ENV || 'devnet') as DriftEnv;
|
const driftEnv = (process.env.ENV || 'devnet') as DriftEnv;
|
||||||
@@ -59,7 +63,8 @@ const stateCommitment: Commitment = 'processed';
|
|||||||
const serverPort = process.env.PORT || 6969;
|
const serverPort = process.env.PORT || 6969;
|
||||||
export const ORDERBOOK_UPDATE_INTERVAL = 1000;
|
export const ORDERBOOK_UPDATE_INTERVAL = 1000;
|
||||||
const useWebsocket = process.env.USE_WEBSOCKET?.toLowerCase() === 'true';
|
const useWebsocket = process.env.USE_WEBSOCKET?.toLowerCase() === 'true';
|
||||||
const useOrderSubscriber = process.env.USE_ORDER_SUBSCRIBER?.toLowerCase() === 'true';
|
const useOrderSubscriber =
|
||||||
|
process.env.USE_ORDER_SUBSCRIBER?.toLowerCase() === 'true';
|
||||||
|
|
||||||
const rateLimitCallsPerSecond = process.env.RATE_LIMIT_CALLS_PER_SECOND
|
const rateLimitCallsPerSecond = process.env.RATE_LIMIT_CALLS_PER_SECOND
|
||||||
? parseInt(process.env.RATE_LIMIT_CALLS_PER_SECOND)
|
? parseInt(process.env.RATE_LIMIT_CALLS_PER_SECOND)
|
||||||
@@ -224,17 +229,17 @@ const main = async () => {
|
|||||||
env: driftEnv,
|
env: driftEnv,
|
||||||
});
|
});
|
||||||
|
|
||||||
let dlobProvider : DLOBProvider;
|
let dlobProvider: DLOBProvider;
|
||||||
if (useOrderSubscriber) {
|
if (useOrderSubscriber) {
|
||||||
let subscriptionConfig;
|
let subscriptionConfig;
|
||||||
if (useWebsocket) {
|
if (useWebsocket) {
|
||||||
subscriptionConfig = {
|
subscriptionConfig = {
|
||||||
type: "websocket",
|
type: 'websocket',
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
subscriptionConfig = {
|
subscriptionConfig = {
|
||||||
type: "polling",
|
type: 'polling',
|
||||||
frequency: ORDERBOOK_UPDATE_INTERVAL
|
frequency: ORDERBOOK_UPDATE_INTERVAL,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -275,7 +280,9 @@ const main = async () => {
|
|||||||
logger.info(`Initializing DLOB Provider...`);
|
logger.info(`Initializing DLOB Provider...`);
|
||||||
const initUserMapStart = Date.now();
|
const initUserMapStart = Date.now();
|
||||||
await dlobProvider.subscribe();
|
await dlobProvider.subscribe();
|
||||||
logger.info(`dlob provider initialized in ${Date.now() - initUserMapStart} ms`);
|
logger.info(
|
||||||
|
`dlob provider initialized in ${Date.now() - initUserMapStart} ms`
|
||||||
|
);
|
||||||
logger.info(`dlob provider size ${dlobProvider.size()}`);
|
logger.info(`dlob provider size ${dlobProvider.size()}`);
|
||||||
|
|
||||||
const initUserStatsMapStarts = Date.now();
|
const initUserStatsMapStarts = Date.now();
|
||||||
@@ -334,7 +341,7 @@ const main = async () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const {userAccount, publicKey} of dlobProvider.getUserAccounts()) {
|
for (const { userAccount, publicKey } of dlobProvider.getUserAccounts()) {
|
||||||
for (const order of userAccount.orders) {
|
for (const order of userAccount.orders) {
|
||||||
if (isVariant(order.status, 'init')) {
|
if (isVariant(order.status, 'init')) {
|
||||||
continue;
|
continue;
|
||||||
@@ -387,7 +394,7 @@ const main = async () => {
|
|||||||
}
|
}
|
||||||
oracles.push(oracleHuman);
|
oracles.push(oracleHuman);
|
||||||
}
|
}
|
||||||
for (const {userAccount, publicKey} of dlobProvider.getUserAccounts()) {
|
for (const { userAccount, publicKey } of dlobProvider.getUserAccounts()) {
|
||||||
for (const order of userAccount.orders) {
|
for (const order of userAccount.orders) {
|
||||||
if (isVariant(order.status, 'init')) {
|
if (isVariant(order.status, 'init')) {
|
||||||
continue;
|
continue;
|
||||||
@@ -449,7 +456,7 @@ const main = async () => {
|
|||||||
try {
|
try {
|
||||||
const dlobOrders: DLOBOrders = [];
|
const dlobOrders: DLOBOrders = [];
|
||||||
|
|
||||||
for (const {userAccount, publicKey} of dlobProvider.getUserAccounts()) {
|
for (const { userAccount, publicKey } of dlobProvider.getUserAccounts()) {
|
||||||
for (const order of userAccount.orders) {
|
for (const order of userAccount.orders) {
|
||||||
if (isVariant(order.status, 'init')) {
|
if (isVariant(order.status, 'init')) {
|
||||||
continue;
|
continue;
|
||||||
@@ -497,7 +504,7 @@ const main = async () => {
|
|||||||
|
|
||||||
const dlobOrders: DLOBOrders = [];
|
const dlobOrders: DLOBOrders = [];
|
||||||
|
|
||||||
for (const {userAccount, publicKey} of dlobProvider.getUserAccounts()) {
|
for (const { userAccount, publicKey } of dlobProvider.getUserAccounts()) {
|
||||||
for (const order of userAccount.orders) {
|
for (const order of userAccount.orders) {
|
||||||
if (isVariant(order.status, 'init')) {
|
if (isVariant(order.status, 'init')) {
|
||||||
continue;
|
continue;
|
||||||
@@ -584,7 +591,9 @@ const main = async () => {
|
|||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
if (`${includeUserStats}`.toLowerCase() === 'true') {
|
if (`${includeUserStats}`.toLowerCase() === 'true') {
|
||||||
const userAccount = dlobProvider.getUserAccount(side.userAccount);
|
const userAccount = dlobProvider.getUserAccount(
|
||||||
|
side.userAccount
|
||||||
|
);
|
||||||
const userStats = await userStatsMap.mustGet(
|
const userStats = await userStatsMap.mustGet(
|
||||||
userAccount.authority.toBase58()
|
userAccount.authority.toBase58()
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -9,14 +9,20 @@ import {
|
|||||||
SlotSubscriber,
|
SlotSubscriber,
|
||||||
UserMap,
|
UserMap,
|
||||||
Wallet,
|
Wallet,
|
||||||
BulkAccountLoader, OrderSubscriber, SlotSource,
|
BulkAccountLoader,
|
||||||
|
OrderSubscriber,
|
||||||
|
SlotSource,
|
||||||
} from '@drift-labs/sdk';
|
} from '@drift-labs/sdk';
|
||||||
|
|
||||||
import { logger, setLogLevel } from '../utils/logger';
|
import { logger, setLogLevel } from '../utils/logger';
|
||||||
import { sleep } from '../utils/utils';
|
import { sleep } from '../utils/utils';
|
||||||
import { DLOBSubscriberIO } from '../dlob-subscriber/DLOBSubscriberIO';
|
import { DLOBSubscriberIO } from '../dlob-subscriber/DLOBSubscriberIO';
|
||||||
import { RedisClient } from '../utils/redisClient';
|
import { RedisClient } from '../utils/redisClient';
|
||||||
import {DLOBProvider, getDLOBProviderFromOrderSubscriber, getDLOBProviderFromUserMap} from "../dlobProvider";
|
import {
|
||||||
|
DLOBProvider,
|
||||||
|
getDLOBProviderFromOrderSubscriber,
|
||||||
|
getDLOBProviderFromUserMap,
|
||||||
|
} from '../dlobProvider';
|
||||||
|
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const driftEnv = (process.env.ENV || 'devnet') as DriftEnv;
|
const driftEnv = (process.env.ENV || 'devnet') as DriftEnv;
|
||||||
@@ -38,7 +44,8 @@ setLogLevel(opts.debug ? 'debug' : 'info');
|
|||||||
|
|
||||||
const endpoint = process.env.ENDPOINT;
|
const endpoint = process.env.ENDPOINT;
|
||||||
const wsEndpoint = process.env.WS_ENDPOINT;
|
const wsEndpoint = process.env.WS_ENDPOINT;
|
||||||
const useOrderSubscriber = process.env.USE_ORDER_SUBSCRIBER?.toLowerCase() === 'true';
|
const useOrderSubscriber =
|
||||||
|
process.env.USE_ORDER_SUBSCRIBER?.toLowerCase() === 'true';
|
||||||
|
|
||||||
logger.info(`RPC endpoint: ${endpoint}`);
|
logger.info(`RPC endpoint: ${endpoint}`);
|
||||||
logger.info(`WS endpoint: ${wsEndpoint}`);
|
logger.info(`WS endpoint: ${wsEndpoint}`);
|
||||||
@@ -85,13 +92,13 @@ const main = async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let slotSource: SlotSource;
|
let slotSource: SlotSource;
|
||||||
let dlobProvider : DLOBProvider;
|
let dlobProvider: DLOBProvider;
|
||||||
if (useOrderSubscriber) {
|
if (useOrderSubscriber) {
|
||||||
const orderSubscriber = new OrderSubscriber({
|
const orderSubscriber = new OrderSubscriber({
|
||||||
driftClient,
|
driftClient,
|
||||||
subscriptionConfig: {
|
subscriptionConfig: {
|
||||||
type: "polling",
|
type: 'polling',
|
||||||
frequency: ORDERBOOK_UPDATE_INTERVAL
|
frequency: ORDERBOOK_UPDATE_INTERVAL,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user