import { address, appendTransactionMessageInstruction, compileTransaction, createNoopSigner, createSolanaRpc, createTransactionMessage, getBase64Decoder, lamports, pipe, setTransactionMessageFeePayerSigner, setTransactionMessageLifetimeUsingBlockhash, type TransactionMessageBytesBase64} from "@solana/kit";import { getTransferSolInstruction } from "@solana-program/system";const rpc_url = "https://api.devnet.solana.com";const rpc = createSolanaRpc(rpc_url);// The fee depends only on the message, so no private key is neededconst sender = createNoopSigner( address("4kg8oh3jdNtn7j2wcS7TrUua31AgbLzDVkBZgTAe44aF"));const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();const transactionMessage = pipe( createTransactionMessage({ version: 0 }), (m) => setTransactionMessageFeePayerSigner(sender, m), (m) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, m), (m) => appendTransactionMessageInstruction( getTransferSolInstruction({ source: sender, destination: address("devFqxUpdo1sYETcWTXT12JE8V53MG2hRsXNMx2bYhA"), amount: lamports(1_000_000n) }), m ));// Compile the message and base64-encode its bytesconst message = getBase64Decoder().decode( compileTransaction(transactionMessage).messageBytes) as TransactionMessageBytesBase64;let fee = await rpc.getFeeForMessage(message).send();console.log(fee);
import { Connection, PublicKey, SystemProgram, TransactionMessage, clusterApiUrl} from "@solana/web3.js";const connection = new Connection(clusterApiUrl("devnet"), "confirmed");// The fee depends only on the message, so no private key is neededconst sender = new PublicKey("4kg8oh3jdNtn7j2wcS7TrUua31AgbLzDVkBZgTAe44aF");const { blockhash } = await connection.getLatestBlockhash();let message = new TransactionMessage({ payerKey: sender, recentBlockhash: blockhash, instructions: [ SystemProgram.transfer({ fromPubkey: sender, toPubkey: new PublicKey("devFqxUpdo1sYETcWTXT12JE8V53MG2hRsXNMx2bYhA"), lamports: 1_000_000 }) ]}).compileToV0Message();let fee = await connection.getFeeForMessage(message);console.log(fee);
use anyhow::Result;use solana_client::nonblocking::rpc_client::RpcClient;use solana_commitment_config::CommitmentConfig;use solana_sdk::{message::Message, pubkey};use solana_system_interface::instruction::transfer;#[tokio::main]async fn main() -> Result<()> { let client = RpcClient::new_with_commitment( String::from("https://api.devnet.solana.com"), CommitmentConfig::confirmed(), ); // The fee depends only on the message, so no private key is needed let sender = pubkey!("4kg8oh3jdNtn7j2wcS7TrUua31AgbLzDVkBZgTAe44aF"); let recipient = pubkey!("devFqxUpdo1sYETcWTXT12JE8V53MG2hRsXNMx2bYhA"); let blockhash = client.get_latest_blockhash().await?; let message = Message::new_with_blockhash( &[transfer(&sender, &recipient, 1_000_000)], Some(&sender), &blockhash, ); let fee = client.get_fee_for_message(&message).await?; println!("{:#?}", fee); Ok(())}
import { address, appendTransactionMessageInstruction, compileTransaction, createNoopSigner, createSolanaRpc, createTransactionMessage, getBase64Decoder, lamports, pipe, setTransactionMessageFeePayerSigner, setTransactionMessageLifetimeUsingBlockhash, type TransactionMessageBytesBase64} from "@solana/kit";import { getTransferSolInstruction } from "@solana-program/system";const rpc_url = "https://api.devnet.solana.com";const rpc = createSolanaRpc(rpc_url);// The fee depends only on the message, so no private key is neededconst sender = createNoopSigner( address("4kg8oh3jdNtn7j2wcS7TrUua31AgbLzDVkBZgTAe44aF"));const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();const transactionMessage = pipe( createTransactionMessage({ version: 0 }), (m) => setTransactionMessageFeePayerSigner(sender, m), (m) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, m), (m) => appendTransactionMessageInstruction( getTransferSolInstruction({ source: sender, destination: address("devFqxUpdo1sYETcWTXT12JE8V53MG2hRsXNMx2bYhA"), amount: lamports(1_000_000n) }), m ));// Compile the message and base64-encode its bytesconst message = getBase64Decoder().decode( compileTransaction(transactionMessage).messageBytes) as TransactionMessageBytesBase64;let fee = await rpc.getFeeForMessage(message).send();console.log(fee);
import { Connection, PublicKey, SystemProgram, TransactionMessage, clusterApiUrl} from "@solana/web3.js";const connection = new Connection(clusterApiUrl("devnet"), "confirmed");// The fee depends only on the message, so no private key is neededconst sender = new PublicKey("4kg8oh3jdNtn7j2wcS7TrUua31AgbLzDVkBZgTAe44aF");const { blockhash } = await connection.getLatestBlockhash();let message = new TransactionMessage({ payerKey: sender, recentBlockhash: blockhash, instructions: [ SystemProgram.transfer({ fromPubkey: sender, toPubkey: new PublicKey("devFqxUpdo1sYETcWTXT12JE8V53MG2hRsXNMx2bYhA"), lamports: 1_000_000 }) ]}).compileToV0Message();let fee = await connection.getFeeForMessage(message);console.log(fee);
use anyhow::Result;use solana_client::nonblocking::rpc_client::RpcClient;use solana_commitment_config::CommitmentConfig;use solana_sdk::{message::Message, pubkey};use solana_system_interface::instruction::transfer;#[tokio::main]async fn main() -> Result<()> { let client = RpcClient::new_with_commitment( String::from("https://api.devnet.solana.com"), CommitmentConfig::confirmed(), ); // The fee depends only on the message, so no private key is needed let sender = pubkey!("4kg8oh3jdNtn7j2wcS7TrUua31AgbLzDVkBZgTAe44aF"); let recipient = pubkey!("devFqxUpdo1sYETcWTXT12JE8V53MG2hRsXNMx2bYhA"); let blockhash = client.get_latest_blockhash().await?; let message = Message::new_with_blockhash( &[transfer(&sender, &recipient, 1_000_000)], Some(&sender), &blockhash, ); let fee = client.get_fee_for_message(&message).await?; println!("{:#?}", fee); Ok(())}
The node returns a fee only while the message’s blockhash is valid. The Kit,
web3.js, and Rust examples compile a transfer message with a recent blockhash
from getLatestBlockhash. The
cURL message carries a placeholder blockhash (all zeros), so the node returns
"value": null.
Values: processed, confirmed, finalizedSolana RPC uses the following commitment levels:
Value
Description
processed
Return data from the highest slot this node has processed. This is the newest view, but the block can still be skipped.
confirmed
Return data from the same slot as finalized. Kept for compatibility and planned for removal in a later release.
finalized
Return data from the highest slot with a finalization certificate, from votes by 80% of stake in one round or 60% of stake in each of two rounds. This is the strongest commitment level.