Understanding Transaction Types, Fees, and Gas Management
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.
Polkadot supports several types of extrinsics, each serving different purposes:
These are extrinsics that are not signed and are added to blocks by validators. They represent information that is already known to be true.
These are extrinsics that are signed by an account and represent actions initiated by users.
These are extrinsics that are not signed but are validated by the runtime logic.
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"
}
}
}Polkadot uses a sophisticated fee calculation system that considers multiple factors:
Unlike Ethereum's gas system, Polkadot uses a "weight" system to measure computational cost:
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());
}
});
});Proper error handling is crucial for transaction management:
In this chapter, we've explored Polkadot's transaction system:
In the next chapter, we'll dive into Polkadot's staking mechanism and validator system, learning how the network maintains security through economic incentives.