Learn ETHERSJS with Real Code Examples
Updated Nov 25, 2025
Code Sample Descriptions
1
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.
2
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.
3
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.
4
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.
5
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.
6
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.
7
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.
8
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.
9
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.
10
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.