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 batching for getMultipleAccountsInfo #276

Merged
merged 1 commit into from
Mar 14, 2025
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
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"packages": [
"packages/*"
],
"version": "7.4.15",
"version": "7.4.16",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/common",
"version": "7.4.15",
"version": "7.4.16",
"description": "Common utilities and types used by streamflow packages.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "./dist/esm/index.js",
Expand Down
28 changes: 28 additions & 0 deletions packages/common/solana/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
RpcResponseAndContext,
SimulatedTransactionResponse,
SendTransactionError,
AccountInfo,
} from "@solana/web3.js";
import bs58 from "bs58";
import PQueue from "p-queue";
Expand Down Expand Up @@ -581,3 +582,30 @@ export async function getMintAndProgram(
tokenProgramId: programId!,
};
}

/**
* Split fetching of Multiple Accounts Info into batches of 100
* as the maximum number of accounts that can be fetched in a single call is 100
*
* @param connection Connection to use
* @param pubKeys Array of public keys to fetch account info for
* @param commitment Desired level of commitment for querying the state
*
* @return Array of AccountInfo objects
*/
export async function getMultipleAccountsInfoBatched(
connection: Connection,
pubKeys: PublicKey[],
commitment?: Commitment,
): Promise<(AccountInfo<Buffer> | null)[]> {
const batchSize = 99;
const batches: Promise<(AccountInfo<Buffer> | null)[]>[] = [];

for (let i = 0; i < pubKeys.length; i += batchSize) {
const batch = pubKeys.slice(i, i + batchSize);
batches.push(connection.getMultipleAccountsInfo(batch, commitment));
}

const results = await Promise.all(batches);
return results.flat();
}
2 changes: 1 addition & 1 deletion packages/distributor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/distributor",
"version": "7.4.15",
"version": "7.4.16",
"description": "JavaScript SDK to interact with Streamflow Airdrop protocol.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "dist/esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/eslint-config",
"version": "7.4.15",
"version": "7.4.16",
"license": "ISC",
"main": "index.js",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/launchpad/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/launchpad",
"version": "7.4.15",
"version": "7.4.16",
"description": "JavaScript SDK to interact with Streamflow Launchpad protocol.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "dist/esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/staking/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/staking",
"version": "7.4.15",
"version": "7.4.16",
"description": "JavaScript SDK to interact with Streamflow Staking protocol.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "dist/esm/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/stream/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/stream",
"version": "7.4.15",
"version": "7.4.16",
"description": "JavaScript SDK to interact with Streamflow protocol.",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
"main": "./dist/esm/index.js",
Expand Down
5 changes: 3 additions & 2 deletions packages/stream/solana/StreamClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
buildSendThrottler,
IProgramAccount,
ThrottleParams,
getMultipleAccountsInfoBatched,
} from "@streamflow/common/solana";
import * as borsh from "borsh";
import { Program } from "@coral-xyz/anchor";
Expand Down Expand Up @@ -1360,7 +1361,7 @@ export class SolanaStreamClient extends BaseStreamClient {
},
]);
const streamPubKeys = alignedOutgoingProgramAccounts.map((account) => account.account.stream);
const streamAccounts = await this.connection.getMultipleAccountsInfo(streamPubKeys, TX_FINALITY_CONFIRMED);
const streamAccounts = await getMultipleAccountsInfoBatched(this.connection, streamPubKeys, TX_FINALITY_CONFIRMED);
streamAccounts.forEach((account, index) => {
if (account) {
const alignedData = alignedOutgoingProgramAccounts[index].account;
Expand All @@ -1379,7 +1380,7 @@ export class SolanaStreamClient extends BaseStreamClient {
const alignedProxyPDAs = alignedStreamsPubKeys.map((streamPubKey) =>
deriveContractPDA(this.alignedProxyProgram.programId, new PublicKey(streamPubKey)),
);
const alignedProxyAccounts = await this.connection.getMultipleAccountsInfo(alignedProxyPDAs);
const alignedProxyAccounts = await getMultipleAccountsInfoBatched(this.connection, alignedProxyPDAs);
alignedProxyAccounts.forEach((account, index) => {
if (account && account.data.length === ALIGNED_METADATA_ACC_SIZE) {
const alignedData = streamRecord[alignedStreamsPubKeys[index]];
Expand Down
Loading