DEV Community

Ethers.js Cheat Sheet

hideckies on September 04, 2021

ethers.js is a library that interact with Ethereum Blockchain. It is a very useful library but the official documentation was a little hard to read...
Collapse
 
mmajudith profile image
mmajudith

Nice write up. Please did you know how i can validate erc721 token ID input field with ether.js?

Collapse
 
hideckies profile image
hideckies

Thank you. Um...I've never done that so I don't know, sorry.

Collapse
 
nazareth profile image
َ

Awesome. Btw, for ES Modules -> import { ethers } from 'ethers';
You have a typo there.

Collapse
 
hideckies profile image
hideckies

Oh thank you. Fixed;)

Collapse
 
ekamkohli profile image
ekamkohli • Edited

In Convert (Wei -> Ether), const balance = ethers.utils.formatEther(balance.toString());
Please change balance.toString() to balanceBigNumber.toString()

Thanks for providing this cheatsheet.

Collapse
 
hideckies profile image
hideckies

Oh I was careless. Thank you so much!

Collapse
 
baqirnekfar profile image
NekfarBaqir • Edited

I have connected with window.ethereum.request function and now I have a disconnect button, how can I do that?

Collapse
 
0xrajkumar profile image
Rajkumar

How we can write using rpc only ?

Collapse
 
hideckies profile image
hideckies

If you implement only with RPC, probably can't change states on blockchain.

Collapse
 
0xrajkumar profile image
Rajkumar

OK

Collapse
 
0xrajkumar profile image
Rajkumar

We can but we need private to do that, btw thanks

Collapse
 
kelviniot profile image
KelvinIOT

This is soo comprehensive & helpful, thanks for sharing man.... Also is it possible to call a contract in ethersJS without passing in the provider?

Collapse
 
hideckies profile image
hideckies

Thanks! You can do this by connecting from your wallet and passing the signer in parameter when initializing a new contract.

Collapse
 
dawsonbotsford profile image
Daws Bot

Strong list! To help folks even further, let's list all the popular chains you might need to connect to with their ID's:

Polygon: 137
Arbitrum One: 42161
Optimism: 10
Avalanche: 43114
BSC: 56

Collapse
 
hideckies profile image
hideckies

Thanks! Added them;)