Skip to main content
Sosho Studio logo
Digital wallet sending a token along a transaction path
ethers.jsBlockchainPublished Updated

ethers.js: Working with Wallets and Transactions

A practical ethers.js v6 guide for dApps: connecting wallets, the right network, reading and writing contracts, tracking transactions, BigInt, and clear errors.

Quick answer

In ethers.js v6, use a provider such as JsonRpcProvider or BrowserProvider to read data and the user's signer to write: connect the wallet with BrowserProvider and getSigner, check and if needed switch the network before every transaction, call the contract function, and wait for confirmation with tx.wait(). Handle token amounts with parseUnits, formatUnits, and BigInt, and turn errors such as a user rejection, insufficient funds, or a contract revert into clear messages.

Provider and signer: reading versus writing

A provider is a read-only connection to the network: it reads balances, contract state, and events and does not need the user's wallet. A signer is an account that can sign transactions, usually coming from the user's wallet, such as MetaMask.

Do reads through a reliable RPC so the page shows data even without a connected wallet, and request the signer only when writing.

Connecting the wallet and the right network

Most user errors come from being on the wrong network or switching accounts mid-flow.

  • Use the EIP-6963 standard to discover installed wallets so several wallets do not conflict
  • Check the chainId before every transaction and request a network switch if needed
  • Listen for account and network change events and update the page state
  • Show the user's address shortened, with an option to copy it in full

Reading and writing contracts

Create a Contract instance from the ABI and address; connected to a provider it only reads, connected to a signer it can send transactions. Before sending a real transaction, simulate it with staticCall so that if it would revert, you can tell the user before they pay a fee.

Use event filters to list past events, but build a separate indexer for long histories; reading many blocks directly from RPC is slow and rate-limited.

Big numbers and units

In ethers.js v6, amounts are native JavaScript BigInt values. Never compute token amounts with regular floating-point numbers; precision is lost.

Each token has its own number of decimals, for example 18 for ether and 6 for USDT on many networks. Use parseUnits to convert user input and formatUnits to display, with the token's decimals.

Sending and tracking transactions

After sending, show the transaction hash immediately with a link to the explorer. Keep a pending state until tx.wait() resolves, and wait for more confirmations for significant amounts.

A transaction may be replaced with a higher fee or cancelled; handle that case and never leave the page in an unknown state.

Take error handling seriously

Raw RPC error messages mean nothing to users. Map errors by their code to clear messages:

  • ACTION_REJECTED: the user rejected the transaction in the wallet
  • INSUFFICIENT_FUNDS: the balance does not cover the amount or the fee
  • CALL_EXCEPTION: the contract reverted; show the reason or custom error
  • Network or RPC errors: use several fallback RPCs for reads and retry

Frequently asked questions

What changed between ethers.js v5 and v6?

Version 6 uses native BigInt instead of BigNumber, BrowserProvider replaces Web3Provider, and helpers such as parseEther are available directly from the library root. Migration is usually limited to those areas.

Why did a failed transaction still cost a fee?

If a transaction executes on-chain and then reverts, the fee for its execution is still paid. Simulating the transaction before sending catches most of these failures before any fee is paid.

How many confirmations should we wait for?

It depends on the network and the amount. One confirmation is usually enough to update the interface, but financial settlement or delivering goods should wait for that network's reliable finality.