How to Safely convert BigNumbers with ethers

Photo by Nick Hillier on Unsplash

How to Safely convert BigNumbers with ethers

elcharitas.(eth|lens|crypto|any)'s photo
·Apr 30, 2022·

1 min read

Play this article

The Problem?

Big numbers are an everyday use for web3 developers on EVM based blockchains. Consider a smart contract like the below:

pragma solidity ^0.8.13;

contract Sample {
    uint256 public constant number = 5000 ether;

If we were to interact with the contract above via ethers, we would write something like:

import { ethers } from "ethers";

const address =  "...";
const abi = [...];

const someAction = (bigNumber) => {
    console.log(bigNumber.toNumber()); // throws an error

const errorAction = () => {}

const contract = new ethers.Contract(address, abi);

From ethersjs docs, BigNumbers have a 'toNumber' property which ought to convert the BigNumber to a normal number. However, this has it's limits. It works for big number values less than 1 ether but for anything more, problems arise due to JavaScript Safe integer.

The fix?

I created a gist right here Feel free to drop comments on it

import { utils } from "ethers";

export function convert(value, unit = "ether") {
    if (value instanceof ethers.BigNumber) {
        return utils.formatUnits(value.toString(), unit);
    } else {
        return ethers.BigNumber.from(value);

The code above simply allows us to convert from normal numbers to big numbers and viz-a-viz.

I hope this helps someone. Cheers!

Share this