diff --git a/src/app/dashboard/reader/page.tsx b/src/app/dashboard/reader/page.tsx index 94de1dd3..c6722ed1 100644 --- a/src/app/dashboard/reader/page.tsx +++ b/src/app/dashboard/reader/page.tsx @@ -8,6 +8,17 @@ import { ReaderPortfolio } from "../../../components/ReaderPortfolio"; import { WriterIdentityClient } from "../../../components/WriterIdentityClient"; import { formatUnits } from "viem"; import { ConnectWallet } from "../../../components/ConnectWallet"; +import { RESERVE_LABEL, PLOT_TOKEN } from "../../../../lib/contracts/constants"; +import { publicClient } from "../../../../lib/rpc"; +import { type Address } from "viem"; + +/** Truncate formatUnits output to at most `digits` decimal places */ +function formatTruncated(value: bigint, decimals: number, digits = 6): string { + const raw = formatUnits(value, decimals); + const dot = raw.indexOf("."); + if (dot === -1 || raw.length - dot - 1 <= digits) return raw; + return raw.slice(0, dot + 1 + digits).replace(/0+$/, "").replace(/\.$/, ""); +} const PAGE_SIZE = 50; @@ -49,6 +60,18 @@ export default function ReaderDashboard() { enabled: isConnected && !!address, }); + // Fetch reserve token decimals dynamically + const { data: reserveDecimals = 18 } = useQuery({ + queryKey: ["reserve-decimals"], + queryFn: async () => { + return publicClient.readContract({ + address: PLOT_TOKEN as Address, + abi: [{ type: "function", name: "decimals", stateMutability: "view", inputs: [], outputs: [{ name: "", type: "uint8" }] }] as const, + functionName: "decimals", + }); + }, + }); + const donations = data?.rows ?? []; const totalCount = data?.totalCount ?? 0; @@ -93,7 +116,7 @@ export default function ReaderDashboard() { {donations.length > 0 && ( {" "} - · {formatUnits(totalDonated, 18)} $PLOT on this page + · {formatTruncated(totalDonated, reserveDecimals)} {RESERVE_LABEL} on this page )}

@@ -108,7 +131,7 @@ export default function ReaderDashboard() {
{donations.map((d) => ( - + ))} {!isLoading && !error && donations.length === 0 && (

@@ -143,7 +166,7 @@ export default function ReaderDashboard() { ); } -function DonationRow({ donation }: { donation: Donation }) { +function DonationRow({ donation, decimals }: { donation: Donation; decimals: number }) { return (

@@ -160,7 +183,7 @@ function DonationRow({ donation }: { donation: Donation }) { )}
- {formatUnits(BigInt(donation.amount), 18)} $PLOT + {formatTruncated(BigInt(donation.amount), decimals)} {RESERVE_LABEL}
);