How to Add Priority Fees to a Transaction
Transaction priority fees are achieved by adding a "compute unit price" instruction to the transaction. This raises the transaction's prioritization in the scheduler and the total transaction fee above the base fee of 5,000 lamports per signature.
By default, the priority fee is the product of 200,000 Compute Units (CU) * number of instructions, with a max of 1.4M CU. This default value is used if a "compute unit limit" instruction is NOT added to the transaction.
You can find a detailed guide here on how to use priority fees.
The total priority fee for a single transaction can be changed by adding the following Compute Budget program instructions into your transaction:
-
compute unit limit - The value provided will replace the default value (200k CU per instruction). Transactions should request the minimum amount of CU required for execution to maximize throughput and minimize fees.
-
compute unit price - this will add the priority fee, above the base fee (5,000 lamports per signature). The value provided, in micro-lamports, will be multiplied by the "compute unit limit" to determine the total priority fee in lamports. For example, if your compute unit limit is
1M CU
, and you add1 microLamport/CU
, the total priority fee will be 1 lamport (1M * 0.000001). The total transaction fee will then be 5001 lamports.
import {getExplorerLink,createTransaction,createSolanaClient,getSignatureFromTransaction,signTransactionMessageWithSigners,} from "gill";import { loadKeypairSignerFromFile } from "gill/node";import {getAddMemoInstruction,getSetComputeUnitPriceInstruction,} from "gill/programs";const { rpc, sendAndConfirmTransaction } = createSolanaClient({urlOrMoniker: "devnet", // or `mainnet`, `localnet`, etc});const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();// load a Signer from the default Solana CLI keypair fileconst signer = await loadKeypairSignerFromFile();// provide the `computeUnitPrice` value to set a priority feeconst transaction = createTransaction({version: "legacy",feePayer: signer,instructions: [getAddMemoInstruction({ memo: "Memo message to send in this transaction" }),],latestBlockhash,computeUnitPrice: 10_000, // set compute unit price of 10k micro-lamports per CU});// or you can manually add the compute unit price instruction to set a priority feeconst transaction2 = createTransaction({version: "legacy",feePayer: signer,instructions: [getAddMemoInstruction({ memo: "Memo message to send in this transaction" }),getSetComputeUnitPriceInstruction({ microLamports: 10_000 }), // set compute unit price of 10k micro-lamports per CU],latestBlockhash,});const signedTransaction = await signTransactionMessageWithSigners(transaction);console.log("Sending transaction:",getExplorerLink({cluster: "devnet",transaction: getSignatureFromTransaction(signedTransaction),}),);await sendAndConfirmTransaction(signedTransaction);
Is this page helpful?