Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fmt service #18

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/dapp/WalletButton.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -80,7 +80,7 @@ export default function WalletButton(props: ButtonProps) {
</>
}>
<Button look="tint" className="w-full justify-center">
{Format.address(address, "short")}
{Fmt.address(address, "short")}
</Button>
</Dropdown>
</>
Expand Down
13 changes: 0 additions & 13 deletions src/utils/format.ts

This file was deleted.

38 changes: 38 additions & 0 deletions src/utils/formatter.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 };
Loading