@solana-program/compute-budget 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,87 @@
1
+ import { Commitment, CompilableTransactionMessage, ITransactionMessageWithFeePayer, Rpc, SimulateTransactionApi, Slot, TransactionMessage } from '@solana/kit';
2
+ export type EstimateComputeUnitLimitFactoryConfig = Readonly<{
3
+ /** An object that supports the {@link SimulateTransactionApi} of the Solana RPC API */
4
+ rpc: Rpc<SimulateTransactionApi>;
5
+ }>;
6
+ export type EstimateComputeUnitLimitFactoryFunction = (transactionMessage: CompilableTransactionMessage | (TransactionMessage & ITransactionMessageWithFeePayer), config?: EstimateComputeUnitLimitFactoryFunctionConfig) => Promise<number>;
7
+ export type EstimateComputeUnitLimitFactoryFunctionConfig = {
8
+ abortSignal?: AbortSignal;
9
+ /**
10
+ * Compute the estimate as of the highest slot that has reached this level of commitment.
11
+ *
12
+ * @defaultValue Whichever default is applied by the underlying {@link RpcApi} in use. For
13
+ * example, when using an API created by a `createSolanaRpc*()` helper, the default commitment
14
+ * is `"confirmed"` unless configured otherwise. Unmitigated by an API layer on the client, the
15
+ * default commitment applied by the server is `"finalized"`.
16
+ */
17
+ commitment?: Commitment;
18
+ /**
19
+ * Prevents accessing stale data by enforcing that the RPC node has processed transactions up to
20
+ * this slot
21
+ */
22
+ minContextSlot?: Slot;
23
+ };
24
+ type EstimateComputeUnitLimitConfig = EstimateComputeUnitLimitFactoryFunctionConfig & Readonly<{
25
+ rpc: Rpc<SimulateTransactionApi>;
26
+ transactionMessage: CompilableTransactionMessage | (TransactionMessage & ITransactionMessageWithFeePayer);
27
+ }>;
28
+ /**
29
+ * Simulates a transaction message on the network and returns the number of compute units it
30
+ * consumed during simulation.
31
+ *
32
+ * The estimate this function returns can be used to set a compute unit limit on the transaction.
33
+ * Correctly budgeting a compute unit limit for your transaction message can increase the probability
34
+ * that your transaction will be accepted for processing.
35
+ *
36
+ * If you don't declare a compute unit limit on your transaction, validators will assume an upper
37
+ * limit of 200K compute units (CU) per instruction. Since validators have an incentive to pack as
38
+ * many transactions into each block as possible, they may choose to include transactions that they
39
+ * know will fit into the remaining compute budget for the current block over transactions that
40
+ * might not. For this reason, you should set a compute unit limit on each of your transaction
41
+ * messages, whenever possible.
42
+ *
43
+ * ## Example
44
+ *
45
+ * ```ts
46
+ * import { getSetComputeLimitInstruction } from '@solana-program/compute-budget';
47
+ * import { createSolanaRpc, getComputeUnitEstimateForTransactionMessageFactory, pipe } from '@solana/kit';
48
+ *
49
+ * // Create an estimator function.
50
+ * const rpc = createSolanaRpc('http://127.0.0.1:8899');
51
+ * const getComputeUnitEstimateForTransactionMessage =
52
+ * getComputeUnitEstimateForTransactionMessageFactory({ rpc });
53
+ *
54
+ * // Create your transaction message.
55
+ * const transactionMessage = pipe(
56
+ * createTransactionMessage({ version: 'legacy' }),
57
+ * /* ... *\/
58
+ * );
59
+ *
60
+ * // Request an estimate of the actual compute units this message will consume.
61
+ * const computeUnitsEstimate =
62
+ * await getComputeUnitEstimateForTransactionMessage(transactionMessage);
63
+ *
64
+ * // Set the transaction message's compute unit budget.
65
+ * const transactionMessageWithComputeUnitLimit = prependTransactionMessageInstruction(
66
+ * getSetComputeLimitInstruction({ units: computeUnitsEstimate }),
67
+ * transactionMessage,
68
+ * );
69
+ * ```
70
+ *
71
+ * > [!WARNING]
72
+ * > The compute unit estimate is just that &ndash; an estimate. The compute unit consumption of the
73
+ * > actual transaction might be higher or lower than what was observed in simulation. Unless you
74
+ * > are confident that your particular transaction message will consume the same or fewer compute
75
+ * > units as was estimated, you might like to augment the estimate by either a fixed number of CUs
76
+ * > or a multiplier.
77
+ *
78
+ * > [!NOTE]
79
+ * > If you are preparing an _unsigned_ transaction, destined to be signed and submitted to the
80
+ * > network by a wallet, you might like to leave it up to the wallet to determine the compute unit
81
+ * > limit. Consider that the wallet might have a more global view of how many compute units certain
82
+ * > types of transactions consume, and might be able to make better estimates of an appropriate
83
+ * > compute unit budget.
84
+ */
85
+ export declare function estimateComputeUnitLimit({ transactionMessage, ...configs }: EstimateComputeUnitLimitConfig): Promise<number>;
86
+ export {};
87
+ //# sourceMappingURL=estimateComputeLimitInternal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"estimateComputeLimitInternal.d.ts","sourceRoot":"","sources":["../../src/estimateComputeLimitInternal.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,4BAA4B,EAK5B,+BAA+B,EAE/B,GAAG,EACH,sBAAsB,EACtB,IAAI,EAKJ,kBAAkB,EACnB,MAAM,aAAa,CAAC;AAKrB,MAAM,MAAM,qCAAqC,GAAG,QAAQ,CAAC;IAC3D,uFAAuF;IACvF,GAAG,EAAE,GAAG,CAAC,sBAAsB,CAAC,CAAC;CAClC,CAAC,CAAC;AAEH,MAAM,MAAM,uCAAuC,GAAG,CACpD,kBAAkB,EACd,4BAA4B,GAC5B,CAAC,kBAAkB,GAAG,+BAA+B,CAAC,EAC1D,MAAM,CAAC,EAAE,6CAA6C,KACnD,OAAO,CAAC,MAAM,CAAC,CAAC;AAErB,MAAM,MAAM,6CAA6C,GAAG;IAC1D,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B;;;;;;;OAOG;IACH,UAAU,CAAC,EAAE,UAAU,CAAC;IACxB;;;OAGG;IACH,cAAc,CAAC,EAAE,IAAI,CAAC;CACvB,CAAC;AAEF,KAAK,8BAA8B,GACjC,6CAA6C,GAC3C,QAAQ,CAAC;IACP,GAAG,EAAE,GAAG,CAAC,sBAAsB,CAAC,CAAC;IACjC,kBAAkB,EACd,4BAA4B,GAC5B,CAAC,kBAAkB,GAAG,+BAA+B,CAAC,CAAC;CAC5D,CAAC,CAAC;AAEP;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,wBAAsB,wBAAwB,CAAC,EAC7C,kBAAkB,EAClB,GAAG,OAAO,EACX,EAAE,8BAA8B,GAAG,OAAO,CAAC,MAAM,CAAC,CAelD"}
@@ -1,2 +1,7 @@
1
1
  export * from './generated';
