@solana-program/compute-budget 0.15.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +3 -8
  2. package/dist/src/index.js +5 -4
  3. package/dist/src/index.js.map +1 -1
  4. package/dist/src/index.mjs +6 -5
  5. package/dist/src/index.mjs.map +1 -1
  6. package/dist/types/estimateAndSetComputeLimit.d.ts.map +1 -1
  7. package/dist/types/estimateComputeLimitInternal.d.ts.map +1 -1
  8. package/dist/types/generated/instructions/requestHeapFrame.d.ts +1 -1
  9. package/dist/types/generated/instructions/requestHeapFrame.d.ts.map +1 -1
  10. package/dist/types/generated/instructions/requestUnits.d.ts +1 -1
  11. package/dist/types/generated/instructions/requestUnits.d.ts.map +1 -1
  12. package/dist/types/generated/instructions/setComputeUnitLimit.d.ts +1 -1
  13. package/dist/types/generated/instructions/setComputeUnitLimit.d.ts.map +1 -1
  14. package/dist/types/generated/instructions/setComputeUnitPrice.d.ts +1 -1
  15. package/dist/types/generated/instructions/setComputeUnitPrice.d.ts.map +1 -1
  16. package/dist/types/generated/instructions/setLoadedAccountsDataSizeLimit.d.ts +1 -1
  17. package/dist/types/generated/instructions/setLoadedAccountsDataSizeLimit.d.ts.map +1 -1
  18. package/dist/types/generated/programs/computeBudget.d.ts +5 -3
  19. package/dist/types/generated/programs/computeBudget.d.ts.map +1 -1
  20. package/dist/types/introspect.d.ts.map +1 -1
  21. package/dist/types/setComputeLimit.d.ts.map +1 -1
  22. package/dist/types/setComputePrice.d.ts.map +1 -1
  23. package/package.json +34 -29
  24. package/src/constants.ts +12 -0
  25. package/src/estimateAndSetComputeLimit.ts +58 -0
  26. package/src/estimateComputeLimit.ts +77 -0
  27. package/src/estimateComputeLimitInternal.ts +167 -0
  28. package/src/generated/index.ts +10 -0
  29. package/src/generated/instructions/index.ts +13 -0
  30. package/src/generated/instructions/requestHeapFrame.ts +114 -0
  31. package/src/generated/instructions/requestUnits.ts +115 -0
  32. package/src/generated/instructions/setComputeUnitLimit.ts +110 -0
  33. package/src/generated/instructions/setComputeUnitPrice.ts +110 -0
  34. package/src/generated/instructions/setLoadedAccountsDataSizeLimit.ts +107 -0
  35. package/src/generated/programs/computeBudget.ts +188 -0
  36. package/src/generated/programs/index.ts +9 -0
  37. package/src/index.ts +8 -0
  38. package/src/introspect.ts +75 -0
  39. package/src/setComputeLimit.ts +76 -0
  40. package/src/setComputePrice.ts +78 -0
