Skip to content

Instantly share code, notes, and snippets.

@elcharitas
Created April 30, 2022 09:44
Show Gist options
  • Save elcharitas/c593f3ba4bc6553c3adbfdf54d0dde62 to your computer and use it in GitHub Desktop.
Save elcharitas/c593f3ba4bc6553c3adbfdf54d0dde62 to your computer and use it in GitHub Desktop.
Safely Convert Ethereum BigNumber to and from JavaScript Number with Units
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