Created
May 21, 2024 22:30
-
-
Save NotoriousPyro/e24897063697bb4836257184616d1c68 to your computer and use it in GitHub Desktop.
AddressTableLookup uncached
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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