Sub topic 8
The Life of a Transaction
Assume you understand wallets, private keys, addresses, and the difference between a blockchain network and an asset. This chapter focuses on what happens after an application creates a transaction and before the network treats it as final.
A transaction is a signed instruction sent to a blockchain. For a native-coin transfer, it names a recipient and an amount. A token transfer usually calls a smart contract. In both cases, the network processes a similar lifecycle.
From instruction to finality
The lifecycle has these stages:
- Construction: A wallet or application creates the transaction. It includes the recipient, value or contract call, fee settings, and the sender's nonce.
- Signing: The wallet uses the private key to create a digital signature. Nodes can verify the signature without learning the private key.
- Broadcast: The signed transaction is sent to one or more nodes through an RPC endpoint.
- Validation: Nodes check the signature, nonce, balance, fee rules, and transaction format. An invalid transaction is rejected.
- Mempool: A valid transaction waits in a node's pool of pending transactions. There is no guarantee that every node has seen it.
- Inclusion: A block producer selects the transaction and places it in a block.
- Confirmation: Later blocks build on that block. Each additional block usually gives the transaction another confirmation.
- Finality: The network reaches a point where reversing the transaction is impossible or highly unlikely under its consensus rules.
A transaction hash identifies the signed transaction. It is useful for looking up status, but it is not proof that the transaction succeeded. A hash can exist for a transaction that is still pending, was dropped, or was included with a failed execution.
Receipt status and reorganization
After inclusion, a node can return a transaction receipt. The receipt commonly contains the block number, gas used, and an execution status:
| Observation | Meaning |
|---|---|
| No receipt | The transaction is pending, unknown, or dropped |
| Receipt status `1` | Execution completed successfully |
| Receipt status `0` | The transaction reached a block but execution failed |
A failed smart-contract transaction can still consume a fee. The network paid for executing the transaction even though the contract's intended state change did not occur.
A temporary chain reorganization, or reorg, happens when nodes replace a recent chain tip with another valid history. A transaction may move out of its original block and return to pending status. Wait for the confirmation policy required by the application rather than trusting inclusion alone.
import { JsonRpcProvider } from "ethers";
const provider = new JsonRpcProvider(process.env.RPC_URL);
const hash = process.env.TX_HASH;
const tx = await provider.getTransaction(hash);
if (!tx) {
console.log("Transaction not found by this provider");
process.exit(0);
}
if (tx.blockNumber === null) {
console.log("Transaction is pending");
process.exit(0);
}
const receipt = await provider.getTransactionReceipt(hash);
const latestBlock = await provider.getBlockNumber();
console.log("Included in block:", tx.blockNumber);
console.log("Confirmations:", latestBlock - tx.blockNumber + 1);
console.log("Status:", receipt?.status === 1 ? "success" : "failed");What to trust
Use the recipient address, network, transaction hash, receipt, and confirmation count together. For valuable transfers, verify the transaction through an independent block explorer or node. Do not treat a wallet's “sent” message as proof of delivery. It may only mean that the wallet broadcast the transaction.
Exercise
A transaction has a hash but no receipt. What does that tell you?
Show answer
It has been identified by its hash, but this provider has not found it included in a block. It may still be pending, delayed, or dropped.