GetFeeForMessage
Returns the fee that the network would charge for a given serialized message. This is the gRPC equivalent of the JSON-RPC getFeeForMessage method, providing fast fee estimation through the Yellowstone Geyser plugin.
Pass a serialized transaction message (without signatures) to determine the exact lamport cost before signing and submitting. This is particularly useful for MEV bots and trading systems that need to factor fees into profitability calculations in real time.
Type: Unary (request/response)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
message | bytes | Yes | The serialized transaction message (base-64 encoded) to estimate fees for |
commitment | CommitmentLevel | No | Commitment level: Processed (0), Confirmed (1), or Finalized (2). Default: Finalized |
Response
| Field | Type | Description |
|---|---|---|
value | uint64 (nullable) | The fee in lamports the network would charge, or null if the message is invalid |
Code Examples
Node.js
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const { Transaction, SystemProgram, PublicKey } = require('@solana/web3.js');
const packageDef = protoLoader.loadSync('geyser.proto', {
keepCase: true, longs: String, enums: String, defaults: true, oneofs: true
});
const proto = grpc.loadPackageDefinition(packageDef).geyser;
const client = new proto.Geyser('88.216.36.108:10001', grpc.credentials.createInsecure());
// Build a sample transfer message
const tx = new Transaction().add(
SystemProgram.transfer({
fromPubkey: new PublicKey('YourWalletPubkey111111111111111111111111111'),
toPubkey: new PublicKey('RecipientPubkey1111111111111111111111111111'),
lamports: 1_000_000,
})
);
const messageBytes = tx.compileMessage().serialize();
client.getFeeForMessage({ message: messageBytes, commitment: 1 }, (err, response) => {
if (err) { console.error('gRPC error:', err); return; }
if (response.value !== null) {
console.log('Fee:', response.value, 'lamports');
} else {
console.log('Invalid message -- could not calculate fee');
}
});Rust
use yellowstone_grpc_client::GeyserGrpcClient;
use yellowstone_grpc_proto::prelude::*;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mut client = GeyserGrpcClient::build_from_uri("http://88.216.36.108:10001")
.connect()
.await?;
// Serialize your transaction message before calling
let serialized_message: Vec<u8> = vec![/* your serialized message bytes */];
let response = client
.get_fee_for_message(serialized_message, Some(CommitmentLevel::Confirmed as i32))
.await?;
let result = response.into_inner();
match result.value {
Some(fee) => println!("Transaction fee: {} lamports", fee),
None => println!("Could not calculate fee for this message"),
}
Ok(())
}Example Response
{
"value": "5000"
}