|
| 1 | +import { getLocale, getTranslations } from "next-intl/server"; |
| 2 | +import { Layers } from "lucide-react"; |
| 3 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; |
| 4 | +import { formatFiatUsd } from "@/lib/i18n/fiat"; |
| 5 | +import { toIntlLocale } from "@/lib/i18n/format"; |
| 6 | +import { getBrlRateForRequest } from "@/services/exchange-rate"; |
| 7 | +import { loadTreasuryDefi, type DefiLiquidity } from "@/services/treasury-defi"; |
| 8 | + |
| 9 | +/** |
| 10 | + * "In DeFi" — what the treasury owns by right in protocol positions, stock not |
| 11 | + * flow. Every row is its own on-chain read; a failed read renders as a failure |
| 12 | + * (amber), a legitimate zero renders as $0 — the two must never look alike. |
| 13 | + * The vault-fee row is the stake graph's own `gnarsAccrued`, so this card and |
| 14 | + * the Sponsorship card can never disagree about that number. |
| 15 | + */ |
| 16 | +export async function TreasuryDefi() { |
| 17 | + const t = await getTranslations("treasury.defi"); |
| 18 | + const locale = await getLocale(); |
| 19 | + const [defi, brlRate] = await Promise.all([loadTreasuryDefi(), getBrlRateForRequest()]); |
| 20 | + |
| 21 | + const fiat = (usd: number) => formatFiatUsd(usd, locale, brlRate); |
| 22 | + const lockLabel = (liq: DefiLiquidity): string => { |
| 23 | + if (liq.kind === "proposal") return t("liquidity.proposal"); |
| 24 | + if (liq.kind === "locked") |
| 25 | + return t("liquidity.locked", { |
| 26 | + date: new Date(liq.unlockAt * 1000).toLocaleDateString(toIntlLocale(locale), { |
| 27 | + day: "2-digit", |
| 28 | + month: "2-digit", |
| 29 | + }), |
| 30 | + }); |
| 31 | + return t("liquidity.instant"); |
| 32 | + }; |
| 33 | + |
| 34 | + return ( |
| 35 | + <Card className="gap-2"> |
| 36 | + <CardHeader> |
| 37 | + <CardTitle className="flex items-center gap-2 text-sm font-medium"> |
| 38 | + <Layers className="size-4" /> {t("title")} |
| 39 | + </CardTitle> |
| 40 | + <CardDescription>{t("description")}</CardDescription> |
| 41 | + </CardHeader> |
| 42 | + <CardContent className="space-y-1"> |
| 43 | + <ul className="divide-y"> |
| 44 | + {defi.positions.map((p) => ( |
| 45 | + <li key={p.labelKey} className="flex items-baseline justify-between gap-3 py-2.5"> |
| 46 | + <div className="min-w-0"> |
| 47 | + <div className="text-[13px] font-medium">{t(`positions.${p.labelKey}`)}</div> |
| 48 | + {/* Zero-by-design gets its explanation, not a redeem hint: the |
| 49 | + treasury never deposits into the vaults — supporters do — |
| 50 | + so a mute $0 here reads as a bug and invites the exact |
| 51 | + wrong "fix" (rendering totalAssets, i.e. everyone's money, |
| 52 | + as Gnars net worth). */} |
| 53 | + <div className="text-[11px] text-muted-foreground"> |
| 54 | + {p.labelKey === "vaultShares" && p.ok && p.usd === 0 |
| 55 | + ? t("positions.vaultSharesZeroNote") |
| 56 | + : lockLabel(p.liquidity)} |
| 57 | + </div> |
| 58 | + </div> |
| 59 | + <div className="shrink-0 text-right"> |
| 60 | + {p.ok ? ( |
| 61 | + <> |
| 62 | + <span |
| 63 | + className={`font-mono text-sm tabular-nums ${p.usd === 0 ? "text-muted-foreground" : "font-semibold"}`} |
| 64 | + > |
| 65 | + {fiat(p.usd)} |
| 66 | + </span> |
| 67 | + {p.amount && ( |
| 68 | + <div className="text-[11px] text-muted-foreground tabular-nums"> |
| 69 | + {p.amount} |
| 70 | + </div> |
| 71 | + )} |
| 72 | + </> |
| 73 | + ) : ( |
| 74 | + <span className="text-xs font-medium text-amber-600 dark:text-amber-500"> |
| 75 | + {t("readFailed")} |
| 76 | + </span> |
| 77 | + )} |
| 78 | + </div> |
| 79 | + </li> |
| 80 | + ))} |
| 81 | + </ul> |
| 82 | + |
| 83 | + <div className="flex items-baseline justify-between gap-2.5 border-t pt-3"> |
| 84 | + <span className="text-xs text-muted-foreground">{t("total")}</span> |
| 85 | + <span className="font-mono text-sm font-semibold tabular-nums"> |
| 86 | + {fiat(defi.knownUsd)} |
| 87 | + {!defi.complete && <span className="text-amber-600 dark:text-amber-500"> *</span>} |
| 88 | + </span> |
| 89 | + </div> |
| 90 | + {!defi.complete && ( |
| 91 | + <p className="text-[11px] text-amber-600 dark:text-amber-500">{t("incomplete")}</p> |
| 92 | + )} |
| 93 | + </CardContent> |
| 94 | + </Card> |
| 95 | + ); |
| 96 | +} |
0 commit comments