Learn Ethersjs - 10 Code Examples & CST Typing Practice Test
Ethers.js is a lightweight, complete JavaScript library for interacting with the Ethereum blockchain, providing tools for wallet management, smart contract interaction, and blockchain communication.
View all 10 Ethersjs code examples →
Learn ETHERSJS with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
Ethers.js Simple Smart Contract Interaction
const { ethers } = require('ethers')
// Connect to local Ethereum node
const provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545')
// Contract ABI and address
const abi = [...]
const address = '0xYourContractAddress'
const contract = new ethers.Contract(address, abi, provider)
// Read value from contract
async function main() {
const value = await contract.getValue()
console.log('Contract value:', value)
// Send transaction to contract
const signer = provider.getSigner(0)
const tx = await contract.connect(signer).setValue(42)
await tx.wait()
console.log('Transaction complete')
}
main()
A minimal example showing how to connect to Ethereum and interact with a deployed smart contract using Ethers.js.
Ethers.js Check Balance
const { ethers } = require('ethers')
const provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545')
async function main() {
const balance = await provider.getBalance('0xYourAddress')
console.log('Balance:', ethers.formatEther(balance))
}
main()
Connects to Ethereum and prints the Ether balance of an account.
Ethers.js Send Ether
const { ethers } = require('ethers')
const provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545')
async function main() {
const signer = provider.getSigner(0)
const tx = await signer.sendTransaction({
to: '0xRecipientAddress',
value: ethers.parseEther('0.1')
})
await tx.wait()
console.log('Ether sent')
}
main()
Send Ether from one account to another using Ethers.js.
Ethers.js Deploy Contract
const { ethers } = require('ethers')
const provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545')
const signer = provider.getSigner(0)
async function main() {
const factory = new ethers.ContractFactory(abi, bytecode, signer)
const contract = await factory.deploy()
await contract.deployed()
console.log('Contract deployed at:', contract.address)
}
main()
Deploy a simple contract using Ethers.js and a signer.
Ethers.js Listen to Events
const { ethers } = require('ethers')
const provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545')
const contract = new ethers.Contract('0xYourContractAddress', abi, provider)
contract.on('ValueChanged', (newValue) => {
console.log('Value changed:', newValue)
})
Subscribe to a contract event and log it in real-time.
Ethers.js Read-Only Contract Call
const { ethers } = require('ethers')
const provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545')
const contract = new ethers.Contract('0xYourContractAddress', abi, provider)
async function main() {
const data = await contract.getData()
console.log('Contract data:', data)
}
main()
Perform a read-only call to fetch contract data without sending a transaction.
Ethers.js Estimate Gas
const { ethers } = require('ethers')
const provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545')
const contract = new ethers.Contract('0xYourContractAddress', abi, provider)
async function main() {
const gasEstimate = await contract.estimateGas.setValue(42)
console.log('Estimated gas:', gasEstimate)
}
main()
Estimate gas usage for a contract method call.
Ethers.js Batch Call Example
const { ethers } = require('ethers')
const provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545')
const contract = new ethers.Contract('0xYourContractAddress', abi, provider)
async function main() {
const value1 = await contract.getValue()
const value2 = await contract.getAnotherValue()
console.log('Values:', value1, value2)
}
main()
Use Ethers.js to make multiple contract calls in sequence.
Ethers.js Sign Message
const { ethers } = require('ethers')
const provider = new ethers.JsonRpcProvider('http://127.0.0.1:8545')
async function main() {
const signer = provider.getSigner(0)
const signature = await signer.signMessage('Hello Ethereum')
console.log('Signature:', signature)
}
main()
Sign a message with an Ethereum account using Ethers.js.
Ethers.js Verify Signature
const { ethers } = require('ethers')
const message = 'Hello Ethereum'
const signature = '0xSignatureHere'
const signerAddress = ethers.verifyMessage(message, signature)
console.log('Signer address:', signerAddress)
Verify an Ethereum signature using Ethers.js.
Frequently Asked Questions about Ethersjs
What is Ethersjs?
Ethers.js is a lightweight, complete JavaScript library for interacting with the Ethereum blockchain, providing tools for wallet management, smart contract interaction, and blockchain communication.
What are the primary use cases for Ethersjs?
Interacting with Ethereum smart contracts. Wallet management and transaction signing. Connecting to Ethereum nodes (mainnet, testnets). Building frontend dApps. Reading blockchain state and events
What are the strengths of Ethersjs?
Simple and easy-to-learn API. Modular and tree-shakable for frontend use. Secure default handling of keys and signing. TypeScript support improves development safety. Actively maintained and widely adopted
What are the limitations of Ethersjs?
Ethereum-only (doesn’t natively support other chains without EVM compatibility). Requires understanding of blockchain concepts. Some advanced features (ENS, multicall) need extra modules. No native transaction batching support. Limited UI helpers (requires integration with other frontend libraries)
How can I practice Ethersjs typing speed?
CodeSpeedTest offers 10+ real Ethersjs code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.