Skip to main content
Estimates the fee for a Solana transaction without running a full simulation. Returns fee data in lamports, human-readable SOL, and optionally USD — but no asset diffs or security validation. Supports both legacy Transaction and VersionedTransaction objects. Includes automatic retry logic for network reliability. Use this when you need a fee estimate only. Use simulateSolanaTransaction when you also want to preview asset changes and security validation.

Installation

npm install @dynamic-labs-sdk/solana

Usage

import { calculateSolanaTransactionFee } from '@dynamic-labs-sdk/solana';
import { getNetworksData } from '@dynamic-labs-sdk/client';
import {
  Transaction,
  SystemProgram,
  PublicKey,
} from '@solana/web3.js';

const networks = getNetworksData();
const networkData = networks.find((n) => n.networkId === 'mainnet-beta');

const transaction = new Transaction().add(
  SystemProgram.transfer({
    fromPubkey: new PublicKey(walletAccount.address),
    toPubkey: new PublicKey(recipientAddress),
    lamports: 1_000_000_000,
  })
);

if (networkData) {
  const feeData = await calculateSolanaTransactionFee({
    transaction,
    networkData,
  });

  console.log(`Estimated fee: ${feeData.humanReadableAmount} SOL`);
}

Parameters

ParameterTypeDescription
transactionTransaction | VersionedTransactionThe Solana transaction to estimate fees for
networkDataNetworkDataNetwork configuration. Get this from getNetworksData()
nativeTokenPriceUsdnumber (optional)USD price of SOL, used to calculate usdAmount

Returns

Promise<SolanaTransactionFeeData>:
FieldTypeDescription
nativeAmountbigintFee in lamports
humanReadableAmountstringFee in SOL, formatted for display
usdAmountstring (optional)Fee in USD. Present only when nativeTokenPriceUsd is provided

Examples

SOL transfer fee

const feeData = await calculateSolanaTransactionFee({
  transaction,
  networkData,
});

console.log(`Fee: ${feeData.humanReadableAmount} SOL`);
console.log(`Fee in lamports: ${feeData.nativeAmount}`);

With USD conversion

const SOL_PRICE_USD = 180;

const feeData = await calculateSolanaTransactionFee({
  transaction,
  networkData,
  nativeTokenPriceUsd: SOL_PRICE_USD,
});

console.log(`Fee: ${feeData.humanReadableAmount} SOL`);
console.log(`≈ $${feeData.usdAmount}`);

Versioned transaction

import { VersionedTransaction, TransactionMessage, PublicKey } from '@solana/web3.js';

const messageV0 = new TransactionMessage({
  payerKey: new PublicKey(walletAccount.address),
  recentBlockhash: latestBlockhash,
  instructions: [instruction],
}).compileToV0Message();

const versionedTx = new VersionedTransaction(messageV0);

const feeData = await calculateSolanaTransactionFee({
  transaction: versionedTx,
  networkData,
});

Error handling

import { FeeEstimationFailedError } from '@dynamic-labs-sdk/client';

try {
  const feeData = await calculateSolanaTransactionFee({ transaction, networkData });
} catch (error) {
  if (error instanceof FeeEstimationFailedError) {
    console.error('Fee estimation failed:', error.message);
  }
}