Learn how providers allow you to read data from the Ethereum blockchain.
A Provider is a read-only connection to the Ethereum blockchain. It lets you query data like account balances, transaction details, and contract state, but cannot send transactions or sign messages.
Key Point: Providers are stateless and can be shared across your application.
Direct connection to an Ethereum RPC endpoint
Use Case: Backend servers, custom RPC endpoints
Connection to MetaMask or other browser wallet
Use Case: Browser-based dApps
Connection to Infura infrastructure
Use Case: Public RPC endpoint (requires API key)
Connection to Alchemy infrastructure
Use Case: Public RPC endpoint (requires API key)
import { ethers } from 'ethers';
// Connect to public RPC (Ethereum mainnet)
const provider = new ethers.JsonRpcProvider(
'https://eth.llamarpc.com'
);
// Connect to MetaMask
const browserProvider = new ethers.BrowserProvider(
window.ethereum
);
// Connect to Sepolia testnet
const testnetProvider = new ethers.JsonRpcProvider(
'https://sepolia.infura.io/v3/YOUR_API_KEY'
);import { ethers } from 'ethers';
const provider = new ethers.JsonRpcProvider(
'https://eth.llamarpc.com'
);
// Get balance (much easier than raw JSON-RPC!)
const balance = await provider.getBalance('0x...');
console.log(ethers.formatEther(balance)); // "1.5" instead of "1500000000000000000"
// Get transaction count (nonce)
const nonce = await provider.getTransactionCount('0x...');
// Get current block
const block = await provider.getBlock('latest');
console.log(block.number);
console.log(block.timestamp);
// Get transaction by hash
const tx = await provider.getTransaction('0x...');
// Get transaction receipt
const receipt = await provider.getTransactionReceipt('0x...');
// Get network
const network = await provider.getNetwork();
console.log(network.name); // "mainnet", "sepolia", etc
// Get current gas price
const gasPrice = await provider.getGasPrice();
console.log(ethers.formatUnits(gasPrice, 'gwei')); // In gweiimport { ethers } from 'ethers';
// Mainnet (Ethereum)
const mainnetProvider = new ethers.JsonRpcProvider(
'https://eth.llamarpc.com'
);
// Sepolia Testnet
const sepoliaProvider = new ethers.JsonRpcProvider(
'https://sepolia.infura.io/v3/YOUR_API_KEY'
);
// Polygon
const polygonProvider = new ethers.JsonRpcProvider(
'https://rpc.ankr.com/polygon'
);
// Arbitrum
const arbitrumProvider = new ethers.JsonRpcProvider(
'https://rpc.ankr.com/arbitrum'
);
// Check network
const network = await mainnetProvider.getNetwork();
console.log(network.chainId); // 1 for mainnet
console.log(network.name); // "mainnet"Providers can listen to blockchain events and notify you when they occur.
import { ethers } from 'ethers';
const provider = new ethers.JsonRpcProvider(
'https://eth.llamarpc.com'
);
// Listen for new blocks
provider.on('block', (blockNumber) => {
console.log('New block:', blockNumber);
});
// Listen for transaction to a specific address
provider.on('0x1234...', (balance) => {
console.log('Address balance changed!');
});
// One-time listener
provider.once('block', (blockNumber) => {
console.log('First block heard:', blockNumber);
});
// Remove listener
provider.removeAllListeners('block');