Skip to main content

ethers v6 quickstart

This example shows how to use ethers v6 with Sei. The patterns apply whether you write a Node.js script, a CLI tool, or a browser dApp.

Install

These snippets are TypeScript, and they use top-level await. To run one without a separate build step, save it as script.mts and use tsx (Node 18+):
The .mts extension tells Node.js to treat the file as an ES module. If you use a plain .ts file in a new npm project, tsx fails with Top-level await is currently not supported with the "cjs" output format.

Read-only provider

Reading chain data

This is the first milestone. It does not need a private key.
You are done when you see:
The exact numbers are illustrative. Your block number and balance will differ. getBlockNumber() and getTransactionCount() return a number, while getBalance() returns a bigint in wei. To format the balance, use ethers.formatEther().

Try it live

Run these read-only calls against Sei Mainnet. You do not need to install anything or use a private key. They are plain JSON-RPC calls, the same reads that the script above performs.

Edit and run

Edit and re-run the full ethers code from this page live in the browser. The sandbox uses a real ethers v6 JsonRpcProvider that reads Sei Testnet. Its block number and balances therefore do not match the Sei Mainnet reads above:

Browser provider

In a browser context, connect to the user’s injected wallet:

Wallet from private key

Use this pattern in scripts and backend services:

Sending a transaction

This next step requires a funded account. Get testnet SEI from the faucet.

Reading a contract

Writing to a contract

For write operations, pass a signer (a wallet or a browser signer) to the contract constructor:

Estimating gas

Listening for events

Listen for new events over the WebSocket endpoint. Listeners that poll the public HTTP endpoint can miss events or report them twice. The contract needs the event in its ABI:
For past events and more examples, see Listen to events.

Next steps