Calculate Transaction Fees¶
Introduction¶
Transaction fees are the cost of executing operations on Polkadot SDK-based chains. Estimating these fees before you submit a transaction helps you manage account balances and build better user experiences in your applications.
This tutorial explains the fee model that FRAME runtimes use and shows how to estimate a transaction's fee with Subxt, Parity's Rust library for Polkadot SDK-based chains.
Fee Model¶
Runtimes that use the Transaction Payment pallet calculate fees from the following components:
- Base fee: The minimum amount a user pays for a transaction, declared as a base weight in the runtime and converted to a fee.
- Length fee: A fee proportional to the encoded length of the transaction.
- Weight fee: A fee proportional to the amount of weight (computational resources) the transaction consumes.
- Tip: An optional amount that increases the priority of the transaction in the transaction queue.
The pallet combines these components as follows:
inclusion_fee = base_fee + length_fee + [targeted_fee_adjustment * weight_fee]
final_fee = inclusion_fee + tip
The targeted_fee_adjustment multiplier tunes the fee based on network congestion. Each runtime configures how weight maps to fees and how the multiplier is updated, so the same call can cost different amounts on different chains.
The runtime exposes fee estimates through the TransactionPaymentApi runtime API. Client libraries call this API to estimate the fee of a signed transaction before submitting it. For more about runtime APIs, see Runtime API Calls.
Estimate Fees with Subxt¶
Prerequisites¶
- Rust toolchain (latest stable) and Cargo.
- A local development node listening on
ws://127.0.0.1:9944, for example the parachain template node described in Run the Node Locally.
Set Up the Project¶
-
Create a new Rust project:
-
Install the Subxt CLI:
-
Download the metadata from your local node:
-
Update
Cargo.tomlwith the required dependencies:Cargo.toml[package] name = "subxt-fee-estimate-example" version = "0.1.0" edition = "2021" [[bin]] name = "estimate_fee" path = "src/bin/estimate_fee.rs" [dependencies] subxt = { version = "0.50.1", features = ["native"] } subxt-signer = "0.50.1" tokio = { version = "1", features = ["rt", "macros"] }
Estimate the Fee¶
The following example builds a balance transfer from the //Alice development account to //Bob, signs it without submitting it, and asks the node for a fee estimate.
Create a file at src/bin/estimate_fee.rs and add the following code to it:
use subxt::config::DefaultExtrinsicParamsBuilder;
use subxt::utils::AccountId32;
use subxt::{OnlineClient, PolkadotConfig};
use subxt_signer::sr25519::dev;
// Generate an interface from the node's metadata
#[subxt::subxt(runtime_metadata_path = "metadata.scale")]
pub mod runtime {}
// WebSocket endpoint of your local development node
const NODE_URL: &str = "ws://127.0.0.1:9944";
// Amount to transfer, in the chain's smallest unit
const AMOUNT: u128 = 1_000_000_000_000;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize the Subxt client and anchor to the current block
let api = OnlineClient::<PolkadotConfig>::from_url(NODE_URL).await?;
let at_block = api.at_current_block().await?;
// Use the Alice and Bob development accounts
let alice = dev::alice();
let bob = AccountId32::from(dev::bob().public_key());
// Build the balance transfer call
let tx = runtime::transactions()
.balances()
.transfer_keep_alive(bob.into(), AMOUNT);
// Sign the transaction without submitting it, then ask the node for a fee estimate
let params = DefaultExtrinsicParamsBuilder::<PolkadotConfig>::new().build();
let fee = at_block
.transactions()
.create_signed(&tx, &alice, params)
.await?
.partial_fee_estimate()
.await?;
println!("Estimated fee: {} (smallest unit)", fee);
Ok(())
}
Key aspects of the code:
- Transaction creation:
runtime::transactions().balances().transfer_keep_alive()builds the balance transfer call from the generated interface. create_signed: Signs the transaction with the given transaction parameters and returns it without submitting it.partial_fee_estimate: Returns an estimate of what the transaction is expected to cost, excluding any tip, based on the block at which you submit it. The actual amount can vary from block to block.
Run the script:
The script prints the estimated fee in the chain's smallest unit.
Other Tools¶
Fee estimation is also available in third-party client libraries, for example Polkadot API (PAPI) and Polkadot.js API. Interfaces such as Polkadot.js Apps also display the estimated fee before you sign a transaction.
Where to Go Next¶
Now that you can calculate transaction fees, explore related guides to send transactions and manage fees in your applications.
-
Guide Pay Fees with Different Tokens
Learn how to send transactions while paying fees using alternative tokens instead of the native chain token.
-
Guide Send Transactions with SDKs
Learn how to send signed transactions using Subxt.
-
Guide Query Chain Data
Explore different methods for querying blockchain data using REST APIs, SDKs, and runtime API calls.
| Created: November 17, 2026