2
+ export * from './constants';
3
+ export * from './estimateAndSetComputeLimit';
4
+ export * from './estimateComputeLimit';
5
+ export * from './setComputeLimit';
6
+ export * from './setComputePrice';
2
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAE5B,cAAc,aAAa,CAAC;AAC5B,cAAc,8BAA8B,CAAC;AAC7C,cAAc,wBAAwB,CAAC;AACvC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC"}
@@ -0,0 +1,35 @@
1
+ import { BaseTransactionMessage, IInstruction, MicroLamports } from '@solana/kit';
2
+ import { SetComputeUnitLimitInstruction, SetComputeUnitPriceInstruction } from './generated';
3
+ /**
4
+ * Finds the index of the first `SetComputeUnitLimit` instruction in a transaction message
5
+ * and its set limit, if any.
6
+ */
7
+ export declare function getSetComputeUnitLimitInstructionIndexAndUnits(transactionMessage: BaseTransactionMessage): {
8
+ index: number;
9
+ units: number;
10
+ } | null;
11
+ /**
12
+ * Finds the index of the first `SetComputeUnitLimit` instruction in a transaction message, if any.
13
+ */
14
+ export declare function getSetComputeUnitLimitInstructionIndex(transactionMessage: BaseTransactionMessage): number;
15
+ /**
16
+ * Checks if the given instruction is a `SetComputeUnitLimit` instruction.
17
+ */
18
+ export declare function isSetComputeUnitLimitInstruction(instruction: IInstruction): instruction is SetComputeUnitLimitInstruction;
19
+ /**
20
+ * Finds the index of the first `SetComputeUnitPrice` instruction in a transaction message
21
+ * and its set micro-lamports, if any.
22
+ */
23
+ export declare function getSetComputeUnitPriceInstructionIndexAndMicroLamports(transactionMessage: BaseTransactionMessage): {
24
+ index: number;
25
+ microLamports: MicroLamports;
26
+ } | null;
27
+ /**
28
+ * Finds the index of the first `SetComputeUnitPrice` instruction in a transaction message, if any.
29
+ */
30
+ export declare function getSetComputeUnitPriceInstructionIndex(transactionMessage: BaseTransactionMessage): number;
31
+ /**
32
+ * Checks if the given instruction is a `SetComputeUnitPrice` instruction.
33
+ */
34
+ export declare function isSetComputeUnitPriceInstruction(instruction: IInstruction): instruction is SetComputeUnitPriceInstruction;
35
+ //# sourceMappingURL=internal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internal.d.ts","sourceRoot":"","sources":["../../src/internal.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EAGtB,YAAY,EACZ,aAAa,EAEd,MAAM,aAAa,CAAC;AACrB,OAAO,EAIL,8BAA8B,EAC9B,8BAA8B,EAC/B,MAAM,aAAa,CAAC;AAErB;;;GAGG;AACH,wBAAgB,8CAA8C,CAC5D,kBAAkB,EAAE,sBAAsB,GACzC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAYzC;AAED;;GAEG;AACH,wBAAgB,sCAAsC,CACpD,kBAAkB,EAAE,sBAAsB,UAK3C;AAED;;GAEG;AACH,wBAAgB,gCAAgC,CAC9C,WAAW,EAAE,YAAY,GACxB,WAAW,IAAI,8BAA8B,CAM/C;AAED;;;GAGG;AACH,wBAAgB,sDAAsD,CACpE,kBAAkB,EAAE,sBAAsB,GACzC;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,aAAa,EAAE,aAAa,CAAA;CAAE,GAAG,IAAI,CAYxD;AAED;;GAEG;AACH,wBAAgB,sCAAsC,CACpD,kBAAkB,EAAE,sBAAsB,UAK3C;AAED;;GAEG;AACH,wBAAgB,gCAAgC,CAC9C,WAAW,EAAE,YAAY,GACxB,WAAW,IAAI,8BAA8B,CAM/C"}
@@ -0,0 +1,20 @@
1
+ import { BaseTransactionMessage, TransactionMessageWithBlockhashLifetime, TransactionMessageWithDurableNonceLifetime } from '@solana/kit';
2
+ /**
3
+ * An invalid blockhash lifetime constraint used as a placeholder for
4
+ * transaction messages that are not yet ready to be compiled.
5
+ *
6
+ * This enables various operations on the transaction message, such as
7
+ * simulating it or calculating its transaction size, whilst defering
8
+ * the actual blockhash to a later stage.
9
+ */
10
+ export declare const PROVISORY_BLOCKHASH_LIFETIME_CONSTRAINT: TransactionMessageWithBlockhashLifetime['lifetimeConstraint'];
11
+ /**
12
+ * Sets a provisory blockhash lifetime constraint on the transaction message
13
+ * if and only if it doesn't already have a lifetime constraint.
14
+ */
15
+ export declare function fillMissingTransactionMessageLifetimeUsingProvisoryBlockhash<TTransactionMessage extends BaseTransactionMessage>(transactionMessage: TTransactionMessage): TTransactionMessage & (TransactionMessageWithBlockhashLifetime | TransactionMessageWithDurableNonceLifetime);
16
+ /**
17
+ * Sets a provisory blockhash lifetime constraint on the transaction message.
18
+ */
19
+ export declare function setTransactionMessageLifetimeUsingProvisoryBlockhash<TTransactionMessage extends BaseTransactionMessage>(transactionMessage: TTransactionMessage): TTransactionMessage & TransactionMessageWithBlockhashLifetime;
20
+ //# sourceMappingURL=internalMoveToKit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"internalMoveToKit.d.ts","sourceRoot":"","sources":["../../src/internalMoveToKit.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,sBAAsB,EAGtB,uCAAuC,EACvC,0CAA0C,EAC3C,MAAM,aAAa,CAAC;AAErB;;;;;;;GAOG;AACH,eAAO,MAAM,uCAAuC,EAAE,uCAAuC,CAAC,oBAAoB,CAI/G,CAAC;AAEJ;;;GAGG;AACH,wBAAgB,4DAA4D,CAC1E,mBAAmB,SAAS,sBAAsB,EAElD,kBAAkB,EAAE,mBAAmB,GACtC,mBAAmB,GACpB,CACI,uCAAuC,GACvC,0CAA0C,CAC7C,CAcF;AAED;;GAEG;AACH,wBAAgB,oDAAoD,CAClE,mBAAmB,SAAS,sBAAsB,EAElD,kBAAkB,EAAE,mBAAmB,GACtC,mBAAmB,GAAG,uCAAuC,CAK/D"}
@@ -0,0 +1,36 @@
1
+ import { BaseTransactionMessage } from '@solana/kit';
2
+ /**
3
+ * Appends a `SetComputeUnitLimit` instruction with a provisory
4
+ * compute unit limit to a given transaction message
5
+ * if and only if it does not already have one.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const transactionMessage = pipe(
10
+ * createTransactionMessage({ version: 0 }),
11
+ * fillProvisorySetComputeUnitLimitInstruction,
12
+ * // ...
13
+ * );
14
+ * ```
15
+ */
16
+ export declare function fillProvisorySetComputeUnitLimitInstruction<TTransactionMessage extends BaseTransactionMessage>(transactionMessage: TTransactionMessage): TTransactionMessage;
17
+ /**
18
+ * Updates the first `SetComputeUnitLimit` instruction in a transaction message
19
+ * with the given units, or appends a new instruction if none exists.
20
+ * A function of the current value can be provided instead of a static value.
21
+ *
22
+ * @param units - The new compute unit limit, or a function that takes the previous
23
+ * compute unit limit and returns the new limit.
24
+ * @param transactionMessage - The transaction message to update.
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * const updatedTransactionMessage = updateOrAppendSetComputeUnitLimitInstruction(
29
+ * // E.g. Keep the current limit if it is set, otherwise set it to the maximum.
30
+ * (currentUnits) => currentUnits === null ? MAX_COMPUTE_UNIT_LIMIT : currentUnits,
31
+ * transactionMessage,
32
+ * );
33
+ * ```
34
+ */
35
+ export declare function updateOrAppendSetComputeUnitLimitInstruction<TTransactionMessage extends BaseTransactionMessage>(units: number | ((previousUnits: number | null) => number), transactionMessage: TTransactionMessage): TTransactionMessage;
36
+ //# sourceMappingURL=setComputeLimit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setComputeLimit.d.ts","sourceRoot":"","sources":["../../src/setComputeLimit.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,sBAAsB,EACvB,MAAM,aAAa,CAAC;AAKrB;;;;;;;;;;;;;GAaG;AACH,wBAAgB,2CAA2C,CACzD,mBAAmB,SAAS,sBAAsB,EAClD,kBAAkB,EAAE,mBAAmB,uBAMxC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,4CAA4C,CAC1D,mBAAmB,SAAS,sBAAsB,EAElD,KAAK,EAAE,MAAM,GAAG,CAAC,CAAC,aAAa,EAAE,MAAM,GAAG,IAAI,KAAK,MAAM,CAAC,EAC1D,kBAAkB,EAAE,mBAAmB,GACtC,mBAAmB,CA0BrB"}
@@ -0,0 +1,34 @@
1
+ import { BaseTransactionMessage, MicroLamports } from '@solana/kit';
2
+ /**
3
+ * Sets the compute unit price of a transaction message in micro-Lamports.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * const transactionMessage = pipe(
8
+ * createTransactionMessage({ version: 0 }),
9
+ * (m) => setTransactionMessageComputeUnitPrice(10_000, m),
10
+ * // ...
11
+ * );
12
+ * ```
13
+ */
14
+ export declare function setTransactionMessageComputeUnitPrice<TTransactionMessage extends BaseTransactionMessage>(microLamports: number | bigint, transactionMessage: TTransactionMessage): TTransactionMessage;
15
+ /**
16
+ * Updates the first `SetComputeUnitPrice` instruction in a transaction message
17
+ * with the given micro-Lamports, or appends a new instruction if none exists.
18
+ * A function of the current value can be provided instead of a static value.
19
+ *
20
+ * @param microLamports - The new compute unit price, or a function that
21
+ * takes the previous price and returns the new one.
22
+ * @param transactionMessage - The transaction message to update.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const updatedTransactionMessage = updateOrAppendSetComputeUnitPriceInstruction(
27
+ * // E.g. double the current price or set it to 10_000 if it isn't set.
28
+ * (currentPrice) => currentPrice === null ? 10_000 : currentPrice * 2,
29
+ * transactionMessage,
30
+ * );
31
+ * ```
32
+ */
33
+ export declare function updateOrAppendSetComputeUnitPriceInstruction<TTransactionMessage extends BaseTransactionMessage>(microLamports: MicroLamports | ((previousMicroLamports: MicroLamports | null) => MicroLamports), transactionMessage: TTransactionMessage): TTransactionMessage;
34
+ //# sourceMappingURL=setComputePrice.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setComputePrice.d.ts","sourceRoot":"","sources":["../../src/setComputePrice.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,sBAAsB,EACtB,aAAa,EACd,MAAM,aAAa,CAAC;AAIrB;;;;;;;;;;;GAWG;AACH,wBAAgB,qCAAqC,CACnD,mBAAmB,SAAS,sBAAsB,EAClD,aAAa,EAAE,MAAM,GAAG,MAAM,EAAE,kBAAkB,EAAE,mBAAmB,uBAKxE;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,4CAA4C,CAC1D,mBAAmB,SAAS,sBAAsB,EAElD,aAAa,EACT,aAAa,GACb,CAAC,CAAC,qBAAqB,EAAE,aAAa,GAAG,IAAI,KAAK,aAAa,CAAC,EACpE,kBAAkB,EAAE,mBAAmB,GACtC,mBAAmB,CAkCrB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana-program/compute-budget",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "JavaScript client for the Compute Budget program",
5
5
  "sideEffects": false,
