DEV Community

Marco Quintella
Marco Quintella

Posted on

Migrating from ethers.js v5 to v6

The Ether.js Library

Ethers.js is a complete and compact library focused on iteracting with EVM blockchains and the Ethereum ecosystem. It's most frequently used in Dapps, wallets and when someone wants to read or write something manually to the blockchains.
The version 6 ws released in March 23 and brought many improvements and changes over its previous version. Let's explore some differences between both versions and how to do a proper migration of your project in a safe and efficient way.

BigInt killing BigNumber

The biggest impact change is that replacement of the class BigNumber in favor of the JS native BigInt. This removes many operations that were made using BigNumber functions by simply using the normal JS operators.

// Big Number in version 5
value = BigNumber.from('1000')
// Sum on version 5
sum = value1.add(value2)
// Equality
isEqual = value1.eq(value2)

// BigInt in version 6
value = 1000n
// Sum, remember both values needs to be a BigInt
sum = value1 + value2
// Equality
isEqual = (value1 === value2)
Enter fullscreen mode Exit fullscreen mode

BigInt is faster, simpler and compatible with any JS environment. The problem here is that BigInt just works with Integers. So for non-integers we use the FixedNumber class.

ES6 Proxy Contracts

The Contract is a ES6 Proxy, that means that it can resolve names and methods in execution time. Its more convenient and flexible when interacting with smart contracts.

Method Overloading

In v6 we can access overloaded methods directly by the Typescript interface.

abi = [
  "function foo(address bar)",
  "function foo(uint160 bar)"
contract = new Contract(address, abi, provider)

// In v6 this will work easily
Enter fullscreen mode Exit fullscreen mode

Method Operations

Some methods inherit to contract operations are not available in the Contract direct scope anymore. Now you need the namesapce in to access them.
Enter fullscreen mode Exit fullscreen mode

There are more minor improvements that are worth of studying in ethers.js Docs

Top comments (0)