getFeeForMessage
Learn getFeeForMessage use cases, code examples, request parameters, response structure, and tips.
The getFeeForMessage RPC method estimates the base transaction fee (in lamports) required to process a given compiled transaction message. This is especially helpful for displaying cost estimates to users or optimizing fee strategies before submitting transactions.
Version Note: This method is available in
solana-corev1.9 and above. For older versions, usegetFees.
β
Common Use Cases
Estimate Transaction Fees Calculate the expected fee for a transaction before sending it to the network.
Optimize Transaction Costs Compare fees across different transaction structures or times to identify cost-efficient strategies.
Improve User Experience Show users a real-time fee estimate in your UI before they sign and submit a transaction.
π Request Parameters
getFeeForMessage takes the following parameters:
message(string, required): A base64-encoded serialized Solana transaction message. Must be a valid compiled message containing instructions, fee payer, and recent blockhash.config(object, optional): Configuration object with:commitment(string): Commitment level (processed,confirmed, orfinalized). Defaults tofinalized.minContextSlot(number): The minimum slot at which the request is evaluated (for consistency in long-running processes).
π¦ Response Structure
The result field contains:
context.slotβ The slot used when evaluating the fee.valueβ Estimated fee in lamports (1 SOL = 1,000,000,000 lamports).nullif the fee could not be calculated (e.g., expired blockhash or invalid message).
π‘ Example: Estimate Fee for a Simple Transfer
First, compile a transfer transaction and extract its message in base64 format, then call:
Code Examples
Example Response
π§ Developer Tips
Message Construction Ensure your message is well-formed with:
Valid fee payer
Correct instructions
A recent blockhash (within ~2 minutes)
Expired Blockhash If the blockhash is too old, the fee may return as
null. Always refresh the blockhash before compiling the message.Base Fee Only This method returns only the base network fee. To estimate priority fees, use
getRecentPrioritizationFees.Lamports, Not SOL The value is returned in lamports. Divide by
1_000_000_000to convert to SOL for display purposes.Handling Null Responses A
nullfee indicates:Invalid or expired blockhash
Malformed message
Inability to compute at current commitment level
Last updated