qie-forge/apps/web/components/VaultPoolStatsBar.tsx
caelum0x 366e8c2845 feat: aggregate borrow cap on QieForgeVault, independent of per-position LTV
Per-position ltvBps was the only check on how much a vault could lend
out in aggregate -- a well-collateralized borrower (or several) could
grow totalDebtPrincipal arbitrarily large with nothing else gating it,
even though real risk management (oracle feed liquidity depth, how much
collateral could actually be sold to cover simultaneous liquidations)
bounds how large a single market can safely get. Adds an immutable
borrowCap (0 = unlimited, exact pre-existing behavior) that borrow()
checks against aggregate debt, same primitive Compound/Aave's per-market
borrow caps already ship.

- 5 new regression tests (sentinel disables feature, exact boundary,
  aggregate across multiple borrowers, interest accrual never blocked):
  57 -> 62/62 pass, zero regressions.
- New one-command scripts/borrow-cap-demo.sh proves the real boundary
  against real deployed bytecode on a disposable anvil.
- Verified live on the persistent self-hosted demo chain
  (http://130.61.180.42:8647) against a separate throwaway vault so the
  public demo's own position was never touched: borrow() landing exactly
  on a 1,000 TUSD cap succeeded (tx 0xc71dd5c4...), the next unit over
  reverted with the real BorrowCapExceeded custom error.
- Redeployed the public demo with a real 100,000 TUSD cap and surfaced
  it on /vault via a new "Borrow cap" stat, no wallet required.
2026-10-29 09:35:15 +03:00

69 lines
2.8 KiB
TypeScript

import { formatUnits } from "viem";
import type { VaultPoolStats } from "@/lib/vaultStats";
import { CoinsIcon } from "./icons";
/**
* Always-visible pool-level stats for `/vault` — TVL, outstanding debt,
* idle liquidity, utilization, lender count proxy (total shares). Rendered
* server-side, with NO wallet connection required, same honest-live-data
* posture as the homepage's network/oracle cards and `/swap`'s quote box.
*
* Deliberately separate from `VaultCard`/`LendCard`'s own per-user stats
* (collateral, debt, health factor, your shares) — those genuinely need a
* connected address. These four numbers do not, and gating them behind
* "Connect Wallet" (the state before this fix) meant anyone without a
* browser wallet extension saw nothing at all on `/vault`.
*/
export function VaultPoolStatsBar({
stats,
borrowDecimals,
borrowSymbol,
}: {
stats: VaultPoolStats;
borrowDecimals: number;
borrowSymbol: string;
}) {
const totalAssets = BigInt(stats.totalAssets);
const totalDebtPrincipal = BigInt(stats.totalDebtPrincipal);
const totalReserves = BigInt(stats.totalReserves);
const borrowCap = BigInt(stats.borrowCap);
const idleLiquidity = totalAssets >= totalDebtPrincipal ? totalAssets - totalDebtPrincipal : 0n;
const utilizationBps = totalAssets > 0n ? (totalDebtPrincipal * 10000n) / totalAssets : 0n;
// See QieForgeVault.sol's `borrowCap`: 0 means no aggregate ceiling is
// configured for this vault, distinct from "0 room left to borrow".
const borrowCapValue =
borrowCap === 0n ? "Unlimited" : `${formatUnits(totalDebtPrincipal, borrowDecimals)} / ${formatUnits(borrowCap, borrowDecimals)} ${borrowSymbol}`;
return (
<div className="card p-5 mb-6 grid grid-cols-2 sm:grid-cols-6 gap-4">
<Stat
icon
label="Total value locked"
value={`${formatUnits(totalAssets, borrowDecimals)} ${borrowSymbol}`}
/>
<Stat label="Idle liquidity" value={`${formatUnits(idleLiquidity, borrowDecimals)} ${borrowSymbol}`} />
<Stat
label="Outstanding debt"
value={`${formatUnits(totalDebtPrincipal, borrowDecimals)} ${borrowSymbol}`}
/>
<Stat label="Utilization" value={`${(Number(utilizationBps) / 100).toFixed(2)}%`} />
<Stat
label="Protocol reserves"
value={`${formatUnits(totalReserves, borrowDecimals)} ${borrowSymbol}`}
/>
<Stat label="Borrow cap" value={borrowCapValue} />
</div>
);
}
function Stat({ icon, label, value }: { icon?: boolean; label: string; value: string }) {
return (
<div>
<p className="text-xs text-muted flex items-center gap-1">
{icon && <CoinsIcon className="w-3.5 h-3.5" />} {label}
</p>
<p className="mt-1 text-lg font-semibold text-foreground break-all">{value}</p>
</div>
);
}