Created
April 30, 2022 09:44
-
-
Save elcharitas/c593f3ba4bc6553c3adbfdf54d0dde62 to your computer and use it in GitHub Desktop.
Safely Convert Ethereum BigNumber to and from JavaScript Number with Units
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment