For the complete documentation index, see llms.txt. This page is also available as Markdown.

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-core v1.9 and above. For older versions, use getFees.


βœ… 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, or finalized). Defaults to finalized.

    • 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). null if 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_000 to convert to SOL for display purposes.

  • Handling Null Responses A null fee indicates:

    • Invalid or expired blockhash

    • Malformed message

    • Inability to compute at current commitment level

Last updated