From 489200b9c818e5c958c1782e173684ab2515385e Mon Sep 17 00:00:00 2001 From: picodes Date: Mon, 9 Dec 2024 13:40:18 +0100 Subject: [PATCH] chore: fmt service --- src/components/dapp/WalletButton.tsx | 60 ++++++++++++++++++---------- src/utils/format.ts | 13 ------ src/utils/formatter.service.ts | 40 +++++++++++++++++++ 3 files changed, 80 insertions(+), 33 deletions(-) delete mode 100644 src/utils/format.ts create mode 100644 src/utils/formatter.service.ts diff --git a/src/components/dapp/WalletButton.tsx b/src/components/dapp/WalletButton.tsx index 250976e3..85e4a423 100644 --- a/src/components/dapp/WalletButton.tsx +++ b/src/components/dapp/WalletButton.tsx @@ -1,6 +1,6 @@ import { type ReactNode, useMemo } from "react"; import { useWalletContext } from "../../context/Wallet.context"; -import { Format } from "../../utils/format"; +import { Fmt } from "../../utils/formatter.service"; import Dropdown from "../extenders/Dropdown"; import Group from "../extenders/Group"; import Modal from "../extenders/Modal"; @@ -16,27 +16,36 @@ import WalletConnectors from "./WalletConnectors"; export type WalletButton = ButtonProps; export default function WalletButton(props: ButtonProps) { - const { address, disconnect, connected, connector, chainId, switchChain, chains } = useWalletContext(); + const { + address, + disconnect, + connected, + connector, + chainId, + switchChain, + chains, + } = useWalletContext(); const chainOptions = useMemo(() => { if (!chains) return []; - return chains.reduce( - (obj, chain) => { - obj[chain.id] = ( - - - {chain.name} - - ); - return obj; - }, - {} as { [chainId: number]: ReactNode }, - ); + return chains.reduce((obj, chain) => { + obj[chain.id] = ( + + + {chain.name} + + ); + return obj; + }, {} as { [chainId: number]: ReactNode }); }, [chains]); if (!connected) return ( - }> + } + > @@ -45,7 +54,10 @@ export default function WalletButton(props: ButtonProps) { return ( <> - switchChain(+c)]} + options={chainOptions} + /> {/* TODO: Show the account icon by default if there is no ENS icon */} - + {address} - @@ -78,9 +97,10 @@ export default function WalletButton(props: ButtonProps) { - }> + } + > diff --git a/src/utils/format.ts b/src/utils/format.ts deleted file mode 100644 index 9ca1fbb3..00000000 --- a/src/utils/format.ts +++ /dev/null @@ -1,13 +0,0 @@ -export abstract class Format { - public static address(value?: string, format?: "short" | "prefix") { - if (!value) return; - switch (format) { - case "short": - return `${value?.slice(0, 2 + 5)}...${value?.slice(-5)}`; - case "prefix": - return value?.slice(0, 5); - default: - return value; - } - } -} diff --git a/src/utils/formatter.service.ts b/src/utils/formatter.service.ts new file mode 100644 index 00000000..5cded40f --- /dev/null +++ b/src/utils/formatter.service.ts @@ -0,0 +1,40 @@ +import { parseUnits } from "viem"; + +export class FormatterService { + public static address(value?: string, format?: "short" | "prefix") { + if (!value) return; + switch (format) { + case "short": + return `${value?.slice(0, 2 + 5)}...${value?.slice(-5)}`; + case "prefix": + return value?.slice(0, 5); + default: + return value; + } + } + + toNumber(value: bigint | string, decimals = 18): number { + const bi = BigInt(value); + + const fractionalPart = + Number.parseFloat((bi % BigInt(10 ** decimals)).toString()) / + 10 ** decimals; + let integerPart = bi / BigInt(10 ** decimals); + + // trim the integer part if it's too large to avoid potential overflows + if (integerPart > BigInt(Number.MAX_SAFE_INTEGER)) { + integerPart = BigInt(Number.MAX_SAFE_INTEGER) / BigInt(10 ** 10); + } + if (integerPart < -1n * BigInt(Number.MAX_SAFE_INTEGER)) { + integerPart = (-1n * BigInt(Number.MAX_SAFE_INTEGER)) / BigInt(10 ** 10); + } + + return Number.parseFloat(integerPart.toString()) + fractionalPart; + } + + toBigInt(value: number, decimals = 18): bigint { + return parseUnits(value.toString(), decimals); + } +} + +export { FormatterService as Fmt };