@@ -0,0 +1,12 @@
1
+ /**
2
+ * A provisory compute unit limit is used to indicate that the transaction
3
+ * should be estimated for compute units before being sent to the network.
4
+ *
5
+ * Setting it to zero ensures the transaction fails unless it is properly estimated.
6
+ */
7
+ export const PROVISORY_COMPUTE_UNIT_LIMIT = 0;
8
+
9
+ /**
10
+ * The maximum compute unit limit that can be set for a transaction.
11
+ */
12
+ export const MAX_COMPUTE_UNIT_LIMIT = 1_400_000;
@@ -0,0 +1,58 @@
1
+ import { TransactionMessage, TransactionMessageWithFeePayer } from '@solana/kit';
2
+
3
+ import { MAX_COMPUTE_UNIT_LIMIT, PROVISORY_COMPUTE_UNIT_LIMIT } from './constants';
4
+ import {
5
+ EstimateComputeUnitLimitFactoryFunction,
6
+ EstimateComputeUnitLimitFactoryFunctionConfig,
7
+ } from './estimateComputeLimitInternal';
8
+ import { findSetComputeUnitLimitInstructionIndexAndUnits } from './introspect';
9
+ import { updateOrAppendSetComputeUnitLimitInstruction } from './setComputeLimit';
10
+
11
+ type EstimateAndUpdateProvisoryComputeUnitLimitFactoryFunction = <
12
+ TTransactionMessage extends TransactionMessage & TransactionMessageWithFeePayer,
13
+ >(
14
+ transactionMessage: TTransactionMessage,
15
+ config?: EstimateComputeUnitLimitFactoryFunctionConfig,
16
+ ) => Promise<TTransactionMessage>;
17
+
18
+ /**
19
+ * Given a transaction message, if it does not have an explicit compute unit limit,
20
+ * estimates the compute unit limit and updates the transaction message with
21
+ * the estimated limit. Otherwise, returns the transaction message unchanged.
22
+ *
23
+ * It requires a function that estimates the compute unit limit.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * const estimateAndUpdateCUs = estimateAndUpdateProvisoryComputeUnitLimitFactory(
28
+ * estimateComputeUnitLimitFactory({ rpc })
29
+ * );
30
+ *
31
+ * const transactionMessageWithCUs = await estimateAndUpdateCUs(transactionMessage);
32
+ * ```
33
+ *
34
+ * @see {@link estimateAndUpdateProvisoryComputeUnitLimitFactory}
35
+ */
36
+ export function estimateAndUpdateProvisoryComputeUnitLimitFactory(
37
+ estimateComputeUnitLimit: EstimateComputeUnitLimitFactoryFunction,
38
+ ): EstimateAndUpdateProvisoryComputeUnitLimitFactoryFunction {
39
+ return async function fn(transactionMessage, config) {
40
+ const instructionDetails = findSetComputeUnitLimitInstructionIndexAndUnits(transactionMessage);
41
+
42
+ // If the transaction message already has a compute unit limit instruction
43
+ // which is set to a specific value — i.e. not 0 or the maximum limit —
44
+ // we don't need to estimate the compute unit limit.
45
+ if (
46
+ instructionDetails &&
47
+ instructionDetails.units !== PROVISORY_COMPUTE_UNIT_LIMIT &&
48
+ instructionDetails.units !== MAX_COMPUTE_UNIT_LIMIT
49
+ ) {
50
+ return transactionMessage;
51
+ }
52
+
53
+ return updateOrAppendSetComputeUnitLimitInstruction(
54
+ await estimateComputeUnitLimit(transactionMessage, config),
55
+ transactionMessage,
56
+ );
57
+ };
58
+ }
@@ -0,0 +1,77 @@
1
+ import {
2
+ estimateComputeUnitLimit,
3
+ EstimateComputeUnitLimitFactoryConfig,
4
+ EstimateComputeUnitLimitFactoryFunction,
5
+ } from './estimateComputeLimitInternal';
6
+
7
+ /**
8
+ * Use this utility to estimate the actual compute unit cost of a given transaction message.
9
+ *
10
+ * Correctly budgeting a compute unit limit for your transaction message can increase the
11
+ * probability that your transaction will be accepted for processing. If you don't declare a compute
12
+ * unit limit on your transaction, validators will assume an upper limit of 200K compute units (CU)
13
+ * per instruction.
14
+ *
15
+ * Since validators have an incentive to pack as many transactions into each block as possible, they
16
+ * may choose to include transactions that they know will fit into the remaining compute budget for
17
+ * the current block over transactions that might not. For this reason, you should set a compute
18
+ * unit limit on each of your transaction messages, whenever possible.
19
+ *
20
+ * > [!WARNING]
21
+ * > The compute unit estimate is just that -- an estimate. The compute unit consumption of the
22
+ * > actual transaction might be higher or lower than what was observed in simulation. Unless you
23
+ * > are confident that your particular transaction message will consume the same or fewer compute
24
+ * > units as was estimated, you might like to augment the estimate by either a fixed number of CUs
25
+ * > or a multiplier.
26
+ *
27
+ * > [!NOTE]
28
+ * > If you are preparing an _unsigned_ transaction, destined to be signed and submitted to the
29
+ * > network by a wallet, you might like to leave it up to the wallet to determine the compute unit
30
+ * > limit. Consider that the wallet might have a more global view of how many compute units certain
31
+ * > types of transactions consume, and might be able to make better estimates of an appropriate
32
+ * > compute unit budget.
33
+ *
34
+ * > [!INFO]
35
+ * > In the event that a transaction message does not already have a `SetComputeUnitLimit`
36
+ * > instruction, this function will add one before simulation. This ensures that the compute unit
37
+ * > consumption of the `SetComputeUnitLimit` instruction itself is included in the estimate.
38
+ *
39
+ * @param config
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * import { getSetComputeUnitLimitInstruction } from '@solana-program/compute-budget';
44
+ * import { createSolanaRpc, estimateComputeUnitLimitFactory, pipe } from '@solana/kit';
45
+ *
46
+ * // Create an estimator function.
47
+ * const rpc = createSolanaRpc('http://127.0.0.1:8899');
48
+ * const estimateComputeUnitLimit = estimateComputeUnitLimitFactory({ rpc });
49
+ *
50
+ * // Create your transaction message.
51
+ * const transactionMessage = pipe(
52
+ * createTransactionMessage({ version: 'legacy' }),
53
+ * /* ... *\/
54
+ * );
55
+ *
56
+ * // Request an estimate of the actual compute units this message will consume. This is done by
57
+ * // simulating the transaction and grabbing the estimated compute units from the result.
58
+ * const estimatedUnits = await estimateComputeUnitLimit(transactionMessage);
59
+ *
60
+ * // Set the transaction message's compute unit budget.
61
+ * const transactionMessageWithComputeUnitLimit = prependTransactionMessageInstruction(
62
+ * getSetComputeUnitLimitInstruction({ units: estimatedUnits }),
63
+ * transactionMessage,
64
+ * );
65
+ * ```
66
+ */
67
+ export function estimateComputeUnitLimitFactory({
68
+ rpc,
69
+ }: EstimateComputeUnitLimitFactoryConfig): EstimateComputeUnitLimitFactoryFunction {
70
+ return async function estimateComputeUnitLimitFactoryFunction(transactionMessage, config) {
71
+ return await estimateComputeUnitLimit({
72
+ ...config,
73
+ rpc,
74
+ transactionMessage,
75
+ });
76
+ };
77
+ }
@@ -0,0 +1,167 @@
1
+ import {
2
+ TransactionMessage,
3
+ Commitment,
4
+ compileTransaction,
5
+ getBase64EncodedWireTransaction,
6
+ getSolanaErrorFromTransactionError,
7
+ isSolanaError,
8
+ isTransactionMessageWithDurableNonceLifetime,
9
+ pipe,
10
+ Rpc,
11
+ RpcSimulateTransactionResult,
12
+ SimulateTransactionApi,
13
+ Slot,
14
+ SOLANA_ERROR__TRANSACTION__FAILED_TO_ESTIMATE_COMPUTE_LIMIT,
15
+ SOLANA_ERROR__TRANSACTION__FAILED_WHEN_SIMULATING_TO_ESTIMATE_COMPUTE_LIMIT,
16
+ SolanaError,
17
+ Transaction,
18
+ TransactionMessageWithFeePayer,
19
+ } from '@solana/kit';
20
+
21
+ import { MAX_COMPUTE_UNIT_LIMIT } from './constants';
22
+ import { updateOrAppendSetComputeUnitLimitInstruction } from './setComputeLimit';
23
+
24
+ export type EstimateComputeUnitLimitFactoryConfig = Readonly<{
25
+ /** An object that supports the {@link SimulateTransactionApi} of the Solana RPC API */
26
+ rpc: Rpc<SimulateTransactionApi>;
27
+ }>;
28
+
29
+ export type EstimateComputeUnitLimitFactoryFunction = (
30
+ transactionMessage: TransactionMessage & TransactionMessageWithFeePayer,
31
+ config?: EstimateComputeUnitLimitFactoryFunctionConfig,
32
+ ) => Promise<number>;
33
+
34
+ export type EstimateComputeUnitLimitFactoryFunctionConfig = {
35
+ abortSignal?: AbortSignal;
36
+ /**
37
+ * Compute the estimate as of the highest slot that has reached this level of commitment.
38
+ *
39
+ * @defaultValue Whichever default is applied by the underlying {@link RpcApi} in use. For
40
+ * example, when using an API created by a `createSolanaRpc*()` helper, the default commitment
41
+ * is `"confirmed"` unless configured otherwise. Unmitigated by an API layer on the client, the
42
+ * default commitment applied by the server is `"finalized"`.
43
+ */
44
+ commitment?: Commitment;
45
+ /**
46
+ * Prevents accessing stale data by enforcing that the RPC node has processed transactions up to
47
+ * this slot
48
+ */
49
+ minContextSlot?: Slot;
50
+ };
51
+
52
+ type EstimateComputeUnitLimitConfig = EstimateComputeUnitLimitFactoryFunctionConfig &
53
+ Readonly<{
54
+ rpc: Rpc<SimulateTransactionApi>;
55
+ transactionMessage: TransactionMessage & TransactionMessageWithFeePayer;
56
+ }>;
57
+
58
+ /**
59
+ * Simulates a transaction message on the network and returns the number of compute units it
60
+ * consumed during simulation.
61
+ *
62
+ * The estimate this function returns can be used to set a compute unit limit on the transaction.
63
+ * Correctly budgeting a compute unit limit for your transaction message can increase the probability
64
+ * that your transaction will be accepted for processing.
65
+ *
66
+ * If you don't declare a compute unit limit on your transaction, validators will assume an upper
67
+ * limit of 200K compute units (CU) per instruction. Since validators have an incentive to pack as
68
+ * many transactions into each block as possible, they may choose to include transactions that they
69
+ * know will fit into the remaining compute budget for the current block over transactions that
70
+ * might not. For this reason, you should set a compute unit limit on each of your transaction
71
+ * messages, whenever possible.
72
+ *
73
+ * ## Example
74
+ *
75
+ * ```ts
76
+ * import { getSetComputeLimitInstruction } from '@solana-program/compute-budget';
77
+ * import { createSolanaRpc, getComputeUnitEstimateForTransactionMessageFactory, pipe } from '@solana/kit';
78
+ *
79
+ * // Create an estimator function.
80
+ * const rpc = createSolanaRpc('http://127.0.0.1:8899');
81
+ * const getComputeUnitEstimateForTransactionMessage =
82
+ * getComputeUnitEstimateForTransactionMessageFactory({ rpc });
83
+ *
84
+ * // Create your transaction message.
85
+ * const transactionMessage = pipe(
86
+ * createTransactionMessage({ version: 'legacy' }),
87
+ * /* ... *\/
88
+ * );
89
+ *
90
+ * // Request an estimate of the actual compute units this message will consume.
91
+ * const computeUnitsEstimate =
92
+ * await getComputeUnitEstimateForTransactionMessage(transactionMessage);
93
+ *
94
+ * // Set the transaction message's compute unit budget.
95
+ * const transactionMessageWithComputeUnitLimit = prependTransactionMessageInstruction(
96
+ * getSetComputeLimitInstruction({ units: computeUnitsEstimate }),
97
+ * transactionMessage,
98
+ * );
99
+ * ```
100
+ *
101
+ * > [!WARNING]
102
+ * > The compute unit estimate is just that &ndash; an estimate. The compute unit consumption of the
103
+ * > actual transaction might be higher or lower than what was observed in simulation. Unless you
104
+ * > are confident that your particular transaction message will consume the same or fewer compute
105
+ * > units as was estimated, you might like to augment the estimate by either a fixed number of CUs
106
+ * > or a multiplier.
107
+ *
108
+ * > [!NOTE]
109
+ * > If you are preparing an _unsigned_ transaction, destined to be signed and submitted to the
110
+ * > network by a wallet, you might like to leave it up to the wallet to determine the compute unit
111
+ * > limit. Consider that the wallet might have a more global view of how many compute units certain
112
+ * > types of transactions consume, and might be able to make better estimates of an appropriate
113
+ * > compute unit budget.
114
+ */
115
+ export async function estimateComputeUnitLimit({
116
+ transactionMessage,
117
+ ...configs
118
+ }: EstimateComputeUnitLimitConfig): Promise<number> {
119
+ const replaceRecentBlockhash = !isTransactionMessageWithDurableNonceLifetime(transactionMessage);
120
+ const transaction = pipe(
121
+ transactionMessage,
122
+ m => updateOrAppendSetComputeUnitLimitInstruction(MAX_COMPUTE_UNIT_LIMIT, m),
123
+ compileTransaction,
124
+ );
125
+
126
+ return await simulateTransactionAndGetConsumedUnits({
127
+ transaction,
128
+ replaceRecentBlockhash,
129
+ ...configs,
130
+ });
131
+ }
132
+
133
+ type SimulateTransactionAndGetConsumedUnitsConfig = Omit<EstimateComputeUnitLimitConfig, 'transactionMessage'> &
134
+ Readonly<{ replaceRecentBlockhash?: boolean; transaction: Transaction }>;
135
+
136
+ async function simulateTransactionAndGetConsumedUnits({
137
+ abortSignal,
138
+ rpc,
139
+ transaction,
140
+ ...simulateConfig
141
+ }: SimulateTransactionAndGetConsumedUnitsConfig): Promise<number> {
142
+ const wireTransactionBytes = getBase64EncodedWireTransaction(transaction);
143
+
144
+ try {
145
+ const response = await rpc
146
+ .simulateTransaction(wireTransactionBytes, { ...simulateConfig, encoding: 'base64', sigVerify: false })
147
+ .send({ abortSignal });
148
+ const { err: transactionError, ...simulationResult } = response.value as RpcSimulateTransactionResult;
149
+ if (simulationResult.unitsConsumed == null) {
150
+ // This should never be hit, because all RPCs should support `unitsConsumed` by now.
151
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__FAILED_TO_ESTIMATE_COMPUTE_LIMIT);
152
+ }
153
+ if (transactionError) {
154
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__FAILED_WHEN_SIMULATING_TO_ESTIMATE_COMPUTE_LIMIT, {
155
+ ...simulationResult,
156
+ cause: getSolanaErrorFromTransactionError(transactionError),
157
+ });
158
+ }
159
+ // FIXME(https://github.com/anza-xyz/agave/issues/1295): The simulation response returns
160
+ // compute units as a u64, but the `SetComputeLimit` instruction only accepts a u32. Until
161
+ // this changes, downcast it.
162
+ return simulationResult.unitsConsumed > 4_294_967_295n ? 4_294_967_295 : Number(simulationResult.unitsConsumed);
163
+ } catch (e) {
164
+ if (isSolanaError(e, SOLANA_ERROR__TRANSACTION__FAILED_WHEN_SIMULATING_TO_ESTIMATE_COMPUTE_LIMIT)) throw e;
165
+ throw new SolanaError(SOLANA_ERROR__TRANSACTION__FAILED_TO_ESTIMATE_COMPUTE_LIMIT, { cause: e });
166
+ }
167
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * This code was AUTOGENERATED using the Codama library.
3
+ * Please DO NOT EDIT THIS FILE, instead use visitors
4
+ * to add features, then rerun Codama to update it.
5
+ *
6
+ * @see https://github.com/codama-idl/codama
7
+ */
8
+
9
+ export * from './instructions';
10
+ export * from './programs';
@@ -0,0 +1,13 @@
1
+ /**
2
+ * This code was AUTOGENERATED using the Codama library.
3
+ * Please DO NOT EDIT THIS FILE, instead use visitors
4
+ * to add features, then rerun Codama to update it.
5
+ *
6
+ * @see https://github.com/codama-idl/codama
7
+ */
8
+
9
+ export * from './requestHeapFrame';
10
+ export * from './requestUnits';
11
+ export * from './setComputeUnitLimit';
12
+ export * from './setComputeUnitPrice';
13
+ export * from './setLoadedAccountsDataSizeLimit';
@@ -0,0 +1,114 @@
1
+ /**
2
+ * This code was AUTOGENERATED using the Codama library.
3
+ * Please DO NOT EDIT THIS FILE, instead use visitors
4
+ * to add features, then rerun Codama to update it.
5
+ *
6
+ * @see https://github.com/codama-idl/codama
7
+ */
8
+
9
+ import {
10
+ combineCodec,
11
+ getStructDecoder,
12
+ getStructEncoder,
13
+ getU32Decoder,
14
+ getU32Encoder,
15
+ getU8Decoder,
16
+ getU8Encoder,
17
+ transformEncoder,
18
+ type AccountMeta,
19
+ type Address,
20
+ type FixedSizeCodec,
21
+ type FixedSizeDecoder,
22
+ type FixedSizeEncoder,
23
+ type Instruction,
24
+ type InstructionWithAccounts,
25
+ type InstructionWithData,
26
+ type ReadonlyUint8Array,
27
+ } from '@solana/kit';
28
+ import { COMPUTE_BUDGET_PROGRAM_ADDRESS } from '../programs';
29
+
30
+ export const REQUEST_HEAP_FRAME_DISCRIMINATOR = 1;
31
+
32
+ export function getRequestHeapFrameDiscriminatorBytes(): ReadonlyUint8Array {
33
+ return getU8Encoder().encode(REQUEST_HEAP_FRAME_DISCRIMINATOR);
34
+ }
35
+
36
+ export type RequestHeapFrameInstruction<
37
+ TProgram extends string = typeof COMPUTE_BUDGET_PROGRAM_ADDRESS,
38
+ TRemainingAccounts extends readonly AccountMeta<string>[] = [],
39
+ > = Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<TRemainingAccounts>;
40
+
41
+ export type RequestHeapFrameInstructionData = {
42
+ discriminator: number;
43
+ /**
44
+ * Requested transaction-wide program heap size in bytes.
45
+ * Must be multiple of 1024. Applies to each program, including CPIs.
46
+ */
47
+ bytes: number;
48
+ };
49
+
50
+ export type RequestHeapFrameInstructionDataArgs = {
51
+ /**
52
+ * Requested transaction-wide program heap size in bytes.
53
+ * Must be multiple of 1024. Applies to each program, including CPIs.
54
+ */
55
+ bytes: number;
56
+ };
57
+
58
+ export function getRequestHeapFrameInstructionDataEncoder(): FixedSizeEncoder<RequestHeapFrameInstructionDataArgs> {
59
+ return transformEncoder(
60
+ getStructEncoder([
61
+ ['discriminator', getU8Encoder()],
62
+ ['bytes', getU32Encoder()],
63
+ ]),
64
+ value => ({ ...value, discriminator: REQUEST_HEAP_FRAME_DISCRIMINATOR }),
65
+ );
66
+ }
67
+
68
+ export function getRequestHeapFrameInstructionDataDecoder(): FixedSizeDecoder<RequestHeapFrameInstructionData> {
69
+ return getStructDecoder([
70
+ ['discriminator', getU8Decoder()],
71
+ ['bytes', getU32Decoder()],
72
+ ]);
73
+ }
74
+
75
+ export function getRequestHeapFrameInstructionDataCodec(): FixedSizeCodec<
76
+ RequestHeapFrameInstructionDataArgs,
77
+ RequestHeapFrameInstructionData
78
+ > {
79
+ return combineCodec(getRequestHeapFrameInstructionDataEncoder(), getRequestHeapFrameInstructionDataDecoder());
80
+ }
81
+
82
+ export type RequestHeapFrameInput = {
83
+ bytes: RequestHeapFrameInstructionDataArgs['bytes'];
84
+ };
85
+
86
+ export function getRequestHeapFrameInstruction<TProgramAddress extends Address = typeof COMPUTE_BUDGET_PROGRAM_ADDRESS>(
87
+ input: RequestHeapFrameInput,
88
+ config?: { programAddress?: TProgramAddress },
89
+ ): RequestHeapFrameInstruction<TProgramAddress> {
90
+ // Program address.
91
+ const programAddress = config?.programAddress ?? COMPUTE_BUDGET_PROGRAM_ADDRESS;
92
+
93
+ // Original args.
94
+ const args = { ...input };
95
+
96
+ return Object.freeze({
97
+ data: getRequestHeapFrameInstructionDataEncoder().encode(args as RequestHeapFrameInstructionDataArgs),
98
+ programAddress,
99
+ } as RequestHeapFrameInstruction<TProgramAddress>);
100
+ }
101
+
102
+ export type ParsedRequestHeapFrameInstruction<TProgram extends string = typeof COMPUTE_BUDGET_PROGRAM_ADDRESS> = {
103
+ programAddress: Address<TProgram>;
104
+ data: RequestHeapFrameInstructionData;
105
+ };
106
+
107
+ export function parseRequestHeapFrameInstruction<TProgram extends string>(
108
+ instruction: Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array>,
109
+ ): ParsedRequestHeapFrameInstruction<TProgram> {
110
+ return {
111
+ programAddress: instruction.programAddress,
112
+ data: getRequestHeapFrameInstructionDataDecoder().decode(instruction.data),
113
+ };
114
+ }
@@ -0,0 +1,115 @@
1
+ /**
2
+ * This code was AUTOGENERATED using the Codama library.
3
+ * Please DO NOT EDIT THIS FILE, instead use visitors
4
+ * to add features, then rerun Codama to update it.
5
+ *
6
+ * @see https://github.com/codama-idl/codama
7
+ */
8
+
9
+ import {
10
+ combineCodec,
11
+ getStructDecoder,
12
+ getStructEncoder,
13
+ getU32Decoder,
14
+ getU32Encoder,
15
+ getU8Decoder,
16
+ getU8Encoder,
17
+ transformEncoder,
18
+ type AccountMeta,
19
+ type Address,
20
+ type FixedSizeCodec,
21
+ type FixedSizeDecoder,
22
+ type FixedSizeEncoder,
23
+ type Instruction,
24
+ type InstructionWithAccounts,
25
+ type InstructionWithData,
26
+ type ReadonlyUint8Array,
27
+ } from '@solana/kit';
28
+ import { COMPUTE_BUDGET_PROGRAM_ADDRESS } from '../programs';
29
+
30
+ export const REQUEST_UNITS_DISCRIMINATOR = 0;
31
+
32
+ export function getRequestUnitsDiscriminatorBytes(): ReadonlyUint8Array {
33
+ return getU8Encoder().encode(REQUEST_UNITS_DISCRIMINATOR);
34
+ }
35
+
36
+ export type RequestUnitsInstruction<
37
+ TProgram extends string = typeof COMPUTE_BUDGET_PROGRAM_ADDRESS,
38
+ TRemainingAccounts extends readonly AccountMeta<string>[] = [],
39
+ > = Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<TRemainingAccounts>;
40
+
41
+ export type RequestUnitsInstructionData = {
42
+ discriminator: number;
43
+ /** Units to request for transaction-wide compute. */
44
+ units: number;
45
+ /** Prioritization fee lamports. */
46
+ additionalFee: number;
47
+ };
48
+
49
+ export type RequestUnitsInstructionDataArgs = {
50
+ /** Units to request for transaction-wide compute. */
51
+ units: number;
52
+ /** Prioritization fee lamports. */
53
+ additionalFee: number;
54
+ };
55
+
56
+ export function getRequestUnitsInstructionDataEncoder(): FixedSizeEncoder<RequestUnitsInstructionDataArgs> {
57
+ return transformEncoder(
58
+ getStructEncoder([
59
+ ['discriminator', getU8Encoder()],
60
+ ['units', getU32Encoder()],
61
+ ['additionalFee', getU32Encoder()],
62
+ ]),
63
+ value => ({ ...value, discriminator: REQUEST_UNITS_DISCRIMINATOR }),
64
+ );
65
+ }
66
+
67
+ export function getRequestUnitsInstructionDataDecoder(): FixedSizeDecoder<RequestUnitsInstructionData> {
68
+ return getStructDecoder([
69
+ ['discriminator', getU8Decoder()],
70
+ ['units', getU32Decoder()],
71
+ ['additionalFee', getU32Decoder()],
72
+ ]);
73
+ }
74
+
75
+ export function getRequestUnitsInstructionDataCodec(): FixedSizeCodec<
76
+ RequestUnitsInstructionDataArgs,
77
+ RequestUnitsInstructionData
78
+ > {
79
+ return combineCodec(getRequestUnitsInstructionDataEncoder(), getRequestUnitsInstructionDataDecoder());
80
+ }
81
+
82
+ export type RequestUnitsInput = {
83
+ units: RequestUnitsInstructionDataArgs['units'];
84
+ additionalFee: RequestUnitsInstructionDataArgs['additionalFee'];
85
+ };
86
+
87
+ export function getRequestUnitsInstruction<TProgramAddress extends Address = typeof COMPUTE_BUDGET_PROGRAM_ADDRESS>(
88
+ input: RequestUnitsInput,
89
+ config?: { programAddress?: TProgramAddress },
90
+ ): RequestUnitsInstruction<TProgramAddress> {
91
+ // Program address.
92
+ const programAddress = config?.programAddress ?? COMPUTE_BUDGET_PROGRAM_ADDRESS;
93
+
94
+ // Original args.
95
+ const args = { ...input };
96
+
97
+ return Object.freeze({
98
+ data: getRequestUnitsInstructionDataEncoder().encode(args as RequestUnitsInstructionDataArgs),
99
+ programAddress,
100
+ } as RequestUnitsInstruction<TProgramAddress>);
101
+ }
102
+
103
+ export type ParsedRequestUnitsInstruction<TProgram extends string = typeof COMPUTE_BUDGET_PROGRAM_ADDRESS> = {
104
+ programAddress: Address<TProgram>;
105
+ data: RequestUnitsInstructionData;
106
+ };
107
+
108
+ export function parseRequestUnitsInstruction<TProgram extends string>(
109
+ instruction: Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array>,
110
+ ): ParsedRequestUnitsInstruction<TProgram> {
111
+ return {
112
+ programAddress: instruction.programAddress,
113
+ data: getRequestUnitsInstructionDataDecoder().decode(instruction.data),
114
+ };
115
+ }
@@ -0,0 +1,110 @@
1
+ /**
2
+ * This code was AUTOGENERATED using the Codama library.
3
+ * Please DO NOT EDIT THIS FILE, instead use visitors
4
+ * to add features, then rerun Codama to update it.
5
+ *
6
+ * @see https://github.com/codama-idl/codama
7
+ */
8
+
9
+ import {
10
+ combineCodec,
11
+ getStructDecoder,
12
+ getStructEncoder,
13
+ getU32Decoder,
14
+ getU32Encoder,
15
+ getU8Decoder,
16
+ getU8Encoder,
17
+ transformEncoder,
18
+ type AccountMeta,
19
+ type Address,
20
+ type FixedSizeCodec,
21
+ type FixedSizeDecoder,
22
+ type FixedSizeEncoder,
23
+ type Instruction,
24
+ type InstructionWithAccounts,
25
+ type InstructionWithData,
26
+ type ReadonlyUint8Array,
27
+ } from '@solana/kit';
28
+ import { COMPUTE_BUDGET_PROGRAM_ADDRESS } from '../programs';
29
+
30
+ export const SET_COMPUTE_UNIT_LIMIT_DISCRIMINATOR = 2;
31
+
32
+ export function getSetComputeUnitLimitDiscriminatorBytes(): ReadonlyUint8Array {
33
+ return getU8Encoder().encode(SET_COMPUTE_UNIT_LIMIT_DISCRIMINATOR);
34
+ }
35
+
36
+ export type SetComputeUnitLimitInstruction<
37
+ TProgram extends string = typeof COMPUTE_BUDGET_PROGRAM_ADDRESS,
38
+ TRemainingAccounts extends readonly AccountMeta<string>[] = [],
39
+ > = Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array> & InstructionWithAccounts<TRemainingAccounts>;
40
+
41
+ export type SetComputeUnitLimitInstructionData = {
42
+ discriminator: number;
43
+ /** Transaction-wide compute unit limit. */
44
+ units: number;
45
+ };
46
+
47
+ export type SetComputeUnitLimitInstructionDataArgs = {
48
+ /** Transaction-wide compute unit limit. */
49
+ units: number;
50
+ };
51
+
52
+ export function getSetComputeUnitLimitInstructionDataEncoder(): FixedSizeEncoder<SetComputeUnitLimitInstructionDataArgs> {
53
+ return transformEncoder(
54
+ getStructEncoder([
55
+ ['discriminator', getU8Encoder()],
56
+ ['units', getU32Encoder()],
57
+ ]),
58
+ value => ({ ...value, discriminator: SET_COMPUTE_UNIT_LIMIT_DISCRIMINATOR }),
59
+ );
60
+ }
61
+
62
+ export function getSetComputeUnitLimitInstructionDataDecoder(): FixedSizeDecoder<SetComputeUnitLimitInstructionData> {
63
+ return getStructDecoder([
64
+ ['discriminator', getU8Decoder()],
65
+ ['units', getU32Decoder()],
66
+ ]);
67
+ }
68
+
69
+ export function getSetComputeUnitLimitInstructionDataCodec(): FixedSizeCodec<
70
+ SetComputeUnitLimitInstructionDataArgs,
71
+ SetComputeUnitLimitInstructionData
72
+ > {
73
+ return combineCodec(getSetComputeUnitLimitInstructionDataEncoder(), getSetComputeUnitLimitInstructionDataDecoder());
74
+ }
75
+
76
+ export type SetComputeUnitLimitInput = {
77
+ units: SetComputeUnitLimitInstructionDataArgs['units'];
78
+ };
79
+
80
+ export function getSetComputeUnitLimitInstruction<
81
+ TProgramAddress extends Address = typeof COMPUTE_BUDGET_PROGRAM_ADDRESS,
82
+ >(
83
+ input: SetComputeUnitLimitInput,
84
+ config?: { programAddress?: TProgramAddress },
85
+ ): SetComputeUnitLimitInstruction<TProgramAddress> {
86
+ // Program address.
87
+ const programAddress = config?.programAddress ?? COMPUTE_BUDGET_PROGRAM_ADDRESS;
88
+
89
+ // Original args.
90
+ const args = { ...input };
91
+
92
+ return Object.freeze({
93
+ data: getSetComputeUnitLimitInstructionDataEncoder().encode(args as SetComputeUnitLimitInstructionDataArgs),
94
+ programAddress,
95
+ } as SetComputeUnitLimitInstruction<TProgramAddress>);
96
+ }
97
+
98
+ export type ParsedSetComputeUnitLimitInstruction<TProgram extends string = typeof COMPUTE_BUDGET_PROGRAM_ADDRESS> = {
99
+ programAddress: Address<TProgram>;
100
+ data: SetComputeUnitLimitInstructionData;
101
+ };
102
+
103
+ export function parseSetComputeUnitLimitInstruction<TProgram extends string>(
104
+ instruction: Instruction<TProgram> & InstructionWithData<ReadonlyUint8Array>,
105
+ ): ParsedSetComputeUnitLimitInstruction<TProgram> {
106
+ return {
107
+ programAddress: instruction.programAddress,
108
+ data: getSetComputeUnitLimitInstructionDataDecoder().decode(instruction.data),
109
+ };
110
+ }