Web3 by Example: Querying Transactions

Transactions are cryptographically signed instructions from accounts. An account will initiate a transaction to update the state of the Ethereum network. The simplest transaction is transferring ETH from one account to another.

In the previous example, we queried the block that contains the transaction. We can iterate over all transactions in a block, and query a specific transaction by its hash.

Get the transaction with hash or return null if the transaction is unknown. If a transaction has not been mined, this method will search the transaction pool. Various backends may have more restrictive transaction pool access (e.g. if the gas price is too low or the transaction was only recently sent and not yet indexed) in which case this method may also return null.

Ge the transaction receipt for hash or return null if the transaction has not been mined.

Let's see what the transaction contains.

transaction.js

const { ethers } = require("ethers");
(async () => {
const txHash =
"0x86025d602b75fadab1e2ec4f8c3d69867bb85abd665c14b739367f489799c18f";
const provider = new ethers.providers.AlchemyProvider();
const tx = await provider.getTransaction(txHash);
console.log("tx", tx);
const receipt = await provider.getTransactionReceipt(txHash);
console.log("receipt", receipt);
})();

output

➜ node transaction.js
tx {
hash: '0x86025d602b75fadab1e2ec4f8c3d69867bb85abd665c14b739367f489799c18f',
type: 2,
accessList: [],
blockHash: '0xd0735d21ebe47e306bb7a00df4a877509aa6f8ffa5c65f8de2885faf13b9f95e',
blockNumber: 14292113,
transactionIndex: 0,
confirmations: 621,
from: '0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8',
gasPrice: BigNumber { _hex: '0x09d8dff696', _isBigNumber: true },
maxPriorityFeePerGas: BigNumber { _hex: '0x3b9aca00', _isBigNumber: true },
maxFeePerGas: BigNumber { _hex: '0x138460e74f', _isBigNumber: true },
gasLimit: BigNumber { _hex: '0x03d090', _isBigNumber: true },
to: '0x457db16E5851a4C592D8b96E45f527445f3adeBB',
value: BigNumber { _hex: '0x7c1fe36fa714d0', _isBigNumber: true },
nonce: 40943985,
data: '0x',
r: '0x343bc2514bd9596807deb5131355dc2b165a5fe3c01d453b0b4102da69bc26f1',
s: '0x1c099ea11a8b44131bcaf7f6f5df154a9ccecbeb94b4a3c499f7ca4b254d4c75',
v: 1,
creates: null,
chainId: 1,
wait: [Function (anonymous)]
}
receipt {
to: '0x457db16E5851a4C592D8b96E45f527445f3adeBB',
from: '0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8',
contractAddress: null,
transactionIndex: 0,
gasUsed: BigNumber { _hex: '0x5208', _isBigNumber: true },
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
blockHash: '0xd0735d21ebe47e306bb7a00df4a877509aa6f8ffa5c65f8de2885faf13b9f95e',
transactionHash: '0x86025d602b75fadab1e2ec4f8c3d69867bb85abd665c14b739367f489799c18f',
logs: [],
blockNumber: 14292113,
confirmations: 621,
cumulativeGasUsed: BigNumber { _hex: '0x5208', _isBigNumber: true },
effectiveGasPrice: BigNumber { _hex: '0x09d8dff696', _isBigNumber: true },
status: 1,
type: 2,
byzantium: true
}

Next example: Transferring ETH