Skip to content

Commit

Permalink
solana/ts: add parsed transaction to callback
Browse files Browse the repository at this point in the history
solana/ts: make ArrayQueue dequeue safer
  • Loading branch information
a5-pickle committed Oct 15, 2024
1 parent f98a6bd commit 05b9e57
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
15 changes: 14 additions & 1 deletion solana/ts/src/matchingEngine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ConfirmOptions,
Connection,
Finality,
ParsedTransactionWithMeta,
PublicKey,
SYSVAR_CLOCK_PUBKEY,
SYSVAR_EPOCH_SCHEDULE_PUBKEY,
Expand Down Expand Up @@ -267,6 +268,7 @@ export class MatchingEngineProgram {
eventSlot: number,
signature: string,
currentSlotInfo?: SlotInfo,
parsedTransaction?: ParsedTransactionWithMeta,
) => void,
commitment: Finality = "confirmed",
): number {
Expand All @@ -279,7 +281,17 @@ export class MatchingEngineProgram {
connection.onSlotChange(async (slotInfo) => {
// TODO: Make this more efficient by fetching multiple parsed transactions.
while (!unprocessedTxs.isEmpty()) {
const { signature, eventSlot } = unprocessedTxs.head();
const head = unprocessedTxs.head();

// This check is superfluous. But we do it anyway to make sure the unprocessed
// transaction queue is actually empty.
//
// Once the ArrayQueue has been thoroughly tested, we can remove this check.
if (head === null) {
break;
}

const { signature, eventSlot } = head;

const parsedTx = await connection.getParsedTransaction(signature, {
commitment,
Expand Down Expand Up @@ -316,6 +328,7 @@ export class MatchingEngineProgram {
eventSlot,
signature,
slotInfo,
parsedTx,
);
}
}
Expand Down
8 changes: 6 additions & 2 deletions solana/ts/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ export class ArrayQueue<T> {
this._data = new Array(capacity ?? 256).fill(null);
}

head(): T {
head(): T | null {
if (this.isEmpty()) {
throw new Error("queue is empty");
return null;
}

return this._data[this._index]!;
Expand All @@ -42,6 +42,10 @@ export class ArrayQueue<T> {
}

dequeue(): void {
if (this.isEmpty()) {
return;
}

const data = this._data;
const index = this._index;

Expand Down

0 comments on commit 05b9e57

Please sign in to comment.