|
| 1 | +/** |
| 2 | + * This code was AUTOGENERATED using the kinobi library. |
| 3 | + * Please DO NOT EDIT THIS FILE, instead use visitors |
| 4 | + * to add features, then rerun kinobi to update it. |
| 5 | + * |
| 6 | + * @see https://github.com/metaplex-foundation/kinobi |
| 7 | + */ |
| 8 | + |
| 9 | +import { |
| 10 | + Account, |
| 11 | + assertAccountExists, |
| 12 | + Context, |
| 13 | + Pda, |
| 14 | + PublicKey, |
| 15 | + RpcAccount, |
| 16 | + RpcGetAccountsOptions, |
| 17 | + publicKey as toPublicKey, |
| 18 | +} from '@metaplex-foundation/umi'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Defines the input for a single account to be fetched and deserialized |
| 22 | + * as part of a mixed-account batch fetch. |
| 23 | + * |
| 24 | + * @typeParam T - The deserialized account type. |
| 25 | + */ |
| 26 | +export type FetchAccountInput<T extends Account<any>> = { |
| 27 | + publicKey: PublicKey | Pda; |
| 28 | + deserialize: (rawAccount: RpcAccount) => T; |
| 29 | +}; |
| 30 | + |
| 31 | +/** @internal */ |
| 32 | +type DeserializedAccounts<T extends FetchAccountInput<any>[]> = { |
| 33 | + [K in keyof T]: T[K] extends FetchAccountInput<infer U> ? U : never; |
| 34 | +}; |
| 35 | + |
| 36 | +/** @internal */ |
| 37 | +type MaybeDeserializedAccounts<T extends FetchAccountInput<any>[]> = { |
| 38 | + [K in keyof T]: T[K] extends FetchAccountInput<infer U> ? U | null : never; |
| 39 | +}; |
| 40 | + |
| 41 | +/** |
| 42 | + * Fetches multiple accounts of potentially different types in a single RPC |
| 43 | + * call and deserializes each one using its provided deserializer. |
| 44 | + * |
| 45 | + * This is useful in frontends and other scenarios where you want to minimize |
| 46 | + * the number of RPC round-trips by batching up to 100 accounts into a single |
| 47 | + * `getMultipleAccounts` call while still getting fully typed results. |
| 48 | + * |
| 49 | + * All accounts must exist, otherwise an error is thrown. Use |
| 50 | + * {@link safeFetchAllMixedAccounts} if some accounts may not exist. |
| 51 | + * |
| 52 | + * @example |
| 53 | + * ```ts |
| 54 | + * const [metadata, edition] = await fetchAllMixedAccounts(context, [ |
| 55 | + * { publicKey: metadataAddr, deserialize: deserializeMetadata }, |
| 56 | + * { publicKey: editionAddr, deserialize: deserializeEdition }, |
| 57 | + * ]); |
| 58 | + * // metadata: Metadata, edition: Edition — fully typed! |
| 59 | + * ``` |
| 60 | + */ |
| 61 | +export async function fetchAllMixedAccounts< |
| 62 | + T extends FetchAccountInput<any>[], |
| 63 | +>( |
| 64 | + context: Pick<Context, 'rpc'>, |
| 65 | + inputs: [...T], |
| 66 | + options?: RpcGetAccountsOptions |
| 67 | +): Promise<DeserializedAccounts<T>> { |
| 68 | + const publicKeys = inputs.map((input) => |
| 69 | + toPublicKey(input.publicKey, false) |
| 70 | + ); |
| 71 | + const maybeAccounts = await context.rpc.getAccounts(publicKeys, options); |
| 72 | + return maybeAccounts.map((maybeAccount, index) => { |
| 73 | + assertAccountExists(maybeAccount); |
| 74 | + return inputs[index].deserialize(maybeAccount); |
| 75 | + }) as DeserializedAccounts<T>; |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Fetches multiple accounts of potentially different types in a single RPC |
| 80 | + * call and deserializes each one using its provided deserializer. |
| 81 | + * |
| 82 | + * Accounts that do not exist are returned as `null` at the corresponding |
| 83 | + * position in the output tuple. |
| 84 | + * |
| 85 | + * @example |
| 86 | + * ```ts |
| 87 | + * const [metadata, edition] = await safeFetchAllMixedAccounts(context, [ |
| 88 | + * { publicKey: metadataAddr, deserialize: deserializeMetadata }, |
| 89 | + * { publicKey: editionAddr, deserialize: deserializeEdition }, |
| 90 | + * ]); |
| 91 | + * // metadata: Metadata | null, edition: Edition | null |
| 92 | + * ``` |
| 93 | + */ |
| 94 | +export async function safeFetchAllMixedAccounts< |
| 95 | + T extends FetchAccountInput<any>[], |
| 96 | +>( |
| 97 | + context: Pick<Context, 'rpc'>, |
| 98 | + inputs: [...T], |
| 99 | + options?: RpcGetAccountsOptions |
| 100 | +): Promise<MaybeDeserializedAccounts<T>> { |
| 101 | + const publicKeys = inputs.map((input) => |
| 102 | + toPublicKey(input.publicKey, false) |
| 103 | + ); |
| 104 | + const maybeAccounts = await context.rpc.getAccounts(publicKeys, options); |
| 105 | + return maybeAccounts.map((maybeAccount, index) => { |
| 106 | + return maybeAccount.exists |
| 107 | + ? inputs[index].deserialize(maybeAccount as RpcAccount) |
| 108 | + : null; |
| 109 | + }) as MaybeDeserializedAccounts<T>; |
| 110 | +} |
0 commit comments