import type { Account } from "@solana/spl-token"; import { TOKEN_PROGRAM_ID, unpackAccount } from "@solana/spl-token"; import type { Connection, PublicKey } from "@solana/web3.js"; /** * Get all token accounts by delegate. * @param connection - The connection to the Solana blockchain. * @param delegatePubkey - The public key of the delegate. * @returns The token accounts by delegate. */ export async function getTokenAccountsByDelegate({ connection, delegate, mint, }: { connection: Connection; delegate: PublicKey; mint: PublicKey; }): Promise { // Each SPL token account is 165 bytes, with the delegate at offset 73 const filters = [ { dataSize: 165 }, { memcmp: { offset: 76, bytes: delegate.toBase58(), }, }, { memcmp: { offset: 0, bytes: mint.toBase58(), }, }, ]; const accounts = await connection.getProgramAccounts(TOKEN_PROGRAM_ID, { filters, }); return accounts.map((account) => unpackAccount(account.pubkey, account.account), ); }