Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solana/ts: add parsed transaction to callback #200

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading