123 lines
3.1 KiB
TypeScript
123 lines
3.1 KiB
TypeScript
import { program } from 'commander';
|
|
|
|
import { Connection, Commitment, PublicKey, Keypair } from '@solana/web3.js';
|
|
|
|
import {
|
|
DriftClient,
|
|
initialize,
|
|
DriftEnv,
|
|
SlotSubscriber,
|
|
UserMap,
|
|
Wallet,
|
|
UserStatsMap,
|
|
} from '@drift-labs/sdk';
|
|
|
|
import { logger, setLogLevel } from './utils/logger';
|
|
|
|
import { sleep } from './utils/utils';
|
|
import { DLOBSubscriberIO } from './dlob-subscriber/DLOBSubscriberIO';
|
|
import { RedisClient } from './utils/redisClient';
|
|
|
|
require('dotenv').config();
|
|
const driftEnv = (process.env.ENV || 'devnet') as DriftEnv;
|
|
const commitHash = process.env.COMMIT;
|
|
const REDIS_HOST = process.env.REDIS_HOST || 'localhost';
|
|
const REDIS_PORT = process.env.REDIS_PORT || '6379';
|
|
const REDIS_PASSWORD = process.env.REDIS_PASSWORD;
|
|
|
|
//@ts-ignore
|
|
const sdkConfig = initialize({ env: process.env.ENV });
|
|
|
|
const stateCommitment: Commitment = 'processed';
|
|
const ORDERBOOK_UPDATE_INTERVAL = 1000;
|
|
|
|
let driftClient: DriftClient;
|
|
|
|
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}`);
|
|
|
|
const main = async () => {
|
|
const wallet = new Wallet(new Keypair());
|
|
const clearingHousePublicKey = new PublicKey(sdkConfig.DRIFT_PROGRAM_ID);
|
|
|
|
const connection = new Connection(endpoint, {
|
|
wsEndpoint: wsEndpoint,
|
|
commitment: stateCommitment,
|
|
});
|
|
|
|
driftClient = new DriftClient({
|
|
connection,
|
|
wallet,
|
|
programID: clearingHousePublicKey,
|
|
accountSubscription: {
|
|
type: 'websocket',
|
|
},
|
|
env: driftEnv,
|
|
userStats: true,
|
|
});
|
|
|
|
const slotSubscriber = new SlotSubscriber(connection, {});
|
|
|
|
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();
|
|
|
|
const userMap = new UserMap(
|
|
driftClient,
|
|
driftClient.userAccountSubscriptionConfig,
|
|
false
|
|
);
|
|
await userMap.subscribe();
|
|
const userStatsMap = new UserStatsMap(
|
|
driftClient,
|
|
driftClient.userStatsAccountSubscriptionConfig
|
|
);
|
|
await userStatsMap.subscribe();
|
|
|
|
const redisClient = new RedisClient(REDIS_HOST, REDIS_PORT, REDIS_PASSWORD);
|
|
await redisClient.connect();
|
|
|
|
const dlobSubscriber = new DLOBSubscriberIO({
|
|
driftClient,
|
|
dlobSource: userMap,
|
|
slotSource: slotSubscriber,
|
|
updateFrequency: ORDERBOOK_UPDATE_INTERVAL,
|
|
redisClient,
|
|
});
|
|
await dlobSubscriber.subscribe();
|
|
|
|
console.log('DLOBSubscriber Publishing Messages');
|
|
};
|
|
|
|
async function recursiveTryCatch(f: () => void) {
|
|
try {
|
|
await f();
|
|
} catch (e) {
|
|
console.error(e);
|
|
await sleep(15000);
|
|
await recursiveTryCatch(f);
|
|
}
|
|
}
|
|
|
|
recursiveTryCatch(() => main());
|
|
|
|
export { sdkConfig, endpoint, wsEndpoint, driftEnv, commitHash, driftClient };
|