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

ParameterTypeRequiredDescription
messagebytesYesThe serialized transaction message (base-64 encoded) to estimate fees for
commitmentCommitmentLevelNoCommitment level: Processed (0), Confirmed (1), or Finalized (2). Default: Finalized

Response

FieldTypeDescription
valueuint64 (nullable)The fee in lamports the network would charge, or null if the message is invalid

Code Examples

Node.js

1const grpc = require('@grpc/grpc-js');
2const protoLoader = require('@grpc/proto-loader');
3const { Transaction, SystemProgram, PublicKey } = require('@solana/web3.js');
4
5const packageDef = protoLoader.loadSync('geyser.proto', {
6  keepCase: true, longs: String, enums: String, defaults: true, oneofs: true
7});
8const proto = grpc.loadPackageDefinition(packageDef).geyser;
9const client = new proto.Geyser('[IP_ADDRESS]:[PORT]', grpc.credentials.createInsecure());
10
11// Build a sample transfer message
12const tx = new Transaction().add(
13  SystemProgram.transfer({
14    fromPubkey: new PublicKey('YourWalletPubkey111111111111111111111111111'),
15    toPubkey: new PublicKey('RecipientPubkey1111111111111111111111111111'),
16    lamports: 1_000_000,
17  })
18);
19const messageBytes = tx.compileMessage().serialize();
20
21client.getFeeForMessage({ message: messageBytes, commitment: 1 }, (err, response) => {
22  if (err) { console.error('gRPC error:', err); return; }
23  if (response.value !== null) {
24    console.log('Fee:', response.value, 'lamports');
25  } else {
26    console.log('Invalid message -- could not calculate fee');
27  }
28});

Rust

1use yellowstone_grpc_client::GeyserGrpcClient;
2use yellowstone_grpc_proto::prelude::*;
3
4#[tokio::main]
5async fn main() -> anyhow::Result<()> {
6    let mut client = GeyserGrpcClient::build_from_uri("http://[IP_ADDRESS]:[PORT]")
7        .connect()
8        .await?;
9
10    // Serialize your transaction message before calling
11    let serialized_message: Vec<u8> = vec![/* your serialized message bytes */];
12
13    let response = client
14        .get_fee_for_message(serialized_message, Some(CommitmentLevel::Confirmed as i32))
15        .await?;
16    let result = response.into_inner();
17
18    match result.value {
19        Some(fee) => println!("Transaction fee: {} lamports", fee),
20        None => println!("Could not calculate fee for this message"),
21    }
22
23    Ok(())
24}

Example Response

1{
2  "value": "5000"
3}