6
6
  "module": "./dist/src/index.mjs",
@@ -35,35 +35,24 @@
35
35
  "@solana/kit": "^2.1.0"
36
36
  },
37
37
  "devDependencies": {
38
- "@ava/typescript": "^4.1.0",
39
38
  "@solana/eslint-config-solana": "^3.0.3",
40
39
  "@solana/kit": "^2.1.0",
41
40
  "@types/node": "^20",
42
41
  "@typescript-eslint/eslint-plugin": "^7.16.1",
43
42
  "@typescript-eslint/parser": "^7.16.1",
44
- "ava": "^6.1.3",
45
43
  "eslint": "^8.57.0",
46
44
  "prettier": "^3.3.3",
47
45
  "rimraf": "^5.0.5",
48
46
  "tsup": "^8.1.2",
49
47
  "typedoc": "^0.25.12",
50
- "typescript": "^5.5.3"
51
- },
52
- "ava": {
53
- "nodeArguments": [
54
- "--no-warnings"
55
- ],
56
- "typescript": {
57
- "compile": false,
58
- "rewritePaths": {
59
- "test/": "dist/test/"
60
- }
61
- }
48
+ "typescript": "^5.5.3",
49
+ "vitest": "^3.1.3"
62
50
  },
63
51
  "scripts": {
64
52
  "build": "rimraf dist && tsup && tsc -p ./tsconfig.declarations.json",
65
53
  "build:docs": "typedoc",
66
- "test": "ava",
54
+ "dev": "vitest",
55
+ "test": "vitest run",
67
56
  "lint": "eslint --ext js,ts,tsx src",
68
57
  "lint:fix": "eslint --fix --ext js,ts,tsx src",
69
58
  "format": "prettier --check src test",