-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
30 additions
and
5 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
import { BN } from "@polkadot/util"; | ||
import { formatEther } from "viem"; | ||
|
||
export function formatEtherAsString(wei: bigint | string | BN): string { | ||
let weiBigInt: bigint; | ||
|
||
if (typeof wei === "bigint") { | ||
weiBigInt = wei; | ||
} else if (typeof wei === "string") { | ||
weiBigInt = BigInt(wei); | ||
} else if (wei instanceof BN) { | ||
weiBigInt = BigInt(wei.toString()); | ||
} else { | ||
throw new Error("Invalid input type"); | ||
} | ||
|
||
return formatEther(weiBigInt); | ||
} | ||
|
||
export function formatEtherAsNumber(wei: bigint | string | BN): number { | ||
const etherBigInt = BigInt(formatEtherAsString(wei)); | ||
const MAX_SAFE_INTEGER_BIGINT = BigInt(Number.MAX_SAFE_INTEGER); | ||
|
||
if (etherBigInt > MAX_SAFE_INTEGER_BIGINT || etherBigInt < -MAX_SAFE_INTEGER_BIGINT) { | ||
throw new Error("Value is outside of safe integer bounds for JavaScript numbers"); | ||
} | ||
|
||
return Number(etherBigInt); | ||
} |
This file was deleted.
Oops, something went wrong.