Scholarpeak

Ethers.js V6 Introduction

Learn how Ethers.js simplifies Ethereum development with abstractions over raw JSON-RPC.

What is Ethers.js?

Ethers.js is a JavaScript library that makes it easier to interact with Ethereum. Instead of dealing with raw JSON-RPC methods, you get convenient abstractions for common tasks like sending transactions, reading contract data, and managing wallets.

Why use Ethers.js instead of raw JSON-RPC?

  • ✓ Automatic hex/decimal conversion (no more manual BigInt conversions)
  • ✓ Cleaner syntax and better developer experience
  • ✓ Built-in error handling and validation
  • ✓ Contract interaction abstraction (no manual ABI encoding)
  • ✓ Better TypeScript support

Installation

Install Ethers.js V6 using npm or yarn:

bash
npm install ethers@6
# or
yarn add ethers@6
# or
pnpm add ethers@6

Key Concepts in Ethers.js

Provider

Reads data from blockchain (view-only)

Signer

Sends transactions and signs messages

Contract

Interacts with smart contracts

Wallet

Manages private keys and accounts

Simple Example: Get Balance

Compare raw JSON-RPC with Ethers.js. Notice how Ethers.js handles conversions automatically.

Raw JSON-RPC:

typescript
// Lots of manual work
const balance = await window.ethereum.request({
  method: 'eth_getBalance',
  params: ['0x...', 'latest']
});

// Manual conversion from Wei to ETH
const ethBalance = 
  (Number(BigInt(balance)) / 1e18)
    .toFixed(2);

Ethers.js:

typescript
// Much cleaner!
import { ethers } from 'ethers';

const provider = new ethers.JsonRpcProvider(
  'https://eth.llamarpc.com'
);

const balance = await provider
  .getBalance('0x...');

// Already in BigNumber format
const ethBalance = 
  ethers.formatEther(balance);

Connecting to MetaMask with Ethers.js

Ethers.js provides a simple way to connect to MetaMask and get a signer for transactions.

typescript
import { ethers } from 'ethers';

async function connectWallet() {
  // Request account access
  await window.ethereum.request({
    method: 'eth_requestAccounts'
  });

  // Create provider from MetaMask
  const provider = new ethers.BrowserProvider(
    window.ethereum
  );

  // Get signer (user's wallet)
  const signer = await provider.getSigner();

  // Get user's address
  const address = await signer.getAddress();
  
  console.log('Connected:', address);

  return { provider, signer };
}

What You'll Learn

  • Providers - Reading data from Ethereum
  • Signers & Wallets - Signing transactions
  • Contracts - Interacting with smart contracts
  • Advanced Patterns - Complex scenarios