@solana/program-client-core 8.3.0 → 8.3.1-canary-20260918111252
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.
- package/README.md +190 -1
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.native.mjs.map +1 -1
- package/dist/index.node.cjs.map +1 -1
- package/dist/index.node.mjs.map +1 -1
- package/dist/types/instruction-input-resolution.d.ts +35 -27
- package/dist/types/instruction-input-resolution.d.ts.map +1 -1
- package/package.json +10 -10
- package/src/instruction-input-resolution.ts +35 -27
package/README.md
CHANGED
|
@@ -11,4 +11,193 @@
|
|
|
11
11
|
|
|
12
12
|
# @solana/program-client-core
|
|
13
13
|
|
|
14
|
-
This package contains types and utilities for building Solana program clients.
|
|
14
|
+
This package contains types and utilities for building Solana program clients. It is mainly used by the [JavaScript Codama renderer](https://github.com/codama-idl/renderers-js) to generate Kit-compatible program clients. It can be used standalone, but it is also exported as part of Kit [`@solana/kit`](https://github.com/anza-xyz/kit/tree/main/packages/kit) under the `@solana/kit/program-client-core` subpath.
|
|
15
|
+
|
|
16
|
+
You will rarely need to import from this package yourself. However, it defines what the instruction builders of generated program clients accept as inputs and how these inputs are resolved into account metas, which is useful to know when using such clients.
|
|
17
|
+
|
|
18
|
+
## Instruction inputs
|
|
19
|
+
|
|
20
|
+
The instruction builders of generated program clients accept an input object containing the accounts and arguments of the instruction. The types below describe what may be provided for each account, depending on whether the program's IDL declares it as a signer.
|
|
21
|
+
|
|
22
|
+
### `InstructionAccountInput`
|
|
23
|
+
|
|
24
|
+
This type represents the accepted inputs for a non-signer instruction account. Namely, one of the following:
|
|
25
|
+
|
|
26
|
+
- An `Address` — the most common case.
|
|
27
|
+
- Any object exposing an `address` property — see `HasAddress` in `@solana/addresses`. This lets third-party wrappers around addresses be passed directly, as long as they expose a Kit `Address` under an `address` property. Note that `TransactionSigner` objects satisfy this shape too, in which case they merely act as address carriers for non-signer accounts.
|
|
28
|
+
- A `ProgramDerivedAddress` — i.e. an `[address, bump]` tuple.
|
|
29
|
+
- An `AccountNonSignerMeta` from `@solana/instructions` — i.e. `{ address, role }` — to explicitly override the role derived from the program's IDL, e.g. to mark an account as writable when the IDL declares it as readonly.
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { AccountRole } from '@solana/instructions';
|
|
33
|
+
|
|
34
|
+
// Given a generated instruction builder whose `mint` account is a non-signer account.
|
|
35
|
+
getMintToInstruction({ mint: mintAddress /* ... */ }); // An address.
|
|
36
|
+
getMintToInstruction({ mint: addressWrapper /* ... */ }); // A third-party wrapper exposing an `address` property.
|
|
37
|
+
getMintToInstruction({ mint: mintPda /* ... */ }); // A program derived address.
|
|
38
|
+
getMintToInstruction({ mint: { address: mintAddress, role: AccountRole.READONLY } /* ... */ }); // A role override.
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### `InstructionSignerInput`
|
|
42
|
+
|
|
43
|
+
This type represents the accepted inputs for a signer instruction account. Namely, one of the following:
|
|
44
|
+
|
|
45
|
+
- A `TransactionSigner` — the most common case.
|
|
46
|
+
- An `AccountSignerMeta` from `@solana/signers` — i.e. `{ address, role, signer }` — to explicitly override the role derived from the program's IDL, e.g. to mark a signer account as writable when the IDL declares it as readonly.
|
|
47
|
+
|
|
48
|
+
If the account's signature is provided by other means — e.g. when the transaction is signed by a multisig or by a wallet that adds the signature later — use `createNoopSigner()` from `@solana/signers` to satisfy the requirement.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import { createNoopSigner } from '@solana/signers';
|
|
52
|
+
|
|
53
|
+
// Given a generated instruction builder whose `authority` account is a signer account.
|
|
54
|
+
getMintToInstruction({ authority: authoritySigner /* ... */ }); // A transaction signer.
|
|
55
|
+
getMintToInstruction({ authority: createNoopSigner(authorityAddress) /* ... */ }); // A signature provided by other means.
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Accounts that may or may not be signers — e.g. an authority that can either be a signer or a multisig account — accept both `InstructionAccountInput` and `InstructionSignerInput`. Providing a `TransactionSigner` for such an account marks it as a signer.
|
|
59
|
+
|
|
60
|
+
### Resolution rules
|
|
61
|
+
|
|
62
|
+
The account metas of the resulting instruction are determined from the provided inputs as follows, in order of precedence:
|
|
63
|
+
|
|
64
|
+
| Provided input | Resulting account meta |
|
|
65
|
+
| ------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
|
|
66
|
+
| An account meta — i.e. carrying an explicit `role` | The provided role is used as-is, regardless of the flags declared by the IDL. Any attached `signer` is preserved. |
|
|
67
|
+
| A `TransactionSigner`, for an account the IDL declares as a signer or as either | The IDL's writable flag is upgraded to the corresponding signer role and the signer is attached to the account meta. |
|
|
68
|
+
| A `TransactionSigner`, for an account the IDL declares as a non-signer | The signer merely carries its address. The IDL's writable flag decides between the readonly and writable roles. |
|
|
69
|
+
| Any other address-carrying value | The IDL's writable flag decides between the readonly and writable roles. |
|
|
70
|
+
|
|
71
|
+
These rules are reflected in the types of the instructions returned by generated builders: for instance, providing `{ address, role: AccountRole.READONLY }` for an account results in a `ReadonlyAccount` meta, whilst providing a `TransactionSigner` for an account that may or may not be a signer results in a signer meta.
|
|
72
|
+
|
|
73
|
+
A couple of failure cases are worth knowing about:
|
|
74
|
+
|
|
75
|
+
- Providing a value that cannot sign — e.g. a plain `Address` — for an account the IDL declares as a signer throws a `SolanaError` with code `SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_SIGNER`. Use `createNoopSigner()` as described above if the signature is provided by other means.
|
|
76
|
+
- Some instructions have an argument that defaults to the bump seed of one of their accounts — e.g. a `bump` argument defaulting to the bump seed of a `metadata` PDA. Since a plain address does not carry a bump seed, providing anything other than a `ProgramDerivedAddress` for such an account throws a `SolanaError` with code `SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE`, unless the dependent argument is provided explicitly.
|
|
77
|
+
|
|
78
|
+
## Types
|
|
79
|
+
|
|
80
|
+
### `ResolvedInstructionAccount`
|
|
81
|
+
|
|
82
|
+
This type represents an account input during instruction building, capturing the provided value alongside the signer and writable flags declared by the program's IDL. The value can be any `InstructionAccountInput`, any `InstructionSignerInput`, or `null` for optional accounts that were not provided.
|
|
83
|
+
|
|
84
|
+
The optional `isSigner` flag describes whether the IDL requires the account to sign the transaction, with `'either'` meaning that the account may or may not be a signer. Omitting the flag is equivalent to setting it to `'either'`.
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const resolvedMint: ResolvedInstructionAccount = { value: mintAddress, isSigner: false, isWritable: true };
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### `ResolvedInstructionAccountMeta`
|
|
91
|
+
|
|
92
|
+
This type helper computes the account meta type produced by an instruction account based on the input provided for it, mirroring the [resolution rules](#resolution-rules) applied at runtime by `getAccountMetaFactory()`. Generated instruction builders use it to accurately type the accounts of the instructions they return.
|
|
93
|
+
|
|
94
|
+
It takes the type of the provided input, the address type parameter of the account and, optionally, the meta type to produce when a `TransactionSigner` is provided. The latter defaults to the address type parameter, so leaving it out — as generated builders do for non-signer accounts — makes signers act as plain address carriers. When the input carries no static information — e.g. when the builder's type parameters fall back to their constraints — it deterministically resolves to the address type parameter, which generated instruction types map to the account meta declared by the program's IDL.
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
// Given a mint account that is not a signer.
|
|
98
|
+
type MintMeta<TInput extends InstructionAccountInput> = ResolvedInstructionAccountMeta<
|
|
99
|
+
TInput,
|
|
100
|
+
InstructionAccountInputAddress<TInput>
|
|
101
|
+
>;
|
|
102
|
+
|
|
103
|
+
// Given an authority account that may or may not be a signer.
|
|
104
|
+
type AuthorityMeta<TInput extends InstructionAccountInput | InstructionSignerInput> = ResolvedInstructionAccountMeta<
|
|
105
|
+
TInput,
|
|
106
|
+
InstructionAccountInputAddress<TInput>,
|
|
107
|
+
ReadonlySignerAccount<InstructionAccountInputAddress<TInput>> &
|
|
108
|
+
AccountSignerMeta<InstructionAccountInputAddress<TInput>>
|
|
109
|
+
>;
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### `InstructionAccountInputAddress`
|
|
113
|
+
|
|
114
|
+
This type helper extracts the address type parameter from an instruction account input — e.g. `'1234'` from an `Address<'1234'>`, a `TransactionSigner<'1234'>`, a `ProgramDerivedAddress<'1234'>` or an account meta of that address. Inputs carrying no address brand resolve to `string`. Generated instruction builders use it to recover the address type parameter of an account from the input provided for it.
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
type A = InstructionAccountInputAddress<Address<'1234'>>; // '1234'
|
|
118
|
+
type B = InstructionAccountInputAddress<TransactionSigner<'1234'>>; // '1234'
|
|
119
|
+
type C = InstructionAccountInputAddress<Address>; // string
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### `InstructionWithByteDelta`
|
|
123
|
+
|
|
124
|
+
This type represents an instruction that tracks how many bytes it adds to or removes from on-chain accounts via a `byteDelta` property. A positive value means bytes are being allocated, whilst a negative value means bytes are being freed. This is useful for calculating how much balance a storage payer must have for a transaction to succeed.
|
|
125
|
+
|
|
126
|
+
### `SelfFetchFunctions`
|
|
127
|
+
|
|
128
|
+
This type describes the `fetch`, `fetchMaybe`, `fetchAll` and `fetchAllMaybe` methods added to account codecs by `addSelfFetchFunctions()`, allowing accounts to be fetched and decoded in one step.
|
|
129
|
+
|
|
130
|
+
### `SelfPlanAndSendFunctions`
|
|
131
|
+
|
|
132
|
+
This type describes the `planTransaction`, `planTransactions`, `sendTransaction` and `sendTransactions` methods added to instructions and instruction plans by `addSelfPlanAndSendFunctions()`, allowing them to be planned and sent directly.
|
|
133
|
+
|
|
134
|
+
## Functions
|
|
135
|
+
|
|
136
|
+
### `getAccountMetaFactory()`
|
|
137
|
+
|
|
138
|
+
This function creates a helper that converts `ResolvedInstructionAccount` objects into `AccountMeta` or `AccountSignerMeta` objects following the [resolution rules](#resolution-rules) above. It takes the program address and the strategy to apply to optional accounts that were not provided: `'programId'` replaces them with the program address as a readonly account, whilst `'omitted'` excludes them from the instruction entirely — in which case the helper returns `undefined` for them.
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
const getAccountMeta = getAccountMetaFactory(programAddress, 'programId');
|
|
142
|
+
const mintMeta = getAccountMeta('mint', { value: mintAddress, isSigner: false, isWritable: true });
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### `getNonNullResolvedInstructionInput()`
|
|
146
|
+
|
|
147
|
+
This function ensures a resolved instruction input is neither `null` nor `undefined` and returns it. It throws a `SolanaError` with code `SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_NON_NULL` otherwise, using the provided input name in the error message.
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
const authority = getNonNullResolvedInstructionInput('authority', maybeAuthority);
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### `getAddressFromResolvedInstructionAccount()`
|
|
154
|
+
|
|
155
|
+
This function extracts the address from the value of a resolved instruction account, whether it is an `Address`, a `ProgramDerivedAddress` or any object exposing an `address` property — such as a `TransactionSigner`, an account meta or a third-party address wrapper. It throws a `SolanaError` with code `SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_NON_NULL` if the value is `null` or `undefined`.
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
const mintAddress = getAddressFromResolvedInstructionAccount('mint', resolvedMint.value);
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### `getResolvedInstructionAccountAsProgramDerivedAddress()`
|
|
162
|
+
|
|
163
|
+
This function ensures the value of a resolved instruction account is a `ProgramDerivedAddress` and returns it. Generated instruction builders use it when another input defaults to the bump seed of the account. It throws a `SolanaError` with code `SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE` otherwise.
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
const [metadataAddress, metadataBump] = getResolvedInstructionAccountAsProgramDerivedAddress(
|
|
167
|
+
'metadata',
|
|
168
|
+
resolvedMetadata.value,
|
|
169
|
+
);
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### `getResolvedInstructionAccountAsTransactionSigner()`
|
|
173
|
+
|
|
174
|
+
This function ensures the value of a resolved instruction account is a `TransactionSigner` — or an `AccountSignerMeta` carrying one — and returns the signer. It throws a `SolanaError` with code `SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE` otherwise.
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
const authoritySigner = getResolvedInstructionAccountAsTransactionSigner('authority', resolvedAuthority.value);
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### `addSelfFetchFunctions()`
|
|
181
|
+
|
|
182
|
+
This function augments an account codec with the `SelfFetchFunctions` methods, using the provided client's RPC to fetch accounts. Generated program clients use it to expose account codecs that can fetch and decode accounts in one step.
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
const mintCodec = addSelfFetchFunctions(client, getMintCodec());
|
|
186
|
+
|
|
187
|
+
const mint = await mintCodec.fetch(mintAddress); // Throws if the account does not exist.
|
|
188
|
+
const maybeMint = await mintCodec.fetchMaybe(mintAddress); // Returns a `MaybeAccount` instead.
|
|
189
|
+
const mints = await mintCodec.fetchAll([mintAddressA, mintAddressB]);
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### `addSelfPlanAndSendFunctions()`
|
|
193
|
+
|
|
194
|
+
This function augments an instruction, an instruction plan or a promise resolving to either with the `SelfPlanAndSendFunctions` methods, using the provided client's transaction planning and sending capabilities. Generated program clients use it to expose instruction builders whose result can be sent directly.
|
|
195
|
+
|
|
196
|
+
```ts
|
|
197
|
+
const instruction = addSelfPlanAndSendFunctions(
|
|
198
|
+
client,
|
|
199
|
+
getTransferInstruction({ source, destination, authority, amount }),
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
await instruction.sendTransaction();
|
|
203
|
+
```
|
|
@@ -1 +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","SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_SIGNER","upgradeRoleToSigner","isTransactionSigner","fetchEncodedAccount","decodeAccount","fetchEncodedAccounts","assertAccountExists","assertAccountsExist"],"mappings":";;;;;;;;;AAuEO,SAAS,kCAAA,CAAsC,WAAmB,KAAA,EAAgC;AACrG,EAAA,IAAI,KAAA,KAAU,IAAA,IAAQ,KAAA,KAAU,MAAA,EAAW;AACvC,IAAA,MAAM,IAAIA,mBAAYC,iFAAA,EAA4E;AAAA,MAC9F;AAAA,KACH,CAAA;AAAA,EACL;AACA,EAAA,OAAO,KAAA;AACX;AAuBO,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,CAACC,iCAAA,CAAwB,KAAK,CAAA,EAAG;AACjC,IAAA,MAAM,IAAIF,mBAAYG,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AAM,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.browser.cjs","sourcesContent":["import { type Address, type HasAddress, isProgramDerivedAddress, type ProgramDerivedAddress } from '@solana/addresses';\nimport {\n SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_NON_NULL,\n SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_SIGNER,\n SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE,\n SolanaError,\n} from '@solana/errors';\nimport { type AccountMeta, type AccountNonSignerMeta, AccountRole, upgradeRoleToSigner } from '@solana/instructions';\nimport { type AccountSignerMeta, isTransactionSigner, type TransactionSigner } from '@solana/signers';\n\n/**\n * Represents the accepted input values for a non-signer instruction account.\n *\n * Namely, one of the following:\n * - An {@link Address} — the most common case.\n * - Any object exposing an `address` property (see {@link HasAddress}) — e.g. a framework's\n * address wrapper class. Note that {@link TransactionSigner | TransactionSigners} satisfy this\n * shape too, in which case they act as plain address carriers for non-signer accounts.\n * - A {@link ProgramDerivedAddress} — i.e. an `[address, bump]` tuple.\n * - An {@link AccountNonSignerMeta} — i.e. `{ address, role }` — to explicitly override the\n * role derived from the program's IDL, e.g. to mark an account as writable or readonly.\n *\n * @typeParam TAddress - Supply a string literal to define an account having a particular address.\n *\n * @see {@link InstructionSignerInput}\n */\nexport type InstructionAccountInput<TAddress extends string = string> =\n | AccountNonSignerMeta<TAddress>\n | Address<TAddress>\n | HasAddress<TAddress>\n | ProgramDerivedAddress<TAddress>;\n\n/**\n * Represents the accepted input values for a signer instruction account.\n *\n * Namely, one of the following:\n * - A {@link TransactionSigner} — the most common case.\n * - An {@link AccountSignerMeta} — i.e. `{ address, role, signer }` — to explicitly override the\n * role derived from the program's IDL, e.g. to mark a signer account as writable or readonly.\n *\n * @typeParam TAddress - Supply a string literal to define an account having a particular address.\n *\n * @see {@link InstructionAccountInput}\n */\nexport type InstructionSignerInput<TAddress extends string = string> =\n | AccountSignerMeta<TAddress>\n | TransactionSigner<TAddress>;\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 any object exposing an `address` property — such as a {@link TransactionSigner}, an\n * account meta, or a framework's address wrapper class (see {@link HasAddress}). This\n * function extracts the underlying address from 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 — or an\n * {@link AccountSignerMeta} carrying one — and returns the signer.\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 const signer = value && hasExplicitRole(value) && 'signer' in value ? value.signer : value;\n if (!isResolvedInstructionAccountSigner<T>(signer)) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE, {\n expectedType: 'TransactionSigner',\n inputName,\n });\n }\n return signer;\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 captures\n * the account value alongside the signer and writable flags declared by the program's IDL.\n * The value can be any {@link InstructionAccountInput}, any {@link InstructionSignerInput},\n * or `null` for optional accounts.\n *\n * The optional `isSigner` flag describes whether the IDL requires the account to sign the\n * transaction — with `'either'` meaning the account may or may not be a signer, in which\n * case providing a {@link TransactionSigner} value is what marks it as one. Omitting the\n * flag — e.g. in program clients generated before its introduction — is equivalent to\n * setting it to `'either'`.\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 * isSigner: false,\n * isWritable: true,\n * };\n * ```\n */\nexport type ResolvedInstructionAccount<\n TAddress extends string = string,\n TValue extends InstructionAccountInput<TAddress> | InstructionSignerInput<TAddress> | null =\n | InstructionAccountInput<TAddress>\n | InstructionSignerInput<TAddress>\n | null,\n> = {\n isSigner?: boolean | 'either';\n isWritable: boolean;\n value: TValue;\n};\n\n/**\n * Extracts the address type parameter from an instruction account input.\n *\n * Given any {@link InstructionAccountInput} or {@link InstructionSignerInput} — e.g. an\n * {@link Address}, an address-bearing object (see {@link HasAddress}), a\n * {@link ProgramDerivedAddress} or an account meta — this type helper resolves to the\n * branded address string it carries. This allows generated program clients to recover the\n * address type parameter of an account from the caller's input type alone — e.g. via\n * `InstructionAccountInputAddress<TInput['authority']>` — instead of declaring a dedicated\n * address type parameter on the instruction builder.\n *\n * When given a union of inputs, the helper distributes over it, so a union whose members\n * all share the same address brand resolves to that brand. Inputs carrying no brand\n * resolve to `string`.\n *\n * @typeParam TInput - The type of the input provided by the caller for this account.\n *\n * @example\n * ```ts\n * type A = InstructionAccountInputAddress<Address<'1234'>>; // '1234'\n * type B = InstructionAccountInputAddress<TransactionSigner<'1234'>>; // '1234'\n * type C = InstructionAccountInputAddress<ProgramDerivedAddress<'1234'>>; // '1234'\n * type D = InstructionAccountInputAddress<Address>; // string\n * ```\n *\n * @see {@link ResolvedInstructionAccountMeta}\n */\nexport type InstructionAccountInputAddress<TInput> =\n TInput extends HasAddress<infer TAddress>\n ? TAddress\n : TInput extends ProgramDerivedAddress<infer TAddress>\n ? TAddress\n : TInput extends Address<infer TAddress>\n ? TAddress\n : string;\n\n/**\n * Computes the account meta type produced by an instruction account, based on the input\n * provided by the caller.\n *\n * This type helper mirrors the runtime logic of {@link getAccountMetaFactory} so that\n * generated program clients can accurately type the accounts of the instructions they\n * return. Namely:\n * - When the input carries an explicit `role` — i.e. it is an {@link AccountNonSignerMeta} or an\n * {@link AccountSignerMeta} — the meta type preserves the input's role type: an inline\n * `role: AccountRole.READONLY` override resolves to `ReadonlyAccount`, while a role only\n * known at runtime widens to {@link AccountMeta}. If the input also carries a `signer`,\n * {@link AccountSignerMeta} is used so the attached signer is reflected in the type.\n * - When the input is a {@link TransactionSigner}, the meta type is `TSignerMeta` — e.g.\n * `ReadonlySignerAccount<TAddress> & AccountSignerMeta<TAddress>` for accounts the IDL\n * declares as signers. For non-signer accounts, `TSignerMeta` should be left to its\n * default of `TAddress` so that signers merely act as address carriers.\n * - Otherwise, the helper resolves to `TAddress` — the branded address string that generated\n * instruction types map to the account meta declared by the program's IDL.\n *\n * Note that the checks are wrapped in tuples (`[TInput] extends [...]`) to prevent unions\n * from distributing. If `TInput` is not narrowed to the caller's specific input type — e.g.\n * when a declared input union is provided instead — the helper deterministically falls back\n * to `TAddress`, matching the account meta declared by the program's IDL.\n *\n * @typeParam TInput - The type of the input provided by the caller for this account.\n * @typeParam TAddress - The address type parameter of the account.\n * @typeParam TSignerMeta - The meta type produced when a {@link TransactionSigner} is\n * provided. Defaults to `TAddress`, which treats signers as plain address carriers.\n *\n * @example\n * The instruction builder below captures the caller's input in a single `TInput` type\n * parameter and recovers each account's address type parameter from it using\n * {@link InstructionAccountInputAddress}.\n * ```ts\n * declare function getTransferInstruction<TInput extends TransferInput>(\n * input: TInput,\n * ): TransferInstruction<\n * ResolvedInstructionAccountMeta<\n * TInput['authority'],\n * InstructionAccountInputAddress<TInput['authority']>,\n * ReadonlySignerAccount<InstructionAccountInputAddress<TInput['authority']>> &\n * AccountSignerMeta<InstructionAccountInputAddress<TInput['authority']>>\n * >\n * >;\n * ```\n *\n * Alternatively, instruction builders may keep a dedicated address type parameter per\n * account. In that case, the parameter below must intersect the concrete input type with\n * the inferred `TInput` type parameter (`TransferInput<TAccountAuthority> & TInput`) —\n * referencing the address type parameters only in `TInput`'s constraint makes their\n * inference fall back to `string`. Defaulting `TInput` to the concrete input type keeps\n * call sites with explicit type arguments working.\n * ```ts\n * declare function getTransferInstruction<\n * TAccountAuthority extends string,\n * TInput extends TransferInput<TAccountAuthority> = TransferInput<TAccountAuthority>,\n * >(\n * input: TransferInput<TAccountAuthority> & TInput,\n * ): TransferInstruction<\n * ResolvedInstructionAccountMeta<\n * TInput['authority'],\n * TAccountAuthority,\n * ReadonlySignerAccount<TAccountAuthority> & AccountSignerMeta<TAccountAuthority>\n * >\n * >;\n * ```\n *\n * @see {@link getAccountMetaFactory}\n * @see {@link InstructionAccountInputAddress}\n */\nexport type ResolvedInstructionAccountMeta<TInput, TAddress extends string, TSignerMeta = TAddress> = [TInput] extends [\n { role: infer TRole extends AccountRole },\n]\n ? ([TInput] extends [{ signer: TransactionSigner<TAddress> }]\n ? AccountSignerMeta<TAddress>\n : AccountMeta<TAddress>) & { readonly role: TRole }\n : [TInput] extends [TransactionSigner<TAddress>]\n ? TSignerMeta\n : TAddress;\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 * The role of the resulting account meta is determined as follows, in order of precedence:\n * 1. If the value carries an explicit `role` — i.e. it is an {@link AccountNonSignerMeta} or\n * an {@link AccountSignerMeta} — that role is used as-is, regardless of the flags declared\n * by the program's IDL.\n * 2. Otherwise, if the value is a {@link TransactionSigner} and the account's `isSigner` flag\n * is not `false`, the IDL's writable flag is upgraded to the corresponding signer role and\n * the signer is attached to the meta. When `isSigner` is `false`, the signer merely acts\n * as an address carrier and no upgrade occurs. Omitting the flag is equivalent to `'either'`.\n * 3. Otherwise, the IDL's writable flag decides between the readonly and writable roles.\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 * @throws Throws a {@link SolanaError} when the account's `isSigner` flag is `true` but the\n * provided value is neither a {@link TransactionSigner} nor carries an explicit `role`. Use\n * `createNoopSigner()` from `@solana/signers` if the account's signature is provided by other means.\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 // Explicit roles always take precedence over the flags declared by the program's IDL.\n if (hasExplicitRole(account.value)) {\n return Object.freeze({\n address: account.value.address,\n role: account.value.role,\n ...('signer' in account.value && account.value.signer ? { signer: account.value.signer } : {}),\n });\n }\n\n // Only mark implicit values as signers when the IDL declares\n // the account as a signer or lets the input decide (`'either'`).\n const idlIsSigner = account.isSigner ?? 'either';\n const isSigner = idlIsSigner !== false && isResolvedInstructionAccountSigner(account.value);\n if (!isSigner && idlIsSigner === true) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_SIGNER, {\n inputName,\n });\n }\n\n const writableRole = account.isWritable ? AccountRole.WRITABLE : AccountRole.READONLY;\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\n/**\n * Checks whether a resolved instruction account value carries an explicit account role,\n * i.e. whether it is an {@link AccountNonSignerMeta} or an {@link AccountSignerMeta}.\n *\n * Since {@link AccountRole} is a numeric enum, requiring `role` to be a number prevents\n * unrelated `role` properties on address-bearing objects from being mistaken for a role\n * override.\n */\nfunction hasExplicitRole(\n value: NonNullable<ResolvedInstructionAccount['value']>,\n): value is AccountNonSignerMeta | AccountSignerMeta {\n return typeof value === 'object' && 'role' in value && typeof value.role === 'number';\n}\n\nfunction isResolvedInstructionAccountSigner<TAddress extends string = string>(\n value: unknown,\n): value is TransactionSigner<TAddress> {\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"]}
|
|
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","SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_SIGNER","upgradeRoleToSigner","isTransactionSigner","fetchEncodedAccount","decodeAccount","fetchEncodedAccounts","assertAccountExists","assertAccountsExist"],"mappings":";;;;;;;;;AAuEO,SAAS,kCAAA,CAAsC,WAAmB,KAAA,EAAgC;AACrG,EAAA,IAAI,KAAA,KAAU,IAAA,IAAQ,KAAA,KAAU,MAAA,EAAW;AACvC,IAAA,MAAM,IAAIA,mBAAYC,iFAAA,EAA4E;AAAA,MAC9F;AAAA,KACH,CAAA;AAAA,EACL;AACA,EAAA,OAAO,KAAA;AACX;AAuBO,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,CAACC,iCAAA,CAAwB,KAAK,CAAA,EAAG;AACjC,IAAA,MAAM,IAAIF,mBAAYG,gFAAA,EAA2E;AAAA,MAC7F,YAAA,EAAc,uBAAA;AAAA,MACd;AAAA,KACH,CAAA;AAAA,EACL;AACA,EAAA,OAAO,KAAA;AACX;AAsBO,SAAS,gDAAA,CACZ,WACA,KAAA,EACoB;AACpB,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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.browser.cjs","sourcesContent":["import { type Address, type HasAddress, isProgramDerivedAddress, type ProgramDerivedAddress } from '@solana/addresses';\nimport {\n SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_NON_NULL,\n SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_SIGNER,\n SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE,\n SolanaError,\n} from '@solana/errors';\nimport { type AccountMeta, type AccountNonSignerMeta, AccountRole, upgradeRoleToSigner } from '@solana/instructions';\nimport { type AccountSignerMeta, isTransactionSigner, type TransactionSigner } from '@solana/signers';\n\n/**\n * Represents the accepted input values for a non-signer instruction account.\n *\n * Namely, one of the following:\n * - An {@link Address} — the most common case.\n * - Any object exposing an `address` property (see {@link HasAddress}) — e.g. a framework's\n * address wrapper class. Note that {@link TransactionSigner | TransactionSigners} satisfy this\n * shape too, in which case they act as plain address carriers for non-signer accounts.\n * - A {@link ProgramDerivedAddress} — i.e. an `[address, bump]` tuple.\n * - An {@link AccountNonSignerMeta} — i.e. `{ address, role }` — to explicitly override the\n * role derived from the program's IDL, e.g. to mark an account as writable or readonly.\n *\n * @typeParam TAddress - Supply a string literal to define an account having a particular address.\n *\n * @see {@link InstructionSignerInput}\n */\nexport type InstructionAccountInput<TAddress extends string = string> =\n | AccountNonSignerMeta<TAddress>\n | Address<TAddress>\n | HasAddress<TAddress>\n | ProgramDerivedAddress<TAddress>;\n\n/**\n * Represents the accepted input values for a signer instruction account.\n *\n * Namely, one of the following:\n * - A {@link TransactionSigner} — the most common case.\n * - An {@link AccountSignerMeta} — i.e. `{ address, role, signer }` — to explicitly override the\n * role derived from the program's IDL, e.g. to mark a signer account as writable or readonly.\n *\n * @typeParam TAddress - Supply a string literal to define an account having a particular address.\n *\n * @see {@link InstructionAccountInput}\n */\nexport type InstructionSignerInput<TAddress extends string = string> =\n | AccountSignerMeta<TAddress>\n | TransactionSigner<TAddress>;\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 any object exposing an `address` property — such as a {@link TransactionSigner}, an\n * account meta, or a framework's address wrapper class (see {@link HasAddress}). This\n * function extracts the underlying address from 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.value);\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.value);\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 — or an\n * {@link AccountSignerMeta} carrying one — and returns the signer.\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.value);\n * ```\n */\nexport function getResolvedInstructionAccountAsTransactionSigner<T extends string = string>(\n inputName: string,\n value: ResolvedInstructionAccount<T>['value'] | undefined,\n): TransactionSigner<T> {\n const signer = value && hasExplicitRole(value) && 'signer' in value ? value.signer : value;\n if (!isResolvedInstructionAccountSigner<T>(signer)) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE, {\n expectedType: 'TransactionSigner',\n inputName,\n });\n }\n return signer;\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 captures\n * the account value alongside the signer and writable flags declared by the program's IDL.\n * The value can be any {@link InstructionAccountInput}, any {@link InstructionSignerInput},\n * or `null` for optional accounts.\n *\n * The optional `isSigner` flag describes whether the IDL requires the account to sign the\n * transaction — with `'either'` meaning the account may or may not be a signer, in which\n * case providing a {@link TransactionSigner} value is what marks it as one. Omitting the\n * flag — e.g. in program clients generated before its introduction — is equivalent to\n * setting it to `'either'`.\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 * isSigner: false,\n * isWritable: true,\n * };\n * ```\n */\nexport type ResolvedInstructionAccount<\n TAddress extends string = string,\n TValue extends InstructionAccountInput<TAddress> | InstructionSignerInput<TAddress> | null =\n | InstructionAccountInput<TAddress>\n | InstructionSignerInput<TAddress>\n | null,\n> = {\n isSigner?: boolean | 'either';\n isWritable: boolean;\n value: TValue;\n};\n\n/**\n * Extracts the address type parameter from an instruction account input.\n *\n * Given any {@link InstructionAccountInput} or {@link InstructionSignerInput} — e.g. an\n * {@link Address}, an address-bearing object (see {@link HasAddress}), a\n * {@link ProgramDerivedAddress} or an account meta — this type helper resolves to the\n * branded address string it carries. This allows generated program clients to recover the\n * address type parameter of an account from the caller's input type alone — e.g. via\n * `InstructionAccountInputAddress<TAccountAuthority>` where `TAccountAuthority` captures the\n * input provided for the `authority` account — instead of declaring a dedicated address\n * type parameter on the instruction builder.\n *\n * When given a union of inputs, the helper distributes over it, so a union whose members\n * all share the same address brand resolves to that brand. Inputs carrying no brand\n * resolve to `string`.\n *\n * @typeParam TInput - The type of the input provided by the caller for this account.\n *\n * @example\n * ```ts\n * type A = InstructionAccountInputAddress<Address<'1234'>>; // '1234'\n * type B = InstructionAccountInputAddress<TransactionSigner<'1234'>>; // '1234'\n * type C = InstructionAccountInputAddress<ProgramDerivedAddress<'1234'>>; // '1234'\n * type D = InstructionAccountInputAddress<Address>; // string\n * ```\n *\n * @see {@link ResolvedInstructionAccountMeta}\n */\nexport type InstructionAccountInputAddress<TInput> =\n TInput extends HasAddress<infer TAddress>\n ? TAddress\n : TInput extends ProgramDerivedAddress<infer TAddress>\n ? TAddress\n : TInput extends Address<infer TAddress>\n ? TAddress\n : string;\n\n/**\n * Computes the account meta type produced by an instruction account, based on the input\n * provided by the caller.\n *\n * This type helper mirrors the runtime logic of {@link getAccountMetaFactory} so that\n * generated program clients can accurately type the accounts of the instructions they\n * return. Namely:\n * - When the input carries an explicit `role` — i.e. it is an {@link AccountNonSignerMeta} or an\n * {@link AccountSignerMeta} — the meta type preserves the input's role type: an inline\n * `role: AccountRole.READONLY` override resolves to `ReadonlyAccount`, while a role only\n * known at runtime widens to {@link AccountMeta}. If the input also carries a `signer`,\n * {@link AccountSignerMeta} is used so the attached signer is reflected in the type.\n * - When the input is a {@link TransactionSigner}, the meta type is `TSignerMeta` — e.g.\n * `ReadonlySignerAccount<TAddress> & AccountSignerMeta<TAddress>` for accounts the IDL\n * declares as signers. For non-signer accounts, `TSignerMeta` should be left to its\n * default of `TAddress` so that signers merely act as address carriers.\n * - Otherwise, the helper resolves to `TAddress` — the branded address string that generated\n * instruction types map to the account meta declared by the program's IDL.\n *\n * Note that the checks are wrapped in tuples (`[TInput] extends [...]`) to prevent unions\n * from distributing. If `TInput` is not narrowed to the caller's specific input type — e.g.\n * when a declared input union is provided instead — the helper deterministically falls back\n * to `TAddress`, matching the account meta declared by the program's IDL.\n *\n * @typeParam TInput - The type of the input provided by the caller for this account.\n * @typeParam TAddress - The address type parameter of the account.\n * @typeParam TSignerMeta - The meta type produced when a {@link TransactionSigner} is\n * provided. Defaults to `TAddress`, which treats signers as plain address carriers.\n *\n * @example\n * The instruction builder below — the shape emitted by the Codama JS renderer — declares\n * one type parameter per account holding the input value provided for that account, and\n * recovers the account's address type parameter from it using\n * {@link InstructionAccountInputAddress}. Since its `input` parameter remains a concrete\n * object type once inferred, TypeScript keeps performing excess property checks on it, so\n * a misspelled optional account is a compile error rather than silently falling back to\n * its default value.\n * ```ts\n * type TransferInput<\n * TAccountAuthority extends InstructionAccountInput | InstructionSignerInput =\n * | InstructionAccountInput\n * | InstructionSignerInput,\n * > = { authority: TAccountAuthority; amount: bigint };\n *\n * declare function getTransferInstruction<\n * TAccountAuthority extends InstructionAccountInput | InstructionSignerInput,\n * >(\n * input: TransferInput<TAccountAuthority>,\n * ): TransferInstruction<\n * ResolvedInstructionAccountMeta<\n * TAccountAuthority,\n * InstructionAccountInputAddress<TAccountAuthority>,\n * ReadonlySignerAccount<InstructionAccountInputAddress<TAccountAuthority>> &\n * AccountSignerMeta<InstructionAccountInputAddress<TAccountAuthority>>\n * >\n * >;\n * ```\n *\n * Alternatively, instruction builders may capture the caller's whole input in a single\n * `TInput` type parameter and index into it — at the cost of excess property checks, since\n * the input is then inferred as `TInput` itself.\n * ```ts\n * declare function getTransferInstruction<TInput extends TransferInput>(\n * input: TInput,\n * ): TransferInstruction<\n * ResolvedInstructionAccountMeta<\n * TInput['authority'],\n * InstructionAccountInputAddress<TInput['authority']>,\n * ReadonlySignerAccount<InstructionAccountInputAddress<TInput['authority']>> &\n * AccountSignerMeta<InstructionAccountInputAddress<TInput['authority']>>\n * >\n * >;\n * ```\n *\n * @see {@link getAccountMetaFactory}\n * @see {@link InstructionAccountInputAddress}\n */\nexport type ResolvedInstructionAccountMeta<TInput, TAddress extends string, TSignerMeta = TAddress> = [TInput] extends [\n { role: infer TRole extends AccountRole },\n]\n ? ([TInput] extends [{ signer: TransactionSigner<TAddress> }]\n ? AccountSignerMeta<TAddress>\n : AccountMeta<TAddress>) & { readonly role: TRole }\n : [TInput] extends [TransactionSigner<TAddress>]\n ? TSignerMeta\n : TAddress;\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 * The role of the resulting account meta is determined as follows, in order of precedence:\n * 1. If the value carries an explicit `role` — i.e. it is an {@link AccountNonSignerMeta} or\n * an {@link AccountSignerMeta} — that role is used as-is, regardless of the flags declared\n * by the program's IDL.\n * 2. Otherwise, if the value is a {@link TransactionSigner} and the account's `isSigner` flag\n * is not `false`, the IDL's writable flag is upgraded to the corresponding signer role and\n * the signer is attached to the meta. When `isSigner` is `false`, the signer merely acts\n * as an address carrier and no upgrade occurs. Omitting the flag is equivalent to `'either'`.\n * 3. Otherwise, the IDL's writable flag decides between the readonly and writable roles.\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 * @throws Throws a {@link SolanaError} when the account's `isSigner` flag is `true` but the\n * provided value is neither a {@link TransactionSigner} nor carries an explicit `role`. Use\n * `createNoopSigner()` from `@solana/signers` if the account's signature is provided by other means.\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 // Explicit roles always take precedence over the flags declared by the program's IDL.\n if (hasExplicitRole(account.value)) {\n return Object.freeze({\n address: account.value.address,\n role: account.value.role,\n ...('signer' in account.value && account.value.signer ? { signer: account.value.signer } : {}),\n });\n }\n\n // Only mark implicit values as signers when the IDL declares\n // the account as a signer or lets the input decide (`'either'`).\n const idlIsSigner = account.isSigner ?? 'either';\n const isSigner = idlIsSigner !== false && isResolvedInstructionAccountSigner(account.value);\n if (!isSigner && idlIsSigner === true) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_SIGNER, {\n inputName,\n });\n }\n\n const writableRole = account.isWritable ? AccountRole.WRITABLE : AccountRole.READONLY;\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\n/**\n * Checks whether a resolved instruction account value carries an explicit account role,\n * i.e. whether it is an {@link AccountNonSignerMeta} or an {@link AccountSignerMeta}.\n *\n * Since {@link AccountRole} is a numeric enum, requiring `role` to be a number prevents\n * unrelated `role` properties on address-bearing objects from being mistaken for a role\n * override.\n */\nfunction hasExplicitRole(\n value: NonNullable<ResolvedInstructionAccount['value']>,\n): value is AccountNonSignerMeta | AccountSignerMeta {\n return typeof value === 'object' && 'role' in value && typeof value.role === 'number';\n}\n\nfunction isResolvedInstructionAccountSigner<TAddress extends string = string>(\n value: unknown,\n): value is TransactionSigner<TAddress> {\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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/instruction-input-resolution.ts","../src/self-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,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.browser.mjs","sourcesContent":["import { type Address, type HasAddress, isProgramDerivedAddress, type ProgramDerivedAddress } from '@solana/addresses';\nimport {\n SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_NON_NULL,\n SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_SIGNER,\n SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE,\n SolanaError,\n} from '@solana/errors';\nimport { type AccountMeta, type AccountNonSignerMeta, AccountRole, upgradeRoleToSigner } from '@solana/instructions';\nimport { type AccountSignerMeta, isTransactionSigner, type TransactionSigner } from '@solana/signers';\n\n/**\n * Represents the accepted input values for a non-signer instruction account.\n *\n * Namely, one of the following:\n * - An {@link Address} — the most common case.\n * - Any object exposing an `address` property (see {@link HasAddress}) — e.g. a framework's\n * address wrapper class. Note that {@link TransactionSigner | TransactionSigners} satisfy this\n * shape too, in which case they act as plain address carriers for non-signer accounts.\n * - A {@link ProgramDerivedAddress} — i.e. an `[address, bump]` tuple.\n * - An {@link AccountNonSignerMeta} — i.e. `{ address, role }` — to explicitly override the\n * role derived from the program's IDL, e.g. to mark an account as writable or readonly.\n *\n * @typeParam TAddress - Supply a string literal to define an account having a particular address.\n *\n * @see {@link InstructionSignerInput}\n */\nexport type InstructionAccountInput<TAddress extends string = string> =\n | AccountNonSignerMeta<TAddress>\n | Address<TAddress>\n | HasAddress<TAddress>\n | ProgramDerivedAddress<TAddress>;\n\n/**\n * Represents the accepted input values for a signer instruction account.\n *\n * Namely, one of the following:\n * - A {@link TransactionSigner} — the most common case.\n * - An {@link AccountSignerMeta} — i.e. `{ address, role, signer }` — to explicitly override the\n * role derived from the program's IDL, e.g. to mark a signer account as writable or readonly.\n *\n * @typeParam TAddress - Supply a string literal to define an account having a particular address.\n *\n * @see {@link InstructionAccountInput}\n */\nexport type InstructionSignerInput<TAddress extends string = string> =\n | AccountSignerMeta<TAddress>\n | TransactionSigner<TAddress>;\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 any object exposing an `address` property — such as a {@link TransactionSigner}, an\n * account meta, or a framework's address wrapper class (see {@link HasAddress}). This\n * function extracts the underlying address from 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 — or an\n * {@link AccountSignerMeta} carrying one — and returns the signer.\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 const signer = value && hasExplicitRole(value) && 'signer' in value ? value.signer : value;\n if (!isResolvedInstructionAccountSigner<T>(signer)) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE, {\n expectedType: 'TransactionSigner',\n inputName,\n });\n }\n return signer;\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 captures\n * the account value alongside the signer and writable flags declared by the program's IDL.\n * The value can be any {@link InstructionAccountInput}, any {@link InstructionSignerInput},\n * or `null` for optional accounts.\n *\n * The optional `isSigner` flag describes whether the IDL requires the account to sign the\n * transaction — with `'either'` meaning the account may or may not be a signer, in which\n * case providing a {@link TransactionSigner} value is what marks it as one. Omitting the\n * flag — e.g. in program clients generated before its introduction — is equivalent to\n * setting it to `'either'`.\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 * isSigner: false,\n * isWritable: true,\n * };\n * ```\n */\nexport type ResolvedInstructionAccount<\n TAddress extends string = string,\n TValue extends InstructionAccountInput<TAddress> | InstructionSignerInput<TAddress> | null =\n | InstructionAccountInput<TAddress>\n | InstructionSignerInput<TAddress>\n | null,\n> = {\n isSigner?: boolean | 'either';\n isWritable: boolean;\n value: TValue;\n};\n\n/**\n * Extracts the address type parameter from an instruction account input.\n *\n * Given any {@link InstructionAccountInput} or {@link InstructionSignerInput} — e.g. an\n * {@link Address}, an address-bearing object (see {@link HasAddress}), a\n * {@link ProgramDerivedAddress} or an account meta — this type helper resolves to the\n * branded address string it carries. This allows generated program clients to recover the\n * address type parameter of an account from the caller's input type alone — e.g. via\n * `InstructionAccountInputAddress<TInput['authority']>` — instead of declaring a dedicated\n * address type parameter on the instruction builder.\n *\n * When given a union of inputs, the helper distributes over it, so a union whose members\n * all share the same address brand resolves to that brand. Inputs carrying no brand\n * resolve to `string`.\n *\n * @typeParam TInput - The type of the input provided by the caller for this account.\n *\n * @example\n * ```ts\n * type A = InstructionAccountInputAddress<Address<'1234'>>; // '1234'\n * type B = InstructionAccountInputAddress<TransactionSigner<'1234'>>; // '1234'\n * type C = InstructionAccountInputAddress<ProgramDerivedAddress<'1234'>>; // '1234'\n * type D = InstructionAccountInputAddress<Address>; // string\n * ```\n *\n * @see {@link ResolvedInstructionAccountMeta}\n */\nexport type InstructionAccountInputAddress<TInput> =\n TInput extends HasAddress<infer TAddress>\n ? TAddress\n : TInput extends ProgramDerivedAddress<infer TAddress>\n ? TAddress\n : TInput extends Address<infer TAddress>\n ? TAddress\n : string;\n\n/**\n * Computes the account meta type produced by an instruction account, based on the input\n * provided by the caller.\n *\n * This type helper mirrors the runtime logic of {@link getAccountMetaFactory} so that\n * generated program clients can accurately type the accounts of the instructions they\n * return. Namely:\n * - When the input carries an explicit `role` — i.e. it is an {@link AccountNonSignerMeta} or an\n * {@link AccountSignerMeta} — the meta type preserves the input's role type: an inline\n * `role: AccountRole.READONLY` override resolves to `ReadonlyAccount`, while a role only\n * known at runtime widens to {@link AccountMeta}. If the input also carries a `signer`,\n * {@link AccountSignerMeta} is used so the attached signer is reflected in the type.\n * - When the input is a {@link TransactionSigner}, the meta type is `TSignerMeta` — e.g.\n * `ReadonlySignerAccount<TAddress> & AccountSignerMeta<TAddress>` for accounts the IDL\n * declares as signers. For non-signer accounts, `TSignerMeta` should be left to its\n * default of `TAddress` so that signers merely act as address carriers.\n * - Otherwise, the helper resolves to `TAddress` — the branded address string that generated\n * instruction types map to the account meta declared by the program's IDL.\n *\n * Note that the checks are wrapped in tuples (`[TInput] extends [...]`) to prevent unions\n * from distributing. If `TInput` is not narrowed to the caller's specific input type — e.g.\n * when a declared input union is provided instead — the helper deterministically falls back\n * to `TAddress`, matching the account meta declared by the program's IDL.\n *\n * @typeParam TInput - The type of the input provided by the caller for this account.\n * @typeParam TAddress - The address type parameter of the account.\n * @typeParam TSignerMeta - The meta type produced when a {@link TransactionSigner} is\n * provided. Defaults to `TAddress`, which treats signers as plain address carriers.\n *\n * @example\n * The instruction builder below captures the caller's input in a single `TInput` type\n * parameter and recovers each account's address type parameter from it using\n * {@link InstructionAccountInputAddress}.\n * ```ts\n * declare function getTransferInstruction<TInput extends TransferInput>(\n * input: TInput,\n * ): TransferInstruction<\n * ResolvedInstructionAccountMeta<\n * TInput['authority'],\n * InstructionAccountInputAddress<TInput['authority']>,\n * ReadonlySignerAccount<InstructionAccountInputAddress<TInput['authority']>> &\n * AccountSignerMeta<InstructionAccountInputAddress<TInput['authority']>>\n * >\n * >;\n * ```\n *\n * Alternatively, instruction builders may keep a dedicated address type parameter per\n * account. In that case, the parameter below must intersect the concrete input type with\n * the inferred `TInput` type parameter (`TransferInput<TAccountAuthority> & TInput`) —\n * referencing the address type parameters only in `TInput`'s constraint makes their\n * inference fall back to `string`. Defaulting `TInput` to the concrete input type keeps\n * call sites with explicit type arguments working.\n * ```ts\n * declare function getTransferInstruction<\n * TAccountAuthority extends string,\n * TInput extends TransferInput<TAccountAuthority> = TransferInput<TAccountAuthority>,\n * >(\n * input: TransferInput<TAccountAuthority> & TInput,\n * ): TransferInstruction<\n * ResolvedInstructionAccountMeta<\n * TInput['authority'],\n * TAccountAuthority,\n * ReadonlySignerAccount<TAccountAuthority> & AccountSignerMeta<TAccountAuthority>\n * >\n * >;\n * ```\n *\n * @see {@link getAccountMetaFactory}\n * @see {@link InstructionAccountInputAddress}\n */\nexport type ResolvedInstructionAccountMeta<TInput, TAddress extends string, TSignerMeta = TAddress> = [TInput] extends [\n { role: infer TRole extends AccountRole },\n]\n ? ([TInput] extends [{ signer: TransactionSigner<TAddress> }]\n ? AccountSignerMeta<TAddress>\n : AccountMeta<TAddress>) & { readonly role: TRole }\n : [TInput] extends [TransactionSigner<TAddress>]\n ? TSignerMeta\n : TAddress;\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 * The role of the resulting account meta is determined as follows, in order of precedence:\n * 1. If the value carries an explicit `role` — i.e. it is an {@link AccountNonSignerMeta} or\n * an {@link AccountSignerMeta} — that role is used as-is, regardless of the flags declared\n * by the program's IDL.\n * 2. Otherwise, if the value is a {@link TransactionSigner} and the account's `isSigner` flag\n * is not `false`, the IDL's writable flag is upgraded to the corresponding signer role and\n * the signer is attached to the meta. When `isSigner` is `false`, the signer merely acts\n * as an address carrier and no upgrade occurs. Omitting the flag is equivalent to `'either'`.\n * 3. Otherwise, the IDL's writable flag decides between the readonly and writable roles.\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 * @throws Throws a {@link SolanaError} when the account's `isSigner` flag is `true` but the\n * provided value is neither a {@link TransactionSigner} nor carries an explicit `role`. Use\n * `createNoopSigner()` from `@solana/signers` if the account's signature is provided by other means.\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 // Explicit roles always take precedence over the flags declared by the program's IDL.\n if (hasExplicitRole(account.value)) {\n return Object.freeze({\n address: account.value.address,\n role: account.value.role,\n ...('signer' in account.value && account.value.signer ? { signer: account.value.signer } : {}),\n });\n }\n\n // Only mark implicit values as signers when the IDL declares\n // the account as a signer or lets the input decide (`'either'`).\n const idlIsSigner = account.isSigner ?? 'either';\n const isSigner = idlIsSigner !== false && isResolvedInstructionAccountSigner(account.value);\n if (!isSigner && idlIsSigner === true) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_SIGNER, {\n inputName,\n });\n }\n\n const writableRole = account.isWritable ? AccountRole.WRITABLE : AccountRole.READONLY;\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\n/**\n * Checks whether a resolved instruction account value carries an explicit account role,\n * i.e. whether it is an {@link AccountNonSignerMeta} or an {@link AccountSignerMeta}.\n *\n * Since {@link AccountRole} is a numeric enum, requiring `role` to be a number prevents\n * unrelated `role` properties on address-bearing objects from being mistaken for a role\n * override.\n */\nfunction hasExplicitRole(\n value: NonNullable<ResolvedInstructionAccount['value']>,\n): value is AccountNonSignerMeta | AccountSignerMeta {\n return typeof value === 'object' && 'role' in value && typeof value.role === 'number';\n}\n\nfunction isResolvedInstructionAccountSigner<TAddress extends string = string>(\n value: unknown,\n): value is TransactionSigner<TAddress> {\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"]}
|
|
1
|
+
{"version":3,"sources":["../src/instruction-input-resolution.ts","../src/self-fetch-functions.ts","../src/self-plan-and-send-functions.ts"],"names":[],"mappings":";;;;;;;AAuEO,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;AAuBO,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,I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AAA,CAAgB,MAAM,OAAO,MAAM,CAAA;AAC3F,IAAA,QAAA,CAAS,gBAAA,GAAmB,OAAM,MAAA,KAAU,MAAM,OAAO,gBAAA,CAAiB,MAAM,OAAO,MAAM,CAAA;AAC7F,IAAA,QAAA,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.browser.mjs","sourcesContent":["import { type Address, type HasAddress, isProgramDerivedAddress, type ProgramDerivedAddress } from '@solana/addresses';\nimport {\n SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_NON_NULL,\n SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_SIGNER,\n SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE,\n SolanaError,\n} from '@solana/errors';\nimport { type AccountMeta, type AccountNonSignerMeta, AccountRole, upgradeRoleToSigner } from '@solana/instructions';\nimport { type AccountSignerMeta, isTransactionSigner, type TransactionSigner } from '@solana/signers';\n\n/**\n * Represents the accepted input values for a non-signer instruction account.\n *\n * Namely, one of the following:\n * - An {@link Address} — the most common case.\n * - Any object exposing an `address` property (see {@link HasAddress}) — e.g. a framework's\n * address wrapper class. Note that {@link TransactionSigner | TransactionSigners} satisfy this\n * shape too, in which case they act as plain address carriers for non-signer accounts.\n * - A {@link ProgramDerivedAddress} — i.e. an `[address, bump]` tuple.\n * - An {@link AccountNonSignerMeta} — i.e. `{ address, role }` — to explicitly override the\n * role derived from the program's IDL, e.g. to mark an account as writable or readonly.\n *\n * @typeParam TAddress - Supply a string literal to define an account having a particular address.\n *\n * @see {@link InstructionSignerInput}\n */\nexport type InstructionAccountInput<TAddress extends string = string> =\n | AccountNonSignerMeta<TAddress>\n | Address<TAddress>\n | HasAddress<TAddress>\n | ProgramDerivedAddress<TAddress>;\n\n/**\n * Represents the accepted input values for a signer instruction account.\n *\n * Namely, one of the following:\n * - A {@link TransactionSigner} — the most common case.\n * - An {@link AccountSignerMeta} — i.e. `{ address, role, signer }` — to explicitly override the\n * role derived from the program's IDL, e.g. to mark a signer account as writable or readonly.\n *\n * @typeParam TAddress - Supply a string literal to define an account having a particular address.\n *\n * @see {@link InstructionAccountInput}\n */\nexport type InstructionSignerInput<TAddress extends string = string> =\n | AccountSignerMeta<TAddress>\n | TransactionSigner<TAddress>;\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 any object exposing an `address` property — such as a {@link TransactionSigner}, an\n * account meta, or a framework's address wrapper class (see {@link HasAddress}). This\n * function extracts the underlying address from 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.value);\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.value);\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 — or an\n * {@link AccountSignerMeta} carrying one — and returns the signer.\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.value);\n * ```\n */\nexport function getResolvedInstructionAccountAsTransactionSigner<T extends string = string>(\n inputName: string,\n value: ResolvedInstructionAccount<T>['value'] | undefined,\n): TransactionSigner<T> {\n const signer = value && hasExplicitRole(value) && 'signer' in value ? value.signer : value;\n if (!isResolvedInstructionAccountSigner<T>(signer)) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__UNEXPECTED_RESOLVED_INSTRUCTION_INPUT_TYPE, {\n expectedType: 'TransactionSigner',\n inputName,\n });\n }\n return signer;\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 captures\n * the account value alongside the signer and writable flags declared by the program's IDL.\n * The value can be any {@link InstructionAccountInput}, any {@link InstructionSignerInput},\n * or `null` for optional accounts.\n *\n * The optional `isSigner` flag describes whether the IDL requires the account to sign the\n * transaction — with `'either'` meaning the account may or may not be a signer, in which\n * case providing a {@link TransactionSigner} value is what marks it as one. Omitting the\n * flag — e.g. in program clients generated before its introduction — is equivalent to\n * setting it to `'either'`.\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 * isSigner: false,\n * isWritable: true,\n * };\n * ```\n */\nexport type ResolvedInstructionAccount<\n TAddress extends string = string,\n TValue extends InstructionAccountInput<TAddress> | InstructionSignerInput<TAddress> | null =\n | InstructionAccountInput<TAddress>\n | InstructionSignerInput<TAddress>\n | null,\n> = {\n isSigner?: boolean | 'either';\n isWritable: boolean;\n value: TValue;\n};\n\n/**\n * Extracts the address type parameter from an instruction account input.\n *\n * Given any {@link InstructionAccountInput} or {@link InstructionSignerInput} — e.g. an\n * {@link Address}, an address-bearing object (see {@link HasAddress}), a\n * {@link ProgramDerivedAddress} or an account meta — this type helper resolves to the\n * branded address string it carries. This allows generated program clients to recover the\n * address type parameter of an account from the caller's input type alone — e.g. via\n * `InstructionAccountInputAddress<TAccountAuthority>` where `TAccountAuthority` captures the\n * input provided for the `authority` account — instead of declaring a dedicated address\n * type parameter on the instruction builder.\n *\n * When given a union of inputs, the helper distributes over it, so a union whose members\n * all share the same address brand resolves to that brand. Inputs carrying no brand\n * resolve to `string`.\n *\n * @typeParam TInput - The type of the input provided by the caller for this account.\n *\n * @example\n * ```ts\n * type A = InstructionAccountInputAddress<Address<'1234'>>; // '1234'\n * type B = InstructionAccountInputAddress<TransactionSigner<'1234'>>; // '1234'\n * type C = InstructionAccountInputAddress<ProgramDerivedAddress<'1234'>>; // '1234'\n * type D = InstructionAccountInputAddress<Address>; // string\n * ```\n *\n * @see {@link ResolvedInstructionAccountMeta}\n */\nexport type InstructionAccountInputAddress<TInput> =\n TInput extends HasAddress<infer TAddress>\n ? TAddress\n : TInput extends ProgramDerivedAddress<infer TAddress>\n ? TAddress\n : TInput extends Address<infer TAddress>\n ? TAddress\n : string;\n\n/**\n * Computes the account meta type produced by an instruction account, based on the input\n * provided by the caller.\n *\n * This type helper mirrors the runtime logic of {@link getAccountMetaFactory} so that\n * generated program clients can accurately type the accounts of the instructions they\n * return. Namely:\n * - When the input carries an explicit `role` — i.e. it is an {@link AccountNonSignerMeta} or an\n * {@link AccountSignerMeta} — the meta type preserves the input's role type: an inline\n * `role: AccountRole.READONLY` override resolves to `ReadonlyAccount`, while a role only\n * known at runtime widens to {@link AccountMeta}. If the input also carries a `signer`,\n * {@link AccountSignerMeta} is used so the attached signer is reflected in the type.\n * - When the input is a {@link TransactionSigner}, the meta type is `TSignerMeta` — e.g.\n * `ReadonlySignerAccount<TAddress> & AccountSignerMeta<TAddress>` for accounts the IDL\n * declares as signers. For non-signer accounts, `TSignerMeta` should be left to its\n * default of `TAddress` so that signers merely act as address carriers.\n * - Otherwise, the helper resolves to `TAddress` — the branded address string that generated\n * instruction types map to the account meta declared by the program's IDL.\n *\n * Note that the checks are wrapped in tuples (`[TInput] extends [...]`) to prevent unions\n * from distributing. If `TInput` is not narrowed to the caller's specific input type — e.g.\n * when a declared input union is provided instead — the helper deterministically falls back\n * to `TAddress`, matching the account meta declared by the program's IDL.\n *\n * @typeParam TInput - The type of the input provided by the caller for this account.\n * @typeParam TAddress - The address type parameter of the account.\n * @typeParam TSignerMeta - The meta type produced when a {@link TransactionSigner} is\n * provided. Defaults to `TAddress`, which treats signers as plain address carriers.\n *\n * @example\n * The instruction builder below — the shape emitted by the Codama JS renderer — declares\n * one type parameter per account holding the input value provided for that account, and\n * recovers the account's address type parameter from it using\n * {@link InstructionAccountInputAddress}. Since its `input` parameter remains a concrete\n * object type once inferred, TypeScript keeps performing excess property checks on it, so\n * a misspelled optional account is a compile error rather than silently falling back to\n * its default value.\n * ```ts\n * type TransferInput<\n * TAccountAuthority extends InstructionAccountInput | InstructionSignerInput =\n * | InstructionAccountInput\n * | InstructionSignerInput,\n * > = { authority: TAccountAuthority; amount: bigint };\n *\n * declare function getTransferInstruction<\n * TAccountAuthority extends InstructionAccountInput | InstructionSignerInput,\n * >(\n * input: TransferInput<TAccountAuthority>,\n * ): TransferInstruction<\n * ResolvedInstructionAccountMeta<\n * TAccountAuthority,\n * InstructionAccountInputAddress<TAccountAuthority>,\n * ReadonlySignerAccount<InstructionAccountInputAddress<TAccountAuthority>> &\n * AccountSignerMeta<InstructionAccountInputAddress<TAccountAuthority>>\n * >\n * >;\n * ```\n *\n * Alternatively, instruction builders may capture the caller's whole input in a single\n * `TInput` type parameter and index into it — at the cost of excess property checks, since\n * the input is then inferred as `TInput` itself.\n * ```ts\n * declare function getTransferInstruction<TInput extends TransferInput>(\n * input: TInput,\n * ): TransferInstruction<\n * ResolvedInstructionAccountMeta<\n * TInput['authority'],\n * InstructionAccountInputAddress<TInput['authority']>,\n * ReadonlySignerAccount<InstructionAccountInputAddress<TInput['authority']>> &\n * AccountSignerMeta<InstructionAccountInputAddress<TInput['authority']>>\n * >\n * >;\n * ```\n *\n * @see {@link getAccountMetaFactory}\n * @see {@link InstructionAccountInputAddress}\n */\nexport type ResolvedInstructionAccountMeta<TInput, TAddress extends string, TSignerMeta = TAddress> = [TInput] extends [\n { role: infer TRole extends AccountRole },\n]\n ? ([TInput] extends [{ signer: TransactionSigner<TAddress> }]\n ? AccountSignerMeta<TAddress>\n : AccountMeta<TAddress>) & { readonly role: TRole }\n : [TInput] extends [TransactionSigner<TAddress>]\n ? TSignerMeta\n : TAddress;\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 * The role of the resulting account meta is determined as follows, in order of precedence:\n * 1. If the value carries an explicit `role` — i.e. it is an {@link AccountNonSignerMeta} or\n * an {@link AccountSignerMeta} — that role is used as-is, regardless of the flags declared\n * by the program's IDL.\n * 2. Otherwise, if the value is a {@link TransactionSigner} and the account's `isSigner` flag\n * is not `false`, the IDL's writable flag is upgraded to the corresponding signer role and\n * the signer is attached to the meta. When `isSigner` is `false`, the signer merely acts\n * as an address carrier and no upgrade occurs. Omitting the flag is equivalent to `'either'`.\n * 3. Otherwise, the IDL's writable flag decides between the readonly and writable roles.\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 * @throws Throws a {@link SolanaError} when the account's `isSigner` flag is `true` but the\n * provided value is neither a {@link TransactionSigner} nor carries an explicit `role`. Use\n * `createNoopSigner()` from `@solana/signers` if the account's signature is provided by other means.\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 // Explicit roles always take precedence over the flags declared by the program's IDL.\n if (hasExplicitRole(account.value)) {\n return Object.freeze({\n address: account.value.address,\n role: account.value.role,\n ...('signer' in account.value && account.value.signer ? { signer: account.value.signer } : {}),\n });\n }\n\n // Only mark implicit values as signers when the IDL declares\n // the account as a signer or lets the input decide (`'either'`).\n const idlIsSigner = account.isSigner ?? 'either';\n const isSigner = idlIsSigner !== false && isResolvedInstructionAccountSigner(account.value);\n if (!isSigner && idlIsSigner === true) {\n throw new SolanaError(SOLANA_ERROR__PROGRAM_CLIENTS__RESOLVED_INSTRUCTION_INPUT_MUST_BE_SIGNER, {\n inputName,\n });\n }\n\n const writableRole = account.isWritable ? AccountRole.WRITABLE : AccountRole.READONLY;\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\n/**\n * Checks whether a resolved instruction account value carries an explicit account role,\n * i.e. whether it is an {@link AccountNonSignerMeta} or an {@link AccountSignerMeta}.\n *\n * Since {@link AccountRole} is a numeric enum, requiring `role` to be a number prevents\n * unrelated `role` properties on address-bearing objects from being mistaken for a role\n * override.\n */\nfunction hasExplicitRole(\n value: NonNullable<ResolvedInstructionAccount['value']>,\n): value is AccountNonSignerMeta | AccountSignerMeta {\n return typeof value === 'object' && 'role' in value && typeof value.role === 'number';\n}\n\nfunction isResolvedInstructionAccountSigner<TAddress extends string = string>(\n value: unknown,\n): value is TransactionSigner<TAddress> {\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"]}
|