Learn how to send transactions and track their status on the blockchain.
Sign and broadcast a transaction to the network
This is the main method for sending ETH or calling smart contracts. It opens MetaMask for the user to confirm the transaction. Returns the transaction hash.
async function sendETH(toAddress, amountETH) {
try {
const accounts = await window.ethereum.request({
method: 'eth_requestAccounts'
});
// Convert ETH to Wei
const amountWei = '0x' + BigInt(Math.round(amountETH * 1e18)).toString(16);
const txHash = await window.ethereum.request({
method: 'eth_sendTransaction',
params: [{
from: accounts[0],
to: toAddress,
value: amountWei
}]
});
console.log('Transaction sent:', txHash);
return txHash;
} catch (error) {
console.error('Transaction failed:', error);
}
}Important: Always convert ETH to Wei and use hex format for the value. Use BigInt to avoid floating-point precision errors.
Get details about a specific transaction
Returns the transaction object with details like sender, receiver, amount, and gas information. Returns null if the transaction doesn't exist.
async function getTransactionDetails(txHash) {
const tx = await window.ethereum.request({
method: 'eth_getTransactionByHash',
params: [txHash]
});
if (!tx) {
console.log('Transaction not found');
return null;
}
console.log('From:', tx.from);
console.log('To:', tx.to);
console.log('Value:', Number(BigInt(tx.value)) / 1e18, 'ETH');
console.log('Gas:', Number(BigInt(tx.gas)));
console.log('Gas Price:', Number(BigInt(tx.gasPrice)) / 1e9, 'GWei');
return tx;
}Get the receipt of a mined transaction
Returns the receipt of a transaction once it's been mined. The receipt includes the transaction status (success or failure), gas used, and block information. Returns null if the transaction hasn't been mined yet.
async function waitForTransaction(txHash) {
let receipt = null;
let attempts = 0;
while (!receipt && attempts < 60) {
receipt = await window.ethereum.request({
method: 'eth_getTransactionReceipt',
params: [txHash]
});
if (!receipt) {
console.log('Waiting for transaction to be mined...');
await new Promise(resolve => setTimeout(resolve, 1000));
attempts++;
}
}
if (!receipt) {
console.error('Transaction timeout');
return null;
}
const isSuccess = receipt.status === '0x1';
console.log('Transaction status:', isSuccess ? 'Success' : 'Failed');
console.log('Gas used:', Number(BigInt(receipt.gasUsed)));
console.log('Block:', Number(BigInt(receipt.blockNumber)));
return receipt;
}Status Codes:
Estimate gas needed for a transaction
Simulates a transaction execution and returns the estimated gas amount needed. Always call this before sending a transaction to avoid out-of-gas errors.
async function estimateAndSendTransaction(toAddress, amountETH) {
try {
const accounts = await window.ethereum.request({
method: 'eth_requestAccounts'
});
const amountWei = '0x' + BigInt(Math.round(amountETH * 1e18)).toString(16);
// Estimate gas
const estimatedGas = await window.ethereum.request({
method: 'eth_estimateGas',
params: [{
from: accounts[0],
to: toAddress,
value: amountWei
}]
});
console.log('Estimated gas:', Number(BigInt(estimatedGas)));
// Add 20% buffer to ensure transaction succeeds
const gasLimit = BigInt(estimatedGas) * BigInt(120) / BigInt(100);
// Send transaction
const txHash = await window.ethereum.request({
method: 'eth_sendTransaction',
params: [{
from: accounts[0],
to: toAddress,
value: amountWei,
gas: '0x' + gasLimit.toString(16)
}]
});
return txHash;
} catch (error) {
console.error('Error:', error);
}
}