@solana/program-client-core 0.0.0 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,117 @@
1
+ 'use strict';
2
+
3
+ var addresses = require('@solana/addresses');
4
+ var errors = require('@solana/errors');
5
+ var instructions = require('@solana/instructions');
6
+ var signers = require('@solana/signers');
7
+ var accounts = require('@solana/accounts');
8
+
9
+ // src/instruction-input-resolution.ts
10
+ function getNonNullResolvedInstructionInput(inputName, value) {
11
+ if (value === null || value === void 0) {
12
+ throw new errors.SolanaError(errors.SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_NON_NULL, {
13
+ inputName
14
+ });
15
+ }
16
+ return value;
17
+ }
18
+ function getAddressFromResolvedInstructionAccount(inputName, value) {
19
+ const nonNullValue = getNonNullResolvedInstructionInput(inputName, value);
20
+ if (typeof value === "object" && "address" in nonNullValue) {
21
+ return nonNullValue.address;
22
+ }
23
+ if (Array.isArray(nonNullValue)) {
24
+ return nonNullValue[0];
25
+ }
26
+ return nonNullValue;
27
+ }
28
+ function getResolvedInstructionAccountAsProgramDerivedAddress(inputName, value) {
29
+ if (!addresses.isProgramDerivedAddress(value)) {
30
+ throw new errors.SolanaError(errors.SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE, {
31
+ expectedType: "ProgramDerivedAddress",
32
+ inputName
33
+ });
34
+ }
35
+ return value;
36
+ }
37
+ function getResolvedInstructionAccountAsTransactionSigner(inputName, value) {
38
+ if (!isResolvedInstructionAccountSigner(value)) {
39
+ throw new errors.SolanaError(errors.SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE, {
40
+ expectedType: "TransactionSigner",
41
+ inputName
42
+ });
43
+ }
44
+ return value;
45
+ }
46
+ function getAccountMetaFactory(programAddress, optionalAccountStrategy) {
47
+ return (inputName, account) => {
48
+ if (!account.value) {
49
+ if (optionalAccountStrategy === "omitted") return;
50
+ return Object.freeze({ address: programAddress, role: instructions.AccountRole.READONLY });
51
+ }
52
+ const writableRole = account.isWritable ? instructions.AccountRole.WRITABLE : instructions.AccountRole.READONLY;
53
+ const isSigner = isResolvedInstructionAccountSigner(account.value);
54
+ return Object.freeze({
55
+ address: getAddressFromResolvedInstructionAccount(inputName, account.value),
56
+ role: isSigner ? instructions.upgradeRoleToSigner(writableRole) : writableRole,
57
+ ...isSigner ? { signer: account.value } : {}
58
+ });
59
+ };
60
+ }
61
+ function isResolvedInstructionAccountSigner(value) {
62
+ return !!value && typeof value === "object" && "address" in value && typeof value.address === "string" && signers.isTransactionSigner(value);
63
+ }
64
+ function addSelfFetchFunctions(client, codec) {
65
+ const fetchMaybe = async (address, config) => {
66
+ const maybeAccount = await accounts.fetchEncodedAccount(client.rpc, address, config);
67
+ return accounts.decodeAccount(maybeAccount, codec);
68
+ };
69
+ const fetchAllMaybe = async (addresses, config) => {
70
+ const maybeAccounts = await accounts.fetchEncodedAccounts(client.rpc, addresses, config);
71
+ return maybeAccounts.map((maybeAccount) => accounts.decodeAccount(maybeAccount, codec));
72
+ };
73
+ const fetch = async (address, config) => {
74
+ const maybeAccount = await fetchMaybe(address, config);
75
+ accounts.assertAccountExists(maybeAccount);
76
+ return maybeAccount;
77
+ };
78
+ const fetchAll = async (addresses, config) => {
79
+ const maybeAccounts = await fetchAllMaybe(addresses, config);
80
+ accounts.assertAccountsExist(maybeAccounts);
81
+ return maybeAccounts;
82
+ };
83
+ const out = { ...codec, fetch, fetchAll, fetchAllMaybe, fetchMaybe };
84
+ return Object.freeze(out);
85
+ }
86
+
87
+ // src/self-plan-and-send-functions.ts
88
+ function addSelfPlanAndSendFunctions(client, input) {
89
+ if (isPromiseLike(input)) {
90
+ const newInput = input;
91
+ newInput.planTransaction = async (config) => await client.planTransaction(await input, config);
92
+ newInput.planTransactions = async (config) => await client.planTransactions(await input, config);
93
+ newInput.sendTransaction = async (config) => await client.sendTransaction(await input, config);
94
+ newInput.sendTransactions = async (config) => await client.sendTransactions(await input, config);
95
+ return newInput;
96
+ }
97
+ return Object.freeze({
98
+ ...input,
99
+ planTransaction: (config) => client.planTransaction(input, config),
100
+ planTransactions: (config) => client.planTransactions(input, config),
101
+ sendTransaction: (config) => client.sendTransaction(input, config),
102
+ sendTransactions: (config) => client.sendTransactions(input, config)
103
+ });
104
+ }
105
+ function isPromiseLike(item) {
106
+ return !!item && (typeof item === "object" || typeof item === "function") && typeof item.then === "function";
107
+ }
108
+
109
+ exports.addSelfFetchFunctions = addSelfFetchFunctions;
110
+ exports.addSelfPlanAndSendFunctions = addSelfPlanAndSendFunctions;
111
+ exports.getAccountMetaFactory = getAccountMetaFactory;
112
+ exports.getAddressFromResolvedInstructionAccount = getAddressFromResolvedInstructionAccount;
113
+ exports.getNonNullResolvedInstructionInput = getNonNullResolvedInstructionInput;
114
+ exports.getResolvedInstructionAccountAsProgramDerivedAddress = getResolvedInstructionAccountAsProgramDerivedAddress;
115
+ exports.getResolvedInstructionAccountAsTransactionSigner = getResolvedInstructionAccountAsTransactionSigner;
116
+ //# sourceMappingURL=index.node.cjs.map
117
+ //# sourceMappingURL=index.node.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/instruction-input-resolution.ts","../src/self-fetch-functions.ts","../src/self-plan-and-send-functions.ts"],"names":["SolanaError","SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_NON_NULL","isProgramDerivedAddress","SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE","AccountRole","upgradeRoleToSigner","isTransactionSigner","fetchEncodedAccount","decodeAccount","fetchEncodedAccounts","assertAccountExists","assertAccountsExist"],"mappings":";;;;;;;;;AAgCO,SAAS,kCAAA,CAAsC,WAAmB,KAAA,EAAgC;AACrG,EAAA,IAAI,KAAA,KAAU,IAAA,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AA,CAAmE;AAAA,IAC7E,GAAG,KAAA;AAAA,IACH,eAAA,EAAiB,CAAA,MAAA,KAAU,MAAA,CAAO,eAAA,CAAgB,OAAO,MAAM,CAAA;AAAA,IAC/D,gBAAA,EAAkB,CAAA,MAAA,KAAU,MAAA,CAAO,gBAAA,CAAiB,OAAO,MAAM,CAAA;AAAA,IACjE,eAAA,EAAiB,CAAA,MAAA,KAAU,MAAA,CAAO,eAAA,CAAgB,OAAO,MAAM,CAAA;AAAA,IAC/D,gBAAA,EAAkB,CAAA,MAAA,KAAU,MAAA,CAAO,gBAAA,CAAiB,OAAO,MAAM;AAAA,GACpE,CAAA;AACL;AAEA,SAAS,cACL,IAAA,EAC+D;AAC/D,EAAA,OACI,CAAC,CAAC,IAAA,KACD,OAAO,IAAA,KAAS,QAAA,IAAY,OAAO,IAAA,KAAS,UAAA,CAAA,IAC7C,OAAQ,IAAA,CAA8B,IAAA,KAAS,UAAA;AAEvD","file":"index.node.cjs","sourcesContent":["import { type Address, isProgramDerivedAddress, type ProgramDerivedAddress } from '@solana/addresses';\nimport {\n SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_NON_NULL,\n SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE,\n SolanaError,\n} from '@solana/errors';\nimport { type AccountMeta, AccountRole, upgradeRoleToSigner } from '@solana/instructions';\nimport { type AccountSignerMeta, isTransactionSigner, type TransactionSigner } from '@solana/signers';\n\n/**\n * Ensures a resolved instruction input is not null or undefined.\n *\n * This function is used during instruction resolution to validate that\n * required inputs have been properly resolved to a non-null value.\n *\n * @typeParam T - The expected type of the resolved input value.\n *\n * @param inputName - The name of the instruction input, used in error messages.\n * @param value - The resolved value to validate.\n * @returns The validated non-null value.\n *\n * @throws Throws a {@link SolanaError} if the value is null or undefined.\n *\n * @example\n * ```ts\n * const resolvedAuthority = getNonNullResolvedInstructionInput(\n * 'authority',\n * maybeAuthority\n * );\n * // resolvedAuthority is guaranteed to be non-null here.\n * ```\n */\nexport function getNonNullResolvedInstructionInput<T>(inputName: string, value: T | null | undefined): T {\n if (value === null || value === undefined) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_NON_NULL, {\n inputName,\n });\n }\n return value;\n}\n\n/**\n * Extracts the address from a resolved instruction account.\n *\n * A resolved instruction account can be an {@link Address}, a {@link ProgramDerivedAddress},\n * or a {@link TransactionSigner}. This function extracts the underlying address from\n * any of these types.\n *\n * @typeParam T - The address type, defaults to `string`.\n *\n * @param inputName - The name of the instruction input, used in error messages.\n * @param value - The resolved account value to extract the address from.\n * @returns The extracted address.\n *\n * @throws Throws a {@link SolanaError} if the value is null or undefined.\n *\n * @example\n * ```ts\n * const address = getAddressFromResolvedInstructionAccount('mint', resolvedMint);\n * ```\n */\nexport function getAddressFromResolvedInstructionAccount<T extends string = string>(\n inputName: string,\n value: ResolvedInstructionAccount<T>['value'] | undefined,\n): Address<T> {\n const nonNullValue = getNonNullResolvedInstructionInput(inputName, value);\n if (typeof value === 'object' && 'address' in nonNullValue) {\n return nonNullValue.address;\n }\n if (Array.isArray(nonNullValue)) {\n return nonNullValue[0] as Address<T>;\n }\n return nonNullValue as Address<T>;\n}\n\n/**\n * Extracts a {@link ProgramDerivedAddress} from a resolved instruction account.\n *\n * This function validates that the resolved account is a PDA and returns it.\n * Use this when you need access to both the address and the bump seed of a PDA.\n *\n * @typeParam T - The address type, defaults to `string`.\n *\n * @param inputName - The name of the instruction input, used in error messages.\n * @param value - The resolved account value expected to be a PDA.\n * @returns The program-derived address.\n *\n * @throws Throws a {@link SolanaError} if the value is not a {@link ProgramDerivedAddress}.\n *\n * @example\n * ```ts\n * const pda = getResolvedInstructionAccountAsProgramDerivedAddress('metadata', resolvedMetadata);\n * const [address, bump] = pda;\n * ```\n */\nexport function getResolvedInstructionAccountAsProgramDerivedAddress<T extends string = string>(\n inputName: string,\n value: ResolvedInstructionAccount<T>['value'] | undefined,\n): ProgramDerivedAddress<T> {\n if (!isProgramDerivedAddress(value)) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE, {\n expectedType: 'ProgramDerivedAddress',\n inputName,\n });\n }\n return value;\n}\n\n/**\n * Extracts a {@link TransactionSigner} from a resolved instruction account.\n *\n * This function validates that the resolved account is a transaction signer and returns it.\n * Use this when you need the resolved account to be a signer.\n *\n * @typeParam T - The address type, defaults to `string`.\n *\n * @param inputName - The name of the instruction input, used in error messages.\n * @param value - The resolved account value expected to be a signer.\n * @returns The transaction signer.\n *\n * @throws Throws a {@link SolanaError} if the value is not a {@link TransactionSigner}.\n *\n * @example\n * ```ts\n * const signer = getResolvedInstructionAccountAsTransactionSigner('authority', resolvedAuthority);\n * ```\n */\nexport function getResolvedInstructionAccountAsTransactionSigner<T extends string = string>(\n inputName: string,\n value: ResolvedInstructionAccount<T>['value'] | undefined,\n): TransactionSigner<T> {\n if (!isResolvedInstructionAccountSigner(value)) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE, {\n expectedType: 'TransactionSigner',\n inputName,\n });\n }\n return value;\n}\n\n/**\n * Represents a resolved account input for an instruction.\n *\n * During instruction building, account inputs are resolved to this type which\n * captures both the account value and whether it should be marked as writable.\n * The value can be an {@link Address}, a {@link ProgramDerivedAddress}, a\n * {@link TransactionSigner}, or `null` for optional accounts.\n *\n * @typeParam TAddress - The address type, defaults to `string`.\n * @typeParam TValue - The type of the resolved value.\n *\n * @example\n * ```ts\n * const mintAccount: ResolvedInstructionAccount = {\n * value: mintAddress,\n * isWritable: true,\n * };\n * ```\n */\nexport type ResolvedInstructionAccount<\n TAddress extends string = string,\n TValue extends Address<TAddress> | ProgramDerivedAddress<TAddress> | TransactionSigner<TAddress> | null =\n | Address<TAddress>\n | ProgramDerivedAddress<TAddress>\n | TransactionSigner<TAddress>\n | null,\n> = {\n isWritable: boolean;\n value: TValue;\n};\n\n/**\n * Creates a factory function that converts resolved instruction accounts to account metas.\n *\n * The factory handles the conversion of {@link ResolvedInstructionAccount} objects into\n * {@link AccountMeta} or {@link AccountSignerMeta} objects suitable for building instructions.\n * It also determines how to handle optional accounts based on the provided strategy.\n *\n * @param programAddress - The program address, used when optional accounts use the `programId` strategy.\n * @param optionalAccountStrategy - How to handle null account values:\n * - `'omitted'`: Optional accounts are excluded from the instruction entirely.\n * - `'programId'`: Optional accounts are replaced with the program address as a read-only account.\n * @returns A factory function that converts a resolved account to an account meta.\n *\n * @example\n * ```ts\n * const toAccountMeta = getAccountMetaFactory(programAddress, 'programId');\n * const mintMeta = toAccountMeta('mint', resolvedMint);\n * ```\n */\nexport function getAccountMetaFactory(programAddress: Address, optionalAccountStrategy: 'omitted' | 'programId') {\n return (inputName: string, account: ResolvedInstructionAccount): AccountMeta | AccountSignerMeta | undefined => {\n if (!account.value) {\n if (optionalAccountStrategy === 'omitted') return;\n return Object.freeze({ address: programAddress, role: AccountRole.READONLY });\n }\n\n const writableRole = account.isWritable ? AccountRole.WRITABLE : AccountRole.READONLY;\n const isSigner = isResolvedInstructionAccountSigner(account.value);\n return Object.freeze({\n address: getAddressFromResolvedInstructionAccount(inputName, account.value),\n role: isSigner ? upgradeRoleToSigner(writableRole) : writableRole,\n ...(isSigner ? { signer: account.value } : {}),\n });\n };\n}\n\nfunction isResolvedInstructionAccountSigner(value: unknown): value is TransactionSigner {\n return (\n !!value &&\n typeof value === 'object' &&\n 'address' in value &&\n typeof value.address === 'string' &&\n isTransactionSigner(value as { address: Address })\n );\n}\n","import {\n type Account,\n assertAccountExists,\n assertAccountsExist,\n decodeAccount,\n type FetchAccountConfig,\n type FetchAccountsConfig,\n fetchEncodedAccount,\n fetchEncodedAccounts,\n type MaybeAccount,\n} from '@solana/accounts';\nimport type { Address } from '@solana/addresses';\nimport type { Codec } from '@solana/codecs-core';\nimport type { ClientWithRpc } from '@solana/plugin-interfaces';\nimport type { GetAccountInfoApi, GetMultipleAccountsApi } from '@solana/rpc-api';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype AnyObjectCodec = Codec<any, object>;\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferTFrom<T> = T extends Codec<infer TFrom, any> ? TFrom : never;\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferTTo<T> = T extends Codec<any, infer TTo> ? TTo : never;\n\n/**\n * Methods that allow a codec to fetch and decode accounts directly.\n *\n * These methods are added to codec objects via {@link addSelfFetchFunctions},\n * enabling a fluent API where you can call `.fetch()` directly on a codec\n * to retrieve and decode accounts in one step.\n *\n * @typeParam TFrom - The type that the codec encodes from.\n * @typeParam TTo - The type that the codec decodes to.\n *\n * @example\n * Fetching a single account and asserting it exists.\n * ```ts\n * const account = await myAccountCodec.fetch(address);\n * // account.data is of type TTo.\n * ```\n *\n * @example\n * Fetching a single account that may not exist.\n * ```ts\n * const maybeAccount = await myAccountCodec.fetchMaybe(address);\n * if (maybeAccount.exists) {\n * // maybeAccount.data is of type TTo.\n * }\n * ```\n *\n * @example\n * Fetching multiple accounts at once.\n * ```ts\n * const accounts = await myAccountCodec.fetchAll([addressA, addressB]);\n * // All accounts exist.\n * ```\n *\n * @see {@link addSelfFetchFunctions}\n */\nexport type SelfFetchFunctions<TFrom extends object, TTo extends TFrom> = {\n /** Fetches and decodes a single account, throwing if it does not exist. */\n readonly fetch: <TAddress extends string>(\n address: Address<TAddress>,\n config?: FetchAccountConfig,\n ) => Promise<Account<TTo, TAddress>>;\n /** Fetches and decodes multiple accounts, throwing if any do not exist. */\n readonly fetchAll: (addresses: Address[], config?: FetchAccountsConfig) => Promise<Account<TTo>[]>;\n /** Fetches and decodes multiple accounts, returning {@link MaybeAccount} for each. */\n readonly fetchAllMaybe: (addresses: Address[], config?: FetchAccountsConfig) => Promise<MaybeAccount<TTo>[]>;\n /** Fetches and decodes a single account, returning a {@link MaybeAccount}. */\n readonly fetchMaybe: <TAddress extends string>(\n address: Address<TAddress>,\n config?: FetchAccountConfig,\n ) => Promise<MaybeAccount<TTo, TAddress>>;\n};\n\n/**\n * Adds self-fetching methods to a codec for retrieving and decoding accounts.\n *\n * This function augments the provided codec with methods that allow it to fetch\n * accounts from the network and decode them in one step. It enables a fluent API\n * where you can call methods like `.fetch()` directly on the codec.\n *\n * @typeParam TFrom - The type that the codec encodes from.\n * @typeParam TTo - The type that the codec decodes to.\n * @typeParam TCodec - The codec type being augmented.\n *\n * @param client - A client that provides RPC access for fetching accounts.\n * @param codec - The codec to augment with self-fetch methods.\n * @returns The codec augmented with {@link SelfFetchFunctions} methods.\n *\n * @example\n * Adding self-fetch functions to an account codec.\n * ```ts\n * import { addSelfFetchFunctions } from '@solana/program-client-core';\n *\n * const myAccountCodec = addSelfFetchFunctions(client, getMyAccountCodec());\n *\n * // Fetch and decode an account in one step.\n * const account = await myAccountCodec.fetch(accountAddress);\n * ```\n *\n * @example\n * Handling accounts that may not exist.\n * ```ts\n * const myAccountCodec = addSelfFetchFunctions(client, getMyAccountCodec());\n *\n * const maybeAccount = await myAccountCodec.fetchMaybe(accountAddress);\n * if (maybeAccount.exists) {\n * console.log('Account data:', maybeAccount.data);\n * } else {\n * console.log(`Account ${maybeAccount.address} does not exist`);\n * }\n * ```\n *\n * @example\n * Fetching multiple accounts at once.\n * ```ts\n * const myAccountCodec = addSelfFetchFunctions(client, getMyAccountCodec());\n *\n * // Throws if any account does not exist.\n * const accounts = await myAccountCodec.fetchAll([addressA, addressB, addressC]);\n *\n * // Returns MaybeAccount for each, allowing some to not exist.\n * const maybeAccounts = await myAccountCodec.fetchAllMaybe([addressA, addressB]);\n * ```\n *\n * @see {@link SelfFetchFunctions}\n */\nexport function addSelfFetchFunctions<TCodec extends AnyObjectCodec>(\n client: ClientWithRpc<GetAccountInfoApi & GetMultipleAccountsApi>,\n codec: TCodec,\n): SelfFetchFunctions<InferTFrom<TCodec>, InferTTo<TCodec>> & TCodec {\n type Functions = SelfFetchFunctions<InferTFrom<TCodec>, InferTTo<TCodec>>;\n type InferredCodec = Codec<InferTFrom<TCodec>, InferTTo<TCodec>>;\n const fetchMaybe: Functions['fetchMaybe'] = async (address, config?) => {\n const maybeAccount = await fetchEncodedAccount(client.rpc, address, config);\n return decodeAccount(maybeAccount, codec as InferredCodec);\n };\n const fetchAllMaybe: Functions['fetchAllMaybe'] = async (addresses, config?) => {\n const maybeAccounts = await fetchEncodedAccounts(client.rpc, addresses, config);\n return maybeAccounts.map(maybeAccount => decodeAccount(maybeAccount, codec as InferredCodec));\n };\n const fetch: Functions['fetch'] = async (address, config?) => {\n const maybeAccount = await fetchMaybe(address, config);\n assertAccountExists(maybeAccount);\n return maybeAccount;\n };\n const fetchAll: Functions['fetchAll'] = async (addresses, config?) => {\n const maybeAccounts = await fetchAllMaybe(addresses, config);\n assertAccountsExist(maybeAccounts);\n return maybeAccounts;\n };\n\n const out = { ...codec, fetch, fetchAll, fetchAllMaybe, fetchMaybe };\n return Object.freeze<typeof out>(out);\n}\n","import type { InstructionPlan } from '@solana/instruction-plans';\nimport type { Instruction } from '@solana/instructions';\nimport type { ClientWithTransactionPlanning, ClientWithTransactionSending } from '@solana/plugin-interfaces';\n\ntype PlanTransaction = ClientWithTransactionPlanning['planTransaction'];\ntype PlanTransactions = ClientWithTransactionPlanning['planTransactions'];\ntype SendTransaction = ClientWithTransactionSending['sendTransaction'];\ntype SendTransactions = ClientWithTransactionSending['sendTransactions'];\n\n/**\n * Methods that allow an instruction or instruction plan to plan and send itself.\n *\n * These methods are added to instruction or instruction plan objects via\n * {@link addSelfPlanAndSendFunctions}, enabling a fluent API where you can call\n * `.sendTransaction()` directly on an instruction without passing it to a separate function.\n *\n * @example\n * Sending a transfer instruction directly.\n * ```ts\n * const result = await getTransferInstruction({ source, destination, amount }).sendTransaction();\n * ```\n *\n * @example\n * Planning multiple transactions from an instruction plan.\n * ```ts\n * const plan = await getComplexInstructionPlan(/* ... *\\/).planTransactions();\n * ```\n *\n * @see {@link addSelfPlanAndSendFunctions}\n */\nexport type SelfPlanAndSendFunctions = {\n /** Plans a single transaction. */\n planTransaction: (config?: Parameters<PlanTransaction>[1]) => ReturnType<PlanTransaction>;\n /** Plans one or more transactions. */\n planTransactions: (config?: Parameters<PlanTransactions>[1]) => ReturnType<PlanTransactions>;\n /** Sends a single transaction. */\n sendTransaction: (config?: Parameters<SendTransaction>[1]) => ReturnType<SendTransaction>;\n /** Sends one or more transactions. */\n sendTransactions: (config?: Parameters<SendTransactions>[1]) => ReturnType<SendTransactions>;\n};\n\n/**\n * Adds self-planning and self-sending methods to an instruction or instruction plan.\n *\n * This function augments the provided instruction or instruction plan with methods\n * that allow it to plan and send itself using the provided client. It enables a fluent API\n * where you can call methods like `.sendTransaction()` directly on the instruction.\n *\n * The function supports both synchronous inputs (instructions, instruction plans) and\n * promise-like inputs, making it suitable for use with async instruction builders.\n *\n * @typeParam TItem - The type of the instruction, instruction plan, or a promise resolving to one.\n *\n * @param client - A client that provides transaction planning and sending capabilities.\n * @param input - The instruction, instruction plan, or promise to augment with self-plan/send methods.\n * @returns The input augmented with {@link SelfPlanAndSendFunctions} methods.\n *\n * @example\n * Adding self-plan and send to a transfer instruction.\n * ```ts\n * import { addSelfPlanAndSendFunctions } from '@solana/program-client-core';\n *\n * const transferInstruction = addSelfPlanAndSendFunctions(\n * client,\n * getTransferInstruction({ payer, source, destination, amount })\n * );\n *\n * // Now you can send directly from the instruction.\n * const result = await transferInstruction.sendTransaction();\n * ```\n *\n * @example\n * Using with an async instruction builder.\n * ```ts\n * const asyncInstruction = addSelfPlanAndSendFunctions(\n * client,\n * fetchAndBuildInstruction(/* ... *\\/)\n * );\n *\n * // The promise is augmented with self-plan/send methods.\n * const result = await asyncInstruction.sendTransaction();\n * ```\n *\n * @see {@link SelfPlanAndSendFunctions}\n */\nexport function addSelfPlanAndSendFunctions<\n TItem extends Instruction | InstructionPlan | PromiseLike<Instruction> | PromiseLike<InstructionPlan>,\n>(\n client: ClientWithTransactionPlanning & ClientWithTransactionSending,\n input: TItem,\n): SelfPlanAndSendFunctions & TItem {\n if (isPromiseLike(input)) {\n const newInput = input as SelfPlanAndSendFunctions & TItem;\n newInput.planTransaction = async config => await client.planTransaction(await input, config);\n newInput.planTransactions = async config => await client.planTransactions(await input, config);\n newInput.sendTransaction = async config => await client.sendTransaction(await input, config);\n newInput.sendTransactions = async config => await client.sendTransactions(await input, config);\n return newInput;\n }\n\n return Object.freeze(<SelfPlanAndSendFunctions & (Instruction | InstructionPlan)>{\n ...input,\n planTransaction: config => client.planTransaction(input, config),\n planTransactions: config => client.planTransactions(input, config),\n sendTransaction: config => client.sendTransaction(input, config),\n sendTransactions: config => client.sendTransactions(input, config),\n }) as unknown as SelfPlanAndSendFunctions & TItem;\n}\n\nfunction isPromiseLike(\n item: Instruction | InstructionPlan | PromiseLike<Instruction> | PromiseLike<InstructionPlan>,\n): item is PromiseLike<Instruction> | PromiseLike<InstructionPlan> {\n return (\n !!item &&\n (typeof item === 'object' || typeof item === 'function') &&\n typeof (item as PromiseLike<unknown>).then === 'function'\n );\n}\n"]}
@@ -0,0 +1,109 @@
1
+ import { isProgramDerivedAddress } from '@solana/addresses';
2
+ import { SolanaError, SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_NON_NULL, SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE } from '@solana/errors';
3
+ import { AccountRole, upgradeRoleToSigner } from '@solana/instructions';
4
+ import { isTransactionSigner } from '@solana/signers';
5
+ import { fetchEncodedAccount, decodeAccount, fetchEncodedAccounts, assertAccountExists, assertAccountsExist } from '@solana/accounts';
6
+
7
+ // src/instruction-input-resolution.ts
8
+ function getNonNullResolvedInstructionInput(inputName, value) {
9
+ if (value === null || value === void 0) {
10
+ throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_NON_NULL, {
11
+ inputName
12
+ });
13
+ }
14
+ return value;
15
+ }
16
+ function getAddressFromResolvedInstructionAccount(inputName, value) {
17
+ const nonNullValue = getNonNullResolvedInstructionInput(inputName, value);
18
+ if (typeof value === "object" && "address" in nonNullValue) {
19
+ return nonNullValue.address;
20
+ }
21
+ if (Array.isArray(nonNullValue)) {
22
+ return nonNullValue[0];
23
+ }
24
+ return nonNullValue;
25
+ }
26
+ function getResolvedInstructionAccountAsProgramDerivedAddress(inputName, value) {
27
+ if (!isProgramDerivedAddress(value)) {
28
+ throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE, {
29
+ expectedType: "ProgramDerivedAddress",
30
+ inputName
31
+ });
32
+ }
33
+ return value;
34
+ }
35
+ function getResolvedInstructionAccountAsTransactionSigner(inputName, value) {
36
+ if (!isResolvedInstructionAccountSigner(value)) {
37
+ throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE, {
38
+ expectedType: "TransactionSigner",
39
+ inputName
40
+ });
41
+ }
42
+ return value;
43
+ }
44
+ function getAccountMetaFactory(programAddress, optionalAccountStrategy) {
45
+ return (inputName, account) => {
46
+ if (!account.value) {
47
+ if (optionalAccountStrategy === "omitted") return;
48
+ return Object.freeze({ address: programAddress, role: AccountRole.READONLY });
49
+ }
50
+ const writableRole = account.isWritable ? AccountRole.WRITABLE : AccountRole.READONLY;
51
+ const isSigner = isResolvedInstructionAccountSigner(account.value);
52
+ return Object.freeze({
53
+ address: getAddressFromResolvedInstructionAccount(inputName, account.value),
54
+ role: isSigner ? upgradeRoleToSigner(writableRole) : writableRole,
55
+ ...isSigner ? { signer: account.value } : {}
56
+ });
57
+ };
58
+ }
59
+ function isResolvedInstructionAccountSigner(value) {
60
+ return !!value && typeof value === "object" && "address" in value && typeof value.address === "string" && isTransactionSigner(value);
61
+ }
62
+ function addSelfFetchFunctions(client, codec) {
63
+ const fetchMaybe = async (address, config) => {
64
+ const maybeAccount = await fetchEncodedAccount(client.rpc, address, config);
65
+ return decodeAccount(maybeAccount, codec);
66
+ };
67
+ const fetchAllMaybe = async (addresses, config) => {
68
+ const maybeAccounts = await fetchEncodedAccounts(client.rpc, addresses, config);
69
+ return maybeAccounts.map((maybeAccount) => decodeAccount(maybeAccount, codec));
70
+ };
71
+ const fetch = async (address, config) => {
72
+ const maybeAccount = await fetchMaybe(address, config);
73
+ assertAccountExists(maybeAccount);
74
+ return maybeAccount;
75
+ };
76
+ const fetchAll = async (addresses, config) => {
77
+ const maybeAccounts = await fetchAllMaybe(addresses, config);
78
+ assertAccountsExist(maybeAccounts);
79
+ return maybeAccounts;
80
+ };
81
+ const out = { ...codec, fetch, fetchAll, fetchAllMaybe, fetchMaybe };
82
+ return Object.freeze(out);
83
+ }
84
+
85
+ // src/self-plan-and-send-functions.ts
86
+ function addSelfPlanAndSendFunctions(client, input) {
87
+ if (isPromiseLike(input)) {
88
+ const newInput = input;
89
+ newInput.planTransaction = async (config) => await client.planTransaction(await input, config);
90
+ newInput.planTransactions = async (config) => await client.planTransactions(await input, config);
91
+ newInput.sendTransaction = async (config) => await client.sendTransaction(await input, config);
92
+ newInput.sendTransactions = async (config) => await client.sendTransactions(await input, config);
93
+ return newInput;
94
+ }
95
+ return Object.freeze({
96
+ ...input,
97
+ planTransaction: (config) => client.planTransaction(input, config),
98
+ planTransactions: (config) => client.planTransactions(input, config),
99
+ sendTransaction: (config) => client.sendTransaction(input, config),
100
+ sendTransactions: (config) => client.sendTransactions(input, config)
101
+ });
102
+ }
103
+ function isPromiseLike(item) {
104
+ return !!item && (typeof item === "object" || typeof item === "function") && typeof item.then === "function";
105
+ }
106
+
107
+ export { addSelfFetchFunctions, addSelfPlanAndSendFunctions, getAccountMetaFactory, getAddressFromResolvedInstructionAccount, getNonNullResolvedInstructionInput, getResolvedInstructionAccountAsProgramDerivedAddress, getResolvedInstructionAccountAsTransactionSigner };
108
+ //# sourceMappingURL=index.node.mjs.map
109
+ //# sourceMappingURL=index.node.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/instruction-input-resolution.ts","../src/self-fetch-functions.ts","../src/self-plan-and-send-functions.ts"],"names":[],"mappings":";;;;;;;AAgCO,SAAS,kCAAA,CAAsC,WAAmB,KAAA,EAAgC;AACrG,EAAA,IAAI,KAAA,KAAU,IAAA,IAAQ,KAAA,KAAU,MAAA,EAAW;AACvC,IAAA,MAAM,IAAI,YAAY,0EAAA,EAA4E;AAAA,MAC9F;AAAA,KACH,CAAA;AAAA,EACL;AACA,EAAA,OAAO,KAAA;AACX;AAsBO,SAAS,wCAAA,CACZ,WACA,KAAA,EACU;AACV,EAAA,MAAM,YAAA,GAAe,kCAAA,CAAmC,SAAA,EAAW,KAAK,CAAA;AACxE,EAAA,IAAI,OAAO,KAAA,KAAU,QAAA,IAAY,SAAA,IAAa,YAAA,EAAc;AACxD,IAAA,OAAO,YAAA,CAAa,OAAA;AAAA,EACxB;AACA,EAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,YAAY,CAAA,EAAG;AAC7B,IAAA,OAAO,aAAa,CAAC,CAAA;AAAA,EACzB;AACA,EAAA,OAAO,YAAA;AACX;AAsBO,SAAS,oDAAA,CACZ,WACA,KAAA,EACwB;AACxB,EAAA,IAAI,CAAC,uBAAA,CAAwB,KAAK,CAAA,EAAG;AACjC,IAAA,MAAM,IAAI,YAAY,yEAAA,EAA2E;AAAA,MAC7F,YAAA,EAAc,uBAAA;AAAA,MACd;AAAA,KACH,CAAA;AAAA,EACL;AACA,EAAA,OAAO,KAAA;AACX;AAqBO,SAAS,gDAAA,CACZ,WACA,KAAA,EACoB;AACpB,EAAA,IAAI,CAAC,kCAAA,CAAmC,KAAK,CAAA,EAAG;AAC5C,IAAA,MAAM,IAAI,YAAY,yEAAA,EAA2E;AAAA,MAC7F,YAAA,EAAc,mBAAA;AAAA,MACd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A,CAAS,eAAA,GAAkB,OAAM,MAAA,KAAU,MAAM,OAAO,eAAA,CAAgB,MAAM,OAAO,MAAM,CAAA;AAC3F,IAAA,QAAA,CAAS,gBAAA,GAAmB,OAAM,MAAA,KAAU,MAAM,OAAO,gBAAA,CAAiB,MAAM,OAAO,MAAM,CAAA;AAC7F,IAAA,OAAO,QAAA;AAAA,EACX;AAEA,EAAA,OAAO,OAAO,MAAA,CAAmE;AAAA,IAC7E,GAAG,KAAA;AAAA,IACH,eAAA,EAAiB,CAAA,MAAA,KAAU,MAAA,CAAO,eAAA,CAAgB,OAAO,MAAM,CAAA;AAAA,IAC/D,gBAAA,EAAkB,CAAA,MAAA,KAAU,MAAA,CAAO,gBAAA,CAAiB,OAAO,MAAM,CAAA;AAAA,IACjE,eAAA,EAAiB,CAAA,MAAA,KAAU,MAAA,CAAO,eAAA,CAAgB,OAAO,MAAM,CAAA;AAAA,IAC/D,gBAAA,EAAkB,CAAA,MAAA,KAAU,MAAA,CAAO,gBAAA,CAAiB,OAAO,MAAM;AAAA,GACpE,CAAA;AACL;AAEA,SAAS,cACL,IAAA,EAC+D;AAC/D,EAAA,OACI,CAAC,CAAC,IAAA,KACD,OAAO,IAAA,KAAS,QAAA,IAAY,OAAO,IAAA,KAAS,UAAA,CAAA,IAC7C,OAAQ,IAAA,CAA8B,IAAA,KAAS,UAAA;AAEvD","file":"index.node.mjs","sourcesContent":["import { type Address, isProgramDerivedAddress, type ProgramDerivedAddress } from '@solana/addresses';\nimport {\n SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_NON_NULL,\n SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE,\n SolanaError,\n} from '@solana/errors';\nimport { type AccountMeta, AccountRole, upgradeRoleToSigner } from '@solana/instructions';\nimport { type AccountSignerMeta, isTransactionSigner, type TransactionSigner } from '@solana/signers';\n\n/**\n * Ensures a resolved instruction input is not null or undefined.\n *\n * This function is used during instruction resolution to validate that\n * required inputs have been properly resolved to a non-null value.\n *\n * @typeParam T - The expected type of the resolved input value.\n *\n * @param inputName - The name of the instruction input, used in error messages.\n * @param value - The resolved value to validate.\n * @returns The validated non-null value.\n *\n * @throws Throws a {@link SolanaError} if the value is null or undefined.\n *\n * @example\n * ```ts\n * const resolvedAuthority = getNonNullResolvedInstructionInput(\n * 'authority',\n * maybeAuthority\n * );\n * // resolvedAuthority is guaranteed to be non-null here.\n * ```\n */\nexport function getNonNullResolvedInstructionInput<T>(inputName: string, value: T | null | undefined): T {\n if (value === null || value === undefined) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_NON_NULL, {\n inputName,\n });\n }\n return value;\n}\n\n/**\n * Extracts the address from a resolved instruction account.\n *\n * A resolved instruction account can be an {@link Address}, a {@link ProgramDerivedAddress},\n * or a {@link TransactionSigner}. This function extracts the underlying address from\n * any of these types.\n *\n * @typeParam T - The address type, defaults to `string`.\n *\n * @param inputName - The name of the instruction input, used in error messages.\n * @param value - The resolved account value to extract the address from.\n * @returns The extracted address.\n *\n * @throws Throws a {@link SolanaError} if the value is null or undefined.\n *\n * @example\n * ```ts\n * const address = getAddressFromResolvedInstructionAccount('mint', resolvedMint);\n * ```\n */\nexport function getAddressFromResolvedInstructionAccount<T extends string = string>(\n inputName: string,\n value: ResolvedInstructionAccount<T>['value'] | undefined,\n): Address<T> {\n const nonNullValue = getNonNullResolvedInstructionInput(inputName, value);\n if (typeof value === 'object' && 'address' in nonNullValue) {\n return nonNullValue.address;\n }\n if (Array.isArray(nonNullValue)) {\n return nonNullValue[0] as Address<T>;\n }\n return nonNullValue as Address<T>;\n}\n\n/**\n * Extracts a {@link ProgramDerivedAddress} from a resolved instruction account.\n *\n * This function validates that the resolved account is a PDA and returns it.\n * Use this when you need access to both the address and the bump seed of a PDA.\n *\n * @typeParam T - The address type, defaults to `string`.\n *\n * @param inputName - The name of the instruction input, used in error messages.\n * @param value - The resolved account value expected to be a PDA.\n * @returns The program-derived address.\n *\n * @throws Throws a {@link SolanaError} if the value is not a {@link ProgramDerivedAddress}.\n *\n * @example\n * ```ts\n * const pda = getResolvedInstructionAccountAsProgramDerivedAddress('metadata', resolvedMetadata);\n * const [address, bump] = pda;\n * ```\n */\nexport function getResolvedInstructionAccountAsProgramDerivedAddress<T extends string = string>(\n inputName: string,\n value: ResolvedInstructionAccount<T>['value'] | undefined,\n): ProgramDerivedAddress<T> {\n if (!isProgramDerivedAddress(value)) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE, {\n expectedType: 'ProgramDerivedAddress',\n inputName,\n });\n }\n return value;\n}\n\n/**\n * Extracts a {@link TransactionSigner} from a resolved instruction account.\n *\n * This function validates that the resolved account is a transaction signer and returns it.\n * Use this when you need the resolved account to be a signer.\n *\n * @typeParam T - The address type, defaults to `string`.\n *\n * @param inputName - The name of the instruction input, used in error messages.\n * @param value - The resolved account value expected to be a signer.\n * @returns The transaction signer.\n *\n * @throws Throws a {@link SolanaError} if the value is not a {@link TransactionSigner}.\n *\n * @example\n * ```ts\n * const signer = getResolvedInstructionAccountAsTransactionSigner('authority', resolvedAuthority);\n * ```\n */\nexport function getResolvedInstructionAccountAsTransactionSigner<T extends string = string>(\n inputName: string,\n value: ResolvedInstructionAccount<T>['value'] | undefined,\n): TransactionSigner<T> {\n if (!isResolvedInstructionAccountSigner(value)) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE, {\n expectedType: 'TransactionSigner',\n inputName,\n });\n }\n return value;\n}\n\n/**\n * Represents a resolved account input for an instruction.\n *\n * During instruction building, account inputs are resolved to this type which\n * captures both the account value and whether it should be marked as writable.\n * The value can be an {@link Address}, a {@link ProgramDerivedAddress}, a\n * {@link TransactionSigner}, or `null` for optional accounts.\n *\n * @typeParam TAddress - The address type, defaults to `string`.\n * @typeParam TValue - The type of the resolved value.\n *\n * @example\n * ```ts\n * const mintAccount: ResolvedInstructionAccount = {\n * value: mintAddress,\n * isWritable: true,\n * };\n * ```\n */\nexport type ResolvedInstructionAccount<\n TAddress extends string = string,\n TValue extends Address<TAddress> | ProgramDerivedAddress<TAddress> | TransactionSigner<TAddress> | null =\n | Address<TAddress>\n | ProgramDerivedAddress<TAddress>\n | TransactionSigner<TAddress>\n | null,\n> = {\n isWritable: boolean;\n value: TValue;\n};\n\n/**\n * Creates a factory function that converts resolved instruction accounts to account metas.\n *\n * The factory handles the conversion of {@link ResolvedInstructionAccount} objects into\n * {@link AccountMeta} or {@link AccountSignerMeta} objects suitable for building instructions.\n * It also determines how to handle optional accounts based on the provided strategy.\n *\n * @param programAddress - The program address, used when optional accounts use the `programId` strategy.\n * @param optionalAccountStrategy - How to handle null account values:\n * - `'omitted'`: Optional accounts are excluded from the instruction entirely.\n * - `'programId'`: Optional accounts are replaced with the program address as a read-only account.\n * @returns A factory function that converts a resolved account to an account meta.\n *\n * @example\n * ```ts\n * const toAccountMeta = getAccountMetaFactory(programAddress, 'programId');\n * const mintMeta = toAccountMeta('mint', resolvedMint);\n * ```\n */\nexport function getAccountMetaFactory(programAddress: Address, optionalAccountStrategy: 'omitted' | 'programId') {\n return (inputName: string, account: ResolvedInstructionAccount): AccountMeta | AccountSignerMeta | undefined => {\n if (!account.value) {\n if (optionalAccountStrategy === 'omitted') return;\n return Object.freeze({ address: programAddress, role: AccountRole.READONLY });\n }\n\n const writableRole = account.isWritable ? AccountRole.WRITABLE : AccountRole.READONLY;\n const isSigner = isResolvedInstructionAccountSigner(account.value);\n return Object.freeze({\n address: getAddressFromResolvedInstructionAccount(inputName, account.value),\n role: isSigner ? upgradeRoleToSigner(writableRole) : writableRole,\n ...(isSigner ? { signer: account.value } : {}),\n });\n };\n}\n\nfunction isResolvedInstructionAccountSigner(value: unknown): value is TransactionSigner {\n return (\n !!value &&\n typeof value === 'object' &&\n 'address' in value &&\n typeof value.address === 'string' &&\n isTransactionSigner(value as { address: Address })\n );\n}\n","import {\n type Account,\n assertAccountExists,\n assertAccountsExist,\n decodeAccount,\n type FetchAccountConfig,\n type FetchAccountsConfig,\n fetchEncodedAccount,\n fetchEncodedAccounts,\n type MaybeAccount,\n} from '@solana/accounts';\nimport type { Address } from '@solana/addresses';\nimport type { Codec } from '@solana/codecs-core';\nimport type { ClientWithRpc } from '@solana/plugin-interfaces';\nimport type { GetAccountInfoApi, GetMultipleAccountsApi } from '@solana/rpc-api';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype AnyObjectCodec = Codec<any, object>;\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferTFrom<T> = T extends Codec<infer TFrom, any> ? TFrom : never;\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype InferTTo<T> = T extends Codec<any, infer TTo> ? TTo : never;\n\n/**\n * Methods that allow a codec to fetch and decode accounts directly.\n *\n * These methods are added to codec objects via {@link addSelfFetchFunctions},\n * enabling a fluent API where you can call `.fetch()` directly on a codec\n * to retrieve and decode accounts in one step.\n *\n * @typeParam TFrom - The type that the codec encodes from.\n * @typeParam TTo - The type that the codec decodes to.\n *\n * @example\n * Fetching a single account and asserting it exists.\n * ```ts\n * const account = await myAccountCodec.fetch(address);\n * // account.data is of type TTo.\n * ```\n *\n * @example\n * Fetching a single account that may not exist.\n * ```ts\n * const maybeAccount = await myAccountCodec.fetchMaybe(address);\n * if (maybeAccount.exists) {\n * // maybeAccount.data is of type TTo.\n * }\n * ```\n *\n * @example\n * Fetching multiple accounts at once.\n * ```ts\n * const accounts = await myAccountCodec.fetchAll([addressA, addressB]);\n * // All accounts exist.\n * ```\n *\n * @see {@link addSelfFetchFunctions}\n */\nexport type SelfFetchFunctions<TFrom extends object, TTo extends TFrom> = {\n /** Fetches and decodes a single account, throwing if it does not exist. */\n readonly fetch: <TAddress extends string>(\n address: Address<TAddress>,\n config?: FetchAccountConfig,\n ) => Promise<Account<TTo, TAddress>>;\n /** Fetches and decodes multiple accounts, throwing if any do not exist. */\n readonly fetchAll: (addresses: Address[], config?: FetchAccountsConfig) => Promise<Account<TTo>[]>;\n /** Fetches and decodes multiple accounts, returning {@link MaybeAccount} for each. */\n readonly fetchAllMaybe: (addresses: Address[], config?: FetchAccountsConfig) => Promise<MaybeAccount<TTo>[]>;\n /** Fetches and decodes a single account, returning a {@link MaybeAccount}. */\n readonly fetchMaybe: <TAddress extends string>(\n address: Address<TAddress>,\n config?: FetchAccountConfig,\n ) => Promise<MaybeAccount<TTo, TAddress>>;\n};\n\n/**\n * Adds self-fetching methods to a codec for retrieving and decoding accounts.\n *\n * This function augments the provided codec with methods that allow it to fetch\n * accounts from the network and decode them in one step. It enables a fluent API\n * where you can call methods like `.fetch()` directly on the codec.\n *\n * @typeParam TFrom - The type that the codec encodes from.\n * @typeParam TTo - The type that the codec decodes to.\n * @typeParam TCodec - The codec type being augmented.\n *\n * @param client - A client that provides RPC access for fetching accounts.\n * @param codec - The codec to augment with self-fetch methods.\n * @returns The codec augmented with {@link SelfFetchFunctions} methods.\n *\n * @example\n * Adding self-fetch functions to an account codec.\n * ```ts\n * import { addSelfFetchFunctions } from '@solana/program-client-core';\n *\n * const myAccountCodec = addSelfFetchFunctions(client, getMyAccountCodec());\n *\n * // Fetch and decode an account in one step.\n * const account = await myAccountCodec.fetch(accountAddress);\n * ```\n *\n * @example\n * Handling accounts that may not exist.\n * ```ts\n * const myAccountCodec = addSelfFetchFunctions(client, getMyAccountCodec());\n *\n * const maybeAccount = await myAccountCodec.fetchMaybe(accountAddress);\n * if (maybeAccount.exists) {\n * console.log('Account data:', maybeAccount.data);\n * } else {\n * console.log(`Account ${maybeAccount.address} does not exist`);\n * }\n * ```\n *\n * @example\n * Fetching multiple accounts at once.\n * ```ts\n * const myAccountCodec = addSelfFetchFunctions(client, getMyAccountCodec());\n *\n * // Throws if any account does not exist.\n * const accounts = await myAccountCodec.fetchAll([addressA, addressB, addressC]);\n *\n * // Returns MaybeAccount for each, allowing some to not exist.\n * const maybeAccounts = await myAccountCodec.fetchAllMaybe([addressA, addressB]);\n * ```\n *\n * @see {@link SelfFetchFunctions}\n */\nexport function addSelfFetchFunctions<TCodec extends AnyObjectCodec>(\n client: ClientWithRpc<GetAccountInfoApi & GetMultipleAccountsApi>,\n codec: TCodec,\n): SelfFetchFunctions<InferTFrom<TCodec>, InferTTo<TCodec>> & TCodec {\n type Functions = SelfFetchFunctions<InferTFrom<TCodec>, InferTTo<TCodec>>;\n type InferredCodec = Codec<InferTFrom<TCodec>, InferTTo<TCodec>>;\n const fetchMaybe: Functions['fetchMaybe'] = async (address, config?) => {\n const maybeAccount = await fetchEncodedAccount(client.rpc, address, config);\n return decodeAccount(maybeAccount, codec as InferredCodec);\n };\n const fetchAllMaybe: Functions['fetchAllMaybe'] = async (addresses, config?) => {\n const maybeAccounts = await fetchEncodedAccounts(client.rpc, addresses, config);\n return maybeAccounts.map(maybeAccount => decodeAccount(maybeAccount, codec as InferredCodec));\n };\n const fetch: Functions['fetch'] = async (address, config?) => {\n const maybeAccount = await fetchMaybe(address, config);\n assertAccountExists(maybeAccount);\n return maybeAccount;\n };\n const fetchAll: Functions['fetchAll'] = async (addresses, config?) => {\n const maybeAccounts = await fetchAllMaybe(addresses, config);\n assertAccountsExist(maybeAccounts);\n return maybeAccounts;\n };\n\n const out = { ...codec, fetch, fetchAll, fetchAllMaybe, fetchMaybe };\n return Object.freeze<typeof out>(out);\n}\n","import type { InstructionPlan } from '@solana/instruction-plans';\nimport type { Instruction } from '@solana/instructions';\nimport type { ClientWithTransactionPlanning, ClientWithTransactionSending } from '@solana/plugin-interfaces';\n\ntype PlanTransaction = ClientWithTransactionPlanning['planTransaction'];\ntype PlanTransactions = ClientWithTransactionPlanning['planTransactions'];\ntype SendTransaction = ClientWithTransactionSending['sendTransaction'];\ntype SendTransactions = ClientWithTransactionSending['sendTransactions'];\n\n/**\n * Methods that allow an instruction or instruction plan to plan and send itself.\n *\n * These methods are added to instruction or instruction plan objects via\n * {@link addSelfPlanAndSendFunctions}, enabling a fluent API where you can call\n * `.sendTransaction()` directly on an instruction without passing it to a separate function.\n *\n * @example\n * Sending a transfer instruction directly.\n * ```ts\n * const result = await getTransferInstruction({ source, destination, amount }).sendTransaction();\n * ```\n *\n * @example\n * Planning multiple transactions from an instruction plan.\n * ```ts\n * const plan = await getComplexInstructionPlan(/* ... *\\/).planTransactions();\n * ```\n *\n * @see {@link addSelfPlanAndSendFunctions}\n */\nexport type SelfPlanAndSendFunctions = {\n /** Plans a single transaction. */\n planTransaction: (config?: Parameters<PlanTransaction>[1]) => ReturnType<PlanTransaction>;\n /** Plans one or more transactions. */\n planTransactions: (config?: Parameters<PlanTransactions>[1]) => ReturnType<PlanTransactions>;\n /** Sends a single transaction. */\n sendTransaction: (config?: Parameters<SendTransaction>[1]) => ReturnType<SendTransaction>;\n /** Sends one or more transactions. */\n sendTransactions: (config?: Parameters<SendTransactions>[1]) => ReturnType<SendTransactions>;\n};\n\n/**\n * Adds self-planning and self-sending methods to an instruction or instruction plan.\n *\n * This function augments the provided instruction or instruction plan with methods\n * that allow it to plan and send itself using the provided client. It enables a fluent API\n * where you can call methods like `.sendTransaction()` directly on the instruction.\n *\n * The function supports both synchronous inputs (instructions, instruction plans) and\n * promise-like inputs, making it suitable for use with async instruction builders.\n *\n * @typeParam TItem - The type of the instruction, instruction plan, or a promise resolving to one.\n *\n * @param client - A client that provides transaction planning and sending capabilities.\n * @param input - The instruction, instruction plan, or promise to augment with self-plan/send methods.\n * @returns The input augmented with {@link SelfPlanAndSendFunctions} methods.\n *\n * @example\n * Adding self-plan and send to a transfer instruction.\n * ```ts\n * import { addSelfPlanAndSendFunctions } from '@solana/program-client-core';\n *\n * const transferInstruction = addSelfPlanAndSendFunctions(\n * client,\n * getTransferInstruction({ payer, source, destination, amount })\n * );\n *\n * // Now you can send directly from the instruction.\n * const result = await transferInstruction.sendTransaction();\n * ```\n *\n * @example\n * Using with an async instruction builder.\n * ```ts\n * const asyncInstruction = addSelfPlanAndSendFunctions(\n * client,\n * fetchAndBuildInstruction(/* ... *\\/)\n * );\n *\n * // The promise is augmented with self-plan/send methods.\n * const result = await asyncInstruction.sendTransaction();\n * ```\n *\n * @see {@link SelfPlanAndSendFunctions}\n */\nexport function addSelfPlanAndSendFunctions<\n TItem extends Instruction | InstructionPlan | PromiseLike<Instruction> | PromiseLike<InstructionPlan>,\n>(\n client: ClientWithTransactionPlanning & ClientWithTransactionSending,\n input: TItem,\n): SelfPlanAndSendFunctions & TItem {\n if (isPromiseLike(input)) {\n const newInput = input as SelfPlanAndSendFunctions & TItem;\n newInput.planTransaction = async config => await client.planTransaction(await input, config);\n newInput.planTransactions = async config => await client.planTransactions(await input, config);\n newInput.sendTransaction = async config => await client.sendTransaction(await input, config);\n newInput.sendTransactions = async config => await client.sendTransactions(await input, config);\n return newInput;\n }\n\n return Object.freeze(<SelfPlanAndSendFunctions & (Instruction | InstructionPlan)>{\n ...input,\n planTransaction: config => client.planTransaction(input, config),\n planTransactions: config => client.planTransactions(input, config),\n sendTransaction: config => client.sendTransaction(input, config),\n sendTransactions: config => client.sendTransactions(input, config),\n }) as unknown as SelfPlanAndSendFunctions & TItem;\n}\n\nfunction isPromiseLike(\n item: Instruction | InstructionPlan | PromiseLike<Instruction> | PromiseLike<InstructionPlan>,\n): item is PromiseLike<Instruction> | PromiseLike<InstructionPlan> {\n return (\n !!item &&\n (typeof item === 'object' || typeof item === 'function') &&\n typeof (item as PromiseLike<unknown>).then === 'function'\n );\n}\n"]}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * This package contains types and utilities for building Solana program clients.
3
+ * This is mainly used by the JavaScript Codama renderer to generate
4
+ * Kit-compatible program clients.
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+ export * from './instruction-input-resolution';
9
+ export * from './instructions';
10
+ export * from './self-fetch-functions';
11
+ export * from './self-plan-and-send-functions';
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gBAAgB,CAAC;AAC/B,cAAc,wBAAwB,CAAC;AACvC,cAAc,gCAAgC,CAAC"}
@@ -0,0 +1,133 @@
1
+ import { type Address, type ProgramDerivedAddress } from '@solana/addresses';
2
+ import { type AccountMeta } from '@solana/instructions';
3
+ import { type AccountSignerMeta, type TransactionSigner } from '@solana/signers';
4
+ /**
5
+ * Ensures a resolved instruction input is not null or undefined.
6
+ *
7
+ * This function is used during instruction resolution to validate that
8
+ * required inputs have been properly resolved to a non-null value.
9
+ *
10
+ * @typeParam T - The expected type of the resolved input value.
11
+ *
12
+ * @param inputName - The name of the instruction input, used in error messages.
13
+ * @param value - The resolved value to validate.
14
+ * @returns The validated non-null value.
15
+ *
16
+ * @throws Throws a {@link SolanaError} if the value is null or undefined.
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * const resolvedAuthority = getNonNullResolvedInstructionInput(
21
+ * 'authority',
22
+ * maybeAuthority
23
+ * );
24
+ * // resolvedAuthority is guaranteed to be non-null here.
25
+ * ```
26
+ */
27
+ export declare function getNonNullResolvedInstructionInput<T>(inputName: string, value: T | null | undefined): T;
28
+ /**
29
+ * Extracts the address from a resolved instruction account.
30
+ *
31
+ * A resolved instruction account can be an {@link Address}, a {@link ProgramDerivedAddress},
32
+ * or a {@link TransactionSigner}. This function extracts the underlying address from
33
+ * any of these types.
34
+ *
35
+ * @typeParam T - The address type, defaults to `string`.
36
+ *
37
+ * @param inputName - The name of the instruction input, used in error messages.
38
+ * @param value - The resolved account value to extract the address from.
39
+ * @returns The extracted address.
40
+ *
41
+ * @throws Throws a {@link SolanaError} if the value is null or undefined.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * const address = getAddressFromResolvedInstructionAccount('mint', resolvedMint);
46
+ * ```
47
+ */
48
+ export declare function getAddressFromResolvedInstructionAccount<T extends string = string>(inputName: string, value: ResolvedInstructionAccount<T>['value'] | undefined): Address<T>;
49
+ /**
50
+ * Extracts a {@link ProgramDerivedAddress} from a resolved instruction account.
51
+ *
52
+ * This function validates that the resolved account is a PDA and returns it.
53
+ * Use this when you need access to both the address and the bump seed of a PDA.
54
+ *
55
+ * @typeParam T - The address type, defaults to `string`.
56
+ *
57
+ * @param inputName - The name of the instruction input, used in error messages.
58
+ * @param value - The resolved account value expected to be a PDA.
59
+ * @returns The program-derived address.
60
+ *
61
+ * @throws Throws a {@link SolanaError} if the value is not a {@link ProgramDerivedAddress}.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * const pda = getResolvedInstructionAccountAsProgramDerivedAddress('metadata', resolvedMetadata);
66
+ * const [address, bump] = pda;
67
+ * ```
68
+ */
69
+ export declare function getResolvedInstructionAccountAsProgramDerivedAddress<T extends string = string>(inputName: string, value: ResolvedInstructionAccount<T>['value'] | undefined): ProgramDerivedAddress<T>;
70
+ /**
71
+ * Extracts a {@link TransactionSigner} from a resolved instruction account.
72
+ *
73
+ * This function validates that the resolved account is a transaction signer and returns it.
74
+ * Use this when you need the resolved account to be a signer.
75
+ *
76
+ * @typeParam T - The address type, defaults to `string`.
77
+ *
78
+ * @param inputName - The name of the instruction input, used in error messages.
79
+ * @param value - The resolved account value expected to be a signer.
80
+ * @returns The transaction signer.
81
+ *
82
+ * @throws Throws a {@link SolanaError} if the value is not a {@link TransactionSigner}.
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * const signer = getResolvedInstructionAccountAsTransactionSigner('authority', resolvedAuthority);
87
+ * ```
88
+ */
89
+ export declare function getResolvedInstructionAccountAsTransactionSigner<T extends string = string>(inputName: string, value: ResolvedInstructionAccount<T>['value'] | undefined): TransactionSigner<T>;
90
+ /**
91
+ * Represents a resolved account input for an instruction.
92
+ *
93
+ * During instruction building, account inputs are resolved to this type which
94
+ * captures both the account value and whether it should be marked as writable.
95
+ * The value can be an {@link Address}, a {@link ProgramDerivedAddress}, a
96
+ * {@link TransactionSigner}, or `null` for optional accounts.
97
+ *
98
+ * @typeParam TAddress - The address type, defaults to `string`.
99
+ * @typeParam TValue - The type of the resolved value.
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * const mintAccount: ResolvedInstructionAccount = {
104
+ * value: mintAddress,
105
+ * isWritable: true,
106
+ * };
107
+ * ```
108
+ */
109
+ export type ResolvedInstructionAccount<TAddress extends string = string, TValue extends Address<TAddress> | ProgramDerivedAddress<TAddress> | TransactionSigner<TAddress> | null = Address<TAddress> | ProgramDerivedAddress<TAddress> | TransactionSigner<TAddress> | null> = {
110
+ isWritable: boolean;
111
+ value: TValue;
112
+ };
113
+ /**
114
+ * Creates a factory function that converts resolved instruction accounts to account metas.
115
+ *
116
+ * The factory handles the conversion of {@link ResolvedInstructionAccount} objects into
117
+ * {@link AccountMeta} or {@link AccountSignerMeta} objects suitable for building instructions.
118
+ * It also determines how to handle optional accounts based on the provided strategy.
119
+ *
120
+ * @param programAddress - The program address, used when optional accounts use the `programId` strategy.
121
+ * @param optionalAccountStrategy - How to handle null account values:
122
+ * - `'omitted'`: Optional accounts are excluded from the instruction entirely.
123
+ * - `'programId'`: Optional accounts are replaced with the program address as a read-only account.
124
+ * @returns A factory function that converts a resolved account to an account meta.
125
+ *
126
+ * @example
127
+ * ```ts
128
+ * const toAccountMeta = getAccountMetaFactory(programAddress, 'programId');
129
+ * const mintMeta = toAccountMeta('mint', resolvedMint);
130
+ * ```
131
+ */
132
+ export declare function getAccountMetaFactory(programAddress: Address, optionalAccountStrategy: 'omitted' | 'programId'): (inputName: string, account: ResolvedInstructionAccount) => AccountMeta | AccountSignerMeta | undefined;
133
+ //# sourceMappingURL=instruction-input-resolution.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instruction-input-resolution.d.ts","sourceRoot":"","sources":["../../src/instruction-input-resolution.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAA2B,KAA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,CAAC,GAAG,IAAI,GACjG,OAAO,CAAC,QAAQ,CAAC,GACjB,qBAAqB,CAAC,QAAQ,CAAC,GAC/B,iBAAiB,CAAC,QAAQ,CAAC,GAC3B,IAAI,IACV;IACA,UAAU,EAAE,OAAO,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,qBAAqB,CAAC,cAAc,EAAE,OAAO,EAAE,uBAAuB,EAAE,SAAS,GAAG,WAAW,IACnG,WAAW,MAAM,EAAE,SAAS,0BAA0B,KAAG,WAAW,GAAG,iBAAiB,GAAG,SAAS,CAc/G"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * An instruction that tracks how many bytes it adds or removes from on-chain accounts.
3
+ *
4
+ * The `byteDelta` indicates the net change in account storage size. A positive value
5
+ * means bytes are being allocated, while a negative value means bytes are being freed.
6
+ * This is useful for calculating how much balance a storage payer must have for a
7
+ * transaction to succeed.
8
+ */
9
+ export type InstructionWithByteDelta = {
10
+ byteDelta: number;
11
+ };
12
+ //# sourceMappingURL=instructions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instructions.d.ts","sourceRoot":"","sources":["../../src/instructions.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACnC,SAAS,EAAE,MAAM,CAAC;CACrB,CAAC"}
@@ -0,0 +1,109 @@
1
+ import { type Account, type FetchAccountConfig, type FetchAccountsConfig, type MaybeAccount } from '@solana/accounts';
2
+ import type { Address } from '@solana/addresses';
3
+ import type { Codec } from '@solana/codecs-core';
4
+ import type { ClientWithRpc } from '@solana/plugin-interfaces';
5
+ import type { GetAccountInfoApi, GetMultipleAccountsApi } from '@solana/rpc-api';
6
+ type AnyObjectCodec = Codec<any, object>;
7
+ type InferTFrom<T> = T extends Codec<infer TFrom, any> ? TFrom : never;
8
+ type InferTTo<T> = T extends Codec<any, infer TTo> ? TTo : never;
9
+ /**
10
+ * Methods that allow a codec to fetch and decode accounts directly.
11
+ *
12
+ * These methods are added to codec objects via {@link addSelfFetchFunctions},
13
+ * enabling a fluent API where you can call `.fetch()` directly on a codec
14
+ * to retrieve and decode accounts in one step.
15
+ *
16
+ * @typeParam TFrom - The type that the codec encodes from.
17
+ * @typeParam TTo - The type that the codec decodes to.
18
+ *
19
+ * @example
20
+ * Fetching a single account and asserting it exists.
21
+ * ```ts
22
+ * const account = await myAccountCodec.fetch(address);
23
+ * // account.data is of type TTo.
24
+ * ```
25
+ *
26
+ * @example
27
+ * Fetching a single account that may not exist.
28
+ * ```ts
29
+ * const maybeAccount = await myAccountCodec.fetchMaybe(address);
30
+ * if (maybeAccount.exists) {
31
+ * // maybeAccount.data is of type TTo.
32
+ * }
33
+ * ```
34
+ *
35
+ * @example
36
+ * Fetching multiple accounts at once.
37
+ * ```ts
38
+ * const accounts = await myAccountCodec.fetchAll([addressA, addressB]);
39
+ * // All accounts exist.
40
+ * ```
41
+ *
42
+ * @see {@link addSelfFetchFunctions}
43
+ */
44
+ export type SelfFetchFunctions<TFrom extends object, TTo extends TFrom> = {
45
+ /** Fetches and decodes a single account, throwing if it does not exist. */
46
+ readonly fetch: <TAddress extends string>(address: Address<TAddress>, config?: FetchAccountConfig) => Promise<Account<TTo, TAddress>>;
47
+ /** Fetches and decodes multiple accounts, throwing if any do not exist. */
48
+ readonly fetchAll: (addresses: Address[], config?: FetchAccountsConfig) => Promise<Account<TTo>[]>;
49
+ /** Fetches and decodes multiple accounts, returning {@link MaybeAccount} for each. */
50
+ readonly fetchAllMaybe: (addresses: Address[], config?: FetchAccountsConfig) => Promise<MaybeAccount<TTo>[]>;
51
+ /** Fetches and decodes a single account, returning a {@link MaybeAccount}. */
52
+ readonly fetchMaybe: <TAddress extends string>(address: Address<TAddress>, config?: FetchAccountConfig) => Promise<MaybeAccount<TTo, TAddress>>;
53
+ };
54
+ /**
55
+ * Adds self-fetching methods to a codec for retrieving and decoding accounts.
56
+ *
57
+ * This function augments the provided codec with methods that allow it to fetch
58
+ * accounts from the network and decode them in one step. It enables a fluent API
59
+ * where you can call methods like `.fetch()` directly on the codec.
60
+ *
61
+ * @typeParam TFrom - The type that the codec encodes from.
62
+ * @typeParam TTo - The type that the codec decodes to.
63
+ * @typeParam TCodec - The codec type being augmented.
64
+ *
65
+ * @param client - A client that provides RPC access for fetching accounts.
66
+ * @param codec - The codec to augment with self-fetch methods.
67
+ * @returns The codec augmented with {@link SelfFetchFunctions} methods.
68
+ *
69
+ * @example
70
+ * Adding self-fetch functions to an account codec.
71
+ * ```ts
72
+ * import { addSelfFetchFunctions } from '@solana/program-client-core';
73
+ *
74
+ * const myAccountCodec = addSelfFetchFunctions(client, getMyAccountCodec());
75
+ *
76
+ * // Fetch and decode an account in one step.
77
+ * const account = await myAccountCodec.fetch(accountAddress);
78
+ * ```
79
+ *
80
+ * @example
81
+ * Handling accounts that may not exist.
82
+ * ```ts
83
+ * const myAccountCodec = addSelfFetchFunctions(client, getMyAccountCodec());
84
+ *
85
+ * const maybeAccount = await myAccountCodec.fetchMaybe(accountAddress);
86
+ * if (maybeAccount.exists) {
87
+ * console.log('Account data:', maybeAccount.data);
88
+ * } else {
89
+ * console.log(`Account ${maybeAccount.address} does not exist`);
90
+ * }
91
+ * ```
92
+ *
93
+ * @example
94
+ * Fetching multiple accounts at once.
95
+ * ```ts
96
+ * const myAccountCodec = addSelfFetchFunctions(client, getMyAccountCodec());
97
+ *
98
+ * // Throws if any account does not exist.
99
+ * const accounts = await myAccountCodec.fetchAll([addressA, addressB, addressC]);
100
+ *
101
+ * // Returns MaybeAccount for each, allowing some to not exist.
102
+ * const maybeAccounts = await myAccountCodec.fetchAllMaybe([addressA, addressB]);
103
+ * ```
104
+ *
105
+ * @see {@link SelfFetchFunctions}
106
+ */
107
+ export declare function addSelfFetchFunctions<TCodec extends AnyObjectCodec>(client: ClientWithRpc<GetAccountInfoApi & GetMultipleAccountsApi>, codec: TCodec): SelfFetchFunctions<InferTFrom<TCodec>, InferTTo<TCodec>> & TCodec;
108
+ export {};
109
+ //# sourceMappingURL=self-fetch-functions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"self-fetch-functions.d.ts","sourceRoot":"","sources":["../../src/self-fetch-functions.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,KAAK,OAAO,EAIZ,KAAK,kBAAkB,EACvB,KAAK,mBAAmB,EAGxB,KAAK,YAAY,EACpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAGjF,KAAK,cAAc,GAAG,KAAK,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;AAEzC,KAAK,UAAU,CAAC,CAAC,IAAI,CAAC,SAAS,KAAK,CAAC,MAAM,KAAK,EAAE,GAAG,CAAC,GAAG,KAAK,GAAG,KAAK,CAAC;AAEvE,KAAK,QAAQ,CAAC,CAAC,IAAI,CAAC,SAAS,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,CAAC,GAAG,GAAG,GAAG,KAAK,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,MAAM,kBAAkB,CAAC,KAAK,SAAS,MAAM,EAAE,GAAG,SAAS,KAAK,IAAI;IACtE,2EAA2E;IAC3E,QAAQ,CAAC,KAAK,EAAE,CAAC,QAAQ,SAAS,MAAM,EACpC,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,EAC1B,MAAM,CAAC,EAAE,kBAAkB,KAC1B,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC,CAAC;IACrC,2EAA2E;IAC3E,Q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