Guides
Connect with ethers.js
Testnet 122606
Section
Guides
Documentation category
Type
Guide
Technical reference
Summary
Full setup guide for ethers.js v6 — providers, wallets, contract calls, and TypeChain.
Article Content

Installation

npm install ethers@^6

Connecting a Provider

import { JsonRpcProvider } from 'ethers'

const provider = new JsonRpcProvider('https://testnet.swssrpc.swss.io')

// Verify connection
const network = await provider.getNetwork()
console.log('Chain ID:', network.chainId.toString()) // 122606

Reading Chain Data

// Latest block
const block = await provider.getBlock('latest')
console.log('Block:', block?.number)

// Account balance
const balance = await provider.getBalance('0xYourAddress...')
console.log('Balance:', formatEther(balance), 'SWSS')

// Gas price
const feeData = await provider.getFeeData()
console.log('Gas price:', formatUnits(feeData.gasPrice ?? 0n, 'gwei'), 'Gwei')

Signing and Sending Transactions

import { JsonRpcProvider, Wallet, parseEther } from 'ethers'

const provider = new JsonRpcProvider('https://testnet.swssrpc.swss.io')
const wallet   = new Wallet(process.env.PRIVATE_KEY!, provider)

const tx = await wallet.sendTransaction({
  to:    '0xRecipient...',
  value: parseEther('1.0'),
})

console.log('Submitted:', tx.hash)
const receipt = await tx.wait()
console.log('Confirmed in block:', receipt?.blockNumber)

Interacting with a Contract

import { Contract } from 'ethers'

const ABI = ['function balanceOf(address) view returns (uint256)']

const contract = new Contract('0xContractAddress...', ABI, provider)
const balance  = await contract.balanceOf('0xHolder...')
console.log('Token balance:', balance.toString())

Type-Safe with TypeChain

For production projects, generate typed contract bindings with TypeChain:

npx typechain --target ethers-v6 --out-dir src/types 'artifacts/**/*.json'