Skip to content

Commit 05b9e57

Browse files
committed
solana/ts: add parsed transaction to callback
solana/ts: make ArrayQueue dequeue safer
1 parent f98a6bd commit 05b9e57

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

solana/ts/src/matchingEngine/index.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ConfirmOptions,
77
Connection,
88
Finality,
9+
ParsedTransactionWithMeta,
910
PublicKey,
1011
SYSVAR_CLOCK_PUBKEY,
1112
SYSVAR_EPOCH_SCHEDULE_PUBKEY,
@@ -267,6 +268,7 @@ export class MatchingEngineProgram {
267268
eventSlot: number,
268269
signature: string,
269270
currentSlotInfo?: SlotInfo,
271+
parsedTransaction?: ParsedTransactionWithMeta,
270272
) => void,
271273
commitment: Finality = "confirmed",
272274
): number {
@@ -279,7 +281,17 @@ export class MatchingEngineProgram {
279281
connection.onSlotChange(async (slotInfo) => {
280282
// TODO: Make this more efficient by fetching multiple parsed transactions.
281283
while (!unprocessedTxs.isEmpty()) {
282-
const { signature, eventSlot } = unprocessedTxs.head();
284+
const head = unprocessedTxs.head();
285+
286+
// This check is superfluous. But we do it anyway to make sure the unprocessed
287+
// transaction queue is actually empty.
288+
//
289+
// Once the ArrayQueue has been thoroughly tested, we can remove this check.
290+
if (head === null) {
291+
break;
292+
}
293+
294+
const { signature, eventSlot } = head;
283295

284296
const parsedTx = await connection.getParsedTransaction(signature, {
285297
commitment,
@@ -316,6 +328,7 @@ export class MatchingEngineProgram {
316328
eventSlot,
317329
signature,
318330
slotInfo,
331+
parsedTx,
319332
);
320333
}
321334
}

solana/ts/src/utils.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ export class ArrayQueue<T> {
2020
this._data = new Array(capacity ?? 256).fill(null);
2121
}
2222

23-
head(): T {
23+
head(): T | null {
2424
if (this.isEmpty()) {
25-
throw new Error("queue is empty");
25+
return null;
2626
}
2727

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

4444
dequeue(): void {
45+
if (this.isEmpty()) {
46+
return;
47+
}
48+
4549
const data = this._data;
4650
const index = this._index;
4751

0 commit comments

Comments
 (0)