Add compute unit helpers #15
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR brings the following high-level helpers to the
@solana-program/compute-budgetJS client:const
MAX_COMPUTE_UNIT_LIMIT(1_400_000): The maximum units assignable to a transaction. This can be used as a temporary value when simulating a transaction to estimate its actual CU.const
PROVISORY_COMPUTE_UNIT_LIMIT(0): A temporary value that can be used to signal that the transaction should be simulated to get its CUs before being sent. It is safer to use thanMAX_COMPUTE_UNIT_LIMITbecause the transaction will fail to execute unless it is properly estimated.function
estimateComputeUnitLimitFactory: Brought from@solana/kitand renamed it fromgetComputeUnitEstimateForTransactionMessageFactorysince the function is now part of a narrower namespace (i.e.@solana-program/compute-budget). Aside from being renamed and refactored internally to use some more granular helpers, it works exactly the same. Tests were also brought from@solana/kitand converted from Jest to Vitest.function
estimateAndUpdateProvisoryComputeUnitLimitFactory: A slightly higher-level helper that accepts a function returned byestimateComputeUnitLimitFactoryand return a function that estimates and updates the compute unit limit instruction if and only if it isn't already set to an explicit value (i.e. notPROVISORY_COMPUTE_UNIT_LIMITnorMAX_COMPUTE_UNIT_LIMIT).function
updateOrAppendSetComputeUnitLimitInstruction: Updates the firstSetComputeUnitLimitinstruction in a transaction message with the given units, or appends a new instruction if none exists. A function of the current value can be provided instead of a static value.function
fillProvisorySetComputeUnitLimitInstruction: Appends aSetComputeUnitLimitinstruction with a provisory compute unit limit to a given transaction message if and only if it does not already have one.function
updateOrAppendSetComputeUnitPriceInstruction: Updates the firstSetComputeUnitPriceinstruction in a transaction message with the given micro-Lamports, or appends a new instruction if none exists. A function of the current value can be provided instead of a static value.function
setTransactionMessageComputeUnitPrice: Appends aSetComputeUnitPriceinstruction with the provided micro-Lamports.Note that some of these helpers will be useful when configuring
TransactionPlannersandTransactionPlanExecutorsin the future. At this point, we may consider adding decorators for these types directly in this package.