Scholarpeak

Providers in Ethers.js V6

Learn how providers allow you to read data from the Ethereum blockchain.

What is a Provider?

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.

Common Providers

JsonRpcProvider

Direct connection to an Ethereum RPC endpoint

Use Case: Backend servers, custom RPC endpoints

BrowserProvider

Connection to MetaMask or other browser wallet

Use Case: Browser-based dApps

InfuraProvider

Connection to Infura infrastructure

Use Case: Public RPC endpoint (requires API key)

AlchemyProvider

Connection to Alchemy infrastructure

Use Case: Public RPC endpoint (requires API key)

Creating a Provider

typescript
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'
);

Common Provider Methods

typescript
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 gwei

Working with Different Networks

typescript
import { 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"

Listening to Events

Providers can listen to blockchain events and notify you when they occur.

typescript
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');