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

add async generators functions to subscribe to updates #8

Merged
merged 7 commits into from
Mar 22, 2023
Merged
Changes from 6 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
82 changes: 82 additions & 0 deletions src/sdk/block-engine/searcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,35 @@ export class SearcherClient {
});
}

// Yields on account updates owned by the provided list of programs.
async *programUpdates(
programs: PublicKey[],
onError: (e: Error) => void
): AsyncGenerator<VersionedTransaction[]> {
const stream: ClientReadableStream<PendingTxNotification> =
this.client.subscribeMempool({
programV0Sub: {
programs: programs.map(p => p.toString()),
},
});

stream.on('error', e => {
onError(e);
});

for await (const pendingTxNotification of stream) {
try {
yield deserializeTransactions(pendingTxNotification.transactions);
} catch (e) {
if (e instanceof Error) {
onError(e);
} else {
onError(new Error('Deserialization error'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you log e here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. also removed this try/catch on bundleResults as it would never be reached

}
}
}
}

// Triggers the provided callback on updates to the provided accounts.
onAccountUpdate(
accounts: PublicKey[],
Expand All @@ -149,6 +178,35 @@ export class SearcherClient {
});
}

// Yields on updates to the provided accounts.
async *accountUpdates(
accounts: PublicKey[],
onError: (e: Error) => void
): AsyncGenerator<VersionedTransaction[]> {
const stream: ClientReadableStream<PendingTxNotification> =
this.client.subscribeMempool({
wlaV0Sub: {
accounts: accounts.map(a => a.toString()),
},
});

stream.on('error', e => {
onError(e);
});

for await (const pendingTxNotification of stream) {
try {
yield deserializeTransactions(pendingTxNotification.transactions);
} catch (e) {
if (e instanceof Error) {
onError(e);
} else {
onError(new Error('Deserialization error'));
}
}
}
}

// Subscribes to bundle results.
onBundleResult(
successCallback: (bundleResult: BundleResult) => void,
Expand All @@ -167,6 +225,30 @@ export class SearcherClient {
errorCallback(new Error(`Stream error: ${e.message}`));
});
}

// Yields on bundle results.
async *bundleResults(
onError: (e: Error) => void
): AsyncGenerator<BundleResult> {
const stream: ClientReadableStream<BundleResult> =
this.client.subscribeBundleResults({});

stream.on('error', e => {
onError(e);
});

for await (const bundleResult of stream) {
try {
yield bundleResult;
} catch (e) {
if (e instanceof Error) {
onError(e);
} else {
onError(new Error('Deserialization error'));
}
}
}
}
}

export const searcherClient = (
Expand Down