Skip to content

Instantly share code, notes, and snippets.

@NotoriousPyro
Created May 21, 2024 22:30
Show Gist options
  • Save NotoriousPyro/e24897063697bb4836257184616d1c68 to your computer and use it in GitHub Desktop.
Save NotoriousPyro/e24897063697bb4836257184616d1c68 to your computer and use it in GitHub Desktop.
AddressTableLookup uncached
export const getAddressLookupTableAccounts_Uncached = async (
addressLookupTableAddresses: string[]
): Promise<AddressLookupTableAccount[]> => {
const keys = Array.from([...new Set(addressLookupTableAddresses)]);
const addressLookupTableAccountInfos = await getAccountInfos(await Promise.all(keys.map(async key => Promise.resolve(new PublicKey(key)))));
return await Promise.all(
addressLookupTableAccountInfos.map(
async (account, index) => {
const addressLookupTableAddress = keys[index];
if (addressLookupTableAddress) {
const {publicKey, accountInfo} = account;
return Promise.resolve(new AddressLookupTableAccount({
key: publicKey,
state: AddressLookupTableAccount.deserialize(accountInfo.data),
}));
}
}
)
);
};
export async function* generateAccountInfos(
publicKeys: PublicKey[],
chunkSize: number = 100,
commitment: Commitment = "confirmed"
): AsyncGenerator<{ publicKey: PublicKey, accountInfo: AccountInfo<Buffer>, slot: number }> {
for (let i = 0; i < publicKeys.length; i += chunkSize) {
const chunk = publicKeys.slice(i, i + chunkSize);
const accountInfos = await connection.getMultipleAccountsInfoAndContext(chunk, { commitment });
for (const [index, accountInfo] of accountInfos.value.entries()) {
yield { publicKey: chunk[index], accountInfo, slot: accountInfos.context.slot };
}
}
}
export const getAccountInfos = async (
publicKeys: PublicKey[],
chunkSize: number = 100,
commitment: Commitment = "confirmed"
): Promise<{ publicKey: PublicKey, accountInfo: AccountInfo<Buffer>, slot: number }[]> => {
const accountInfos = [];
for await (const accountInfo of generateAccountInfos(publicKeys, chunkSize, commitment)) {
accountInfos.push(accountInfo);
}
return accountInfos;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment