Skip to content

Commit

Permalink
Merge pull request #23 from drift-labs/crispheaney/rpc-optimizations
Browse files Browse the repository at this point in the history
quick rpc optimizations
  • Loading branch information
crispheaney authored Nov 20, 2023
2 parents 54f5332 + e7bd57a commit 423814e
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ const main = async () => {
});

const dlobCoder = DLOBOrdersCoder.create();
const slotSubscriber = new SlotSubscriber(connection, {});

const lamportsBalance = await connection.getBalance(wallet.publicKey);
logger.info(
Expand All @@ -199,12 +198,11 @@ const main = async () => {
logger.error(e);
});

await slotSubscriber.subscribe();
slotSubscriber.eventEmitter.on('newSlot', async (slot: number) => {
setInterval(async () => {
await lastSlotReceivedMutex.runExclusive(async () => {
lastSlotReceived = slot;
lastSlotReceived = bulkAccountLoader.getSlot();
});
});
}, ORDERBOOK_UPDATE_INTERVAL);

const userMap = new UserMap(
driftClient,
Expand All @@ -217,15 +215,15 @@ const main = async () => {
accountLoader: new BulkAccountLoader(
connection,
stateCommitment,
ORDERBOOK_UPDATE_INTERVAL * 10
0
),
});
await userStatsMap.subscribe();

const dlobSubscriber = new DLOBSubscriber({
driftClient,
dlobSource: userMap,
slotSource: slotSubscriber,
slotSource: bulkAccountLoader,
updateFrequency: ORDERBOOK_UPDATE_INTERVAL,
});
await dlobSubscriber.subscribe();
Expand Down Expand Up @@ -255,7 +253,7 @@ const main = async () => {
// object with userAccount key and orders object serialized
const orders: Array<any> = [];
const oracles: Array<any> = [];
const slot = slotSubscriber.currentSlot;
const slot = bulkAccountLoader.getSlot();

for (const market of driftClient.getPerpMarketAccounts()) {
const oracle = driftClient.getOracleDataForPerpMarket(
Expand Down Expand Up @@ -299,7 +297,7 @@ const main = async () => {
app.get('/orders/json', async (_req, res, next) => {
try {
// object with userAccount key and orders object serialized
const slot = slotSubscriber.currentSlot;
const slot = bulkAccountLoader.getSlot();
const orders: Array<any> = [];
const oracles: Array<any> = [];
for (const market of driftClient.getPerpMarketAccounts()) {
Expand Down Expand Up @@ -462,7 +460,7 @@ const main = async () => {

res.end(
JSON.stringify({
slot: slotSubscriber.currentSlot,
slot: bulkAccountLoader.getSlot(),
data: dlobCoder.encode(dlobOrders).toString('base64'),
})
);
Expand Down Expand Up @@ -551,7 +549,7 @@ const main = async () => {
.getDLOB()
.getRestingLimitBids(
normedMarketIndex,
slotSubscriber.getSlot(),
bulkAccountLoader.getSlot(),
normedMarketType,
oracle
)
Expand All @@ -562,7 +560,7 @@ const main = async () => {
.getDLOB()
.getRestingLimitAsks(
normedMarketIndex,
slotSubscriber.getSlot(),
bulkAccountLoader.getSlot(),
normedMarketType,
oracle
)
Expand Down

0 comments on commit 423814e

Please sign in to comment.