Transactions & Fees

Understanding Transaction Types, Fees, and Gas Management

Understanding Polkadot Transactions

Transactions in Polkadot are called "extrinsics" and represent any data that comes from outside the chain. This includes transfers, smart contract calls, and governance actions. Understanding how transactions work is crucial for building applications on Polkadot.

Types of Extrinsics

Polkadot supports several types of extrinsics, each serving different purposes:

Inherent Extrinsics

These are extrinsics that are not signed and are added to blocks by validators. They represent information that is already known to be true.

  • • Timestamp extrinsics
  • • Randomness extrinsics
  • • Consensus-related extrinsics

Signed Extrinsics

These are extrinsics that are signed by an account and represent actions initiated by users.

  • • Balance transfers
  • • Smart contract calls
  • • Governance votes
  • • Staking operations

Unsigned Extrinsics

These are extrinsics that are not signed but are validated by the runtime logic.

  • • Heartbeat extrinsics from validators
  • • Off-chain worker extrinsics

Transaction Structure

Every extrinsic in Polkadot follows a specific structure:

// Extrinsic structure
{
  // Extrinsic version and signature
  version: 4,
  signature: {
    signer: "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY",
    signature: "0x...",
    era: "MortalEra",
    nonce: 0,
    tip: 0
  },
  // The actual call data
  method: {
    pallet: "Balances",
    method: "transfer",
    args: {
      dest: "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty",
      value: "1000000000000"
    }
  }
}

Fee Calculation

Polkadot uses a sophisticated fee calculation system that considers multiple factors:

Fee Components:

  • Base Fee: Fixed fee for including any extrinsic in a block
  • Length Fee: Fee proportional to the size of the extrinsic
  • Weight Fee: Fee based on the computational complexity
  • Tip: Optional additional fee to prioritize the transaction

Weight and Gas

Unlike Ethereum's gas system, Polkadot uses a "weight" system to measure computational cost:

Weight System

  • • Measured in units of time
  • • Pre-calculated for each operation
  • • More predictable than gas
  • • Allows for better fee estimation

Benefits

  • • No gas limit surprises
  • • Consistent fee calculation
  • • Better user experience
  • • Predictable transaction costs

Transaction Lifecycle

Understanding the transaction lifecycle helps in building robust applications:

// Transaction lifecycle example
import { ApiPromise, WsProvider } from '@polkadot/api';

const api = await ApiPromise.create({ provider: new WsProvider('wss://rpc.polkadot.io') });

// 1. Create transaction
const transfer = api.tx.balances.transfer(
  '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty',
  1000000000000
);

// 2. Sign transaction
const signedTx = await transfer.signAsync(senderKeypair);

// 3. Send transaction
const hash = await signedTx.send();

// 4. Track transaction status
signedTx.on('status', (status) => {
  console.log('Transaction status:', status.status.type);
  if (status.status.isInBlock) {
    console.log('Transaction included in block:', status.status.asInBlock.toHex());
  }
  if (status.status.isFinalized) {
    console.log('Transaction finalized in block:', status.status.asFinalized.toHex());
  }
});

// 5. Handle events
signedTx.on('events', (events) => {
  events.forEach(({ event }) => {
    if (api.events.balances.Transfer.is(event)) {
      console.log('Transfer event:', event.data.toString());
    }
  });
});

Error Handling

Proper error handling is crucial for transaction management:

Common Transaction Errors:

  • Insufficient Balance: Account doesn't have enough funds
  • Invalid Nonce: Transaction nonce is incorrect
  • Weight Limit Exceeded: Transaction is too complex
  • Bad Origin: Transaction not authorized for this account
  • Stale Transaction: Transaction is too old

Best Practices

Transaction Best Practices:

  • Always check account balance before sending transactions
  • Use proper nonce management to avoid conflicts
  • Set appropriate tips for transaction prioritization
  • Handle all possible errors gracefully
  • Monitor transaction status and provide user feedback
  • Use batch transactions when possible to save fees
  • Test on testnets before mainnet deployment

Summary

In this chapter, we've explored Polkadot's transaction system:

  • • Extrinsics are Polkadot's version of transactions
  • • Three types: inherent, signed, and unsigned extrinsics
  • • Fee calculation includes base, length, weight, and tip components
  • • Weight system provides predictable transaction costs
  • • Proper error handling and status monitoring are essential

In the next chapter, we'll dive into Polkadot's staking mechanism and validator system, learning how the network maintains security through economic incentives.