qie-forge/apps/web/lib/config.ts
caelum0x 36ee94e427 fix: close a real lender-share donation/inflation exploit, ship vault borrow/repay/supply UI, fix wallet chain-ID + stale demo oracle
Found by actually using the live site and testing against the real deployed
demo contract, not just reading code:

- QieForgeVault.deposit() minted a first lender's shares against their own
  amount alone, ignoring any totalAssets() already in the vault before
  totalLenderShares existed -- exactly the state DeployDemo.s.sol's own
  seeding pattern creates. Reproduced live: a 1,000 TUSD first deposit into
  the pre-funded demo vault minted shares worth 501,000 TUSD (100% of
  totalAssets, including 500,000 pre-funded liquidity + 20,000 outstanding
  debt) -- the textbook ERC4626 donation/inflation attack, no mitigation
  unlike QieForgePair's MINIMUM_LIQUIDITY burn. Fixed with an equivalent
  MINIMUM_LENDER_SHARES floor locked on first deposit into a non-pristine
  vault; forge test 41/41 -> 43/43 with two new regression tests; re-verified
  live against a freshly redeployed demo stack (same documented 500,000/
  20,000/2.8x numbers) that the same attack now round-trips exactly.
- SwapCard/VaultCard's own "Connect Wallet" buttons hardcoded chainId 1983
  (QIE Testnet) regardless of the actually configured network, so a real
  wallet user on the live demo (chain 31337) would connect to the wrong
  chain. Fixed to derive chainId from getNetworkConfig(), same pattern the
  header's WalletConnectButton already used correctly.
- The live demo oracle's mock price feed staleness window (1 hour) had
  elapsed since deploy, so borrow()/healthFactor() were reverting for
  everyone including the documented demo position. Fixed live via
  setMaxStaleness() and in DeployDemo.s.sol for future redeploys.

Shipped the actual missing feature that prompted this pass: /vault only
ever exposed depositCollateral() in the UI, never borrow/repay/withdraw-
collateral or the lender deposit()/withdraw() that have existed in the
contract and SDKs since f55e3c6. Added those to VaultCard plus a new
LendCard (supply/withdraw liquidity, live pool stats), a shared
erc20Actions.ts approve-then-send helper, and verified every new handler's
exact encoding against the live redeployed contract with cast send/call.

Also fixed infra: the self-hosted demo RPC's Caddy site had silently
dropped out of sites-enabled a second time (not in the box's managed
caddy/sites/ dir or CADDY_SITES) -- added it for real this time in the
infra-box repo instead of hand-copying into /etc/caddy/ again.
2026-10-28 21:12:06 +03:00

71 lines
2.8 KiB
TypeScript

import { resolveNetwork, type QieNetworkName } from "@qie-forge/core";
/**
* All contract addresses here are deliberately env-driven with NO
* hardcoded default — matching this project's fail-closed posture
* everywhere else (QusdcClient, QieOracleAdapter). Nothing is deployed to
* the live QIE testnet/mainnet yet (see docs/STATUS.md: blocked on a
* funded deployer wallet, an owner/operator step). The UI reflects that
* honestly — Swap/Vault show a clear "not deployed on this network yet"
* state instead of pointing at a placeholder address.
*/
export function getNetworkName(): QieNetworkName {
const value = process.env.NEXT_PUBLIC_QIE_FORGE_NETWORK;
if (value === "mainnet") return "mainnet";
if (value === "demo") return "demo";
return "testnet";
}
export function getNetworkConfig() {
return resolveNetwork(getNetworkName(), {
rpcUrl: process.env.NEXT_PUBLIC_QIE_FORGE_RPC_URL || undefined,
});
}
export function getFactoryAddress(): `0x${string}` | undefined {
return (process.env.NEXT_PUBLIC_QIE_FORGE_FACTORY_ADDRESS as `0x${string}` | undefined) || undefined;
}
export function getRouterAddress(): `0x${string}` | undefined {
return (process.env.NEXT_PUBLIC_QIE_FORGE_ROUTER_ADDRESS as `0x${string}` | undefined) || undefined;
}
export function getVaultAddress(): `0x${string}` | undefined {
return (process.env.NEXT_PUBLIC_QIE_FORGE_VAULT_ADDRESS as `0x${string}` | undefined) || undefined;
}
export function getVaultCollateralSymbol(): string {
return process.env.NEXT_PUBLIC_QIE_FORGE_VAULT_COLLATERAL_SYMBOL || "Collateral";
}
/** The vault's borrow/quote asset — distinct from the collateral asset
* above. Undefined (fail-closed, same posture as every other address in
* this file) until a real deployment sets it; the borrow/repay UI in
* VaultCard and the whole LendCard stay disabled without it. */
export function getVaultBorrowAddress(): `0x${string}` | undefined {
return (process.env.NEXT_PUBLIC_QIE_FORGE_VAULT_BORROW_ADDRESS as `0x${string}` | undefined) || undefined;
}
export function getVaultBorrowDecimals(): number {
return Number(process.env.NEXT_PUBLIC_QIE_FORGE_VAULT_BORROW_DECIMALS ?? 18);
}
export function getVaultBorrowSymbol(): string {
return process.env.NEXT_PUBLIC_QIE_FORGE_VAULT_BORROW_SYMBOL || "the quote asset";
}
/** Comma-separated `SYMBOL:address:decimals` list, e.g. "WQIE:0xabc...:18,TUSD:0xdef...:6". */
export function getSwapTokens(): { symbol: string; address: `0x${string}`; decimals: number }[] {
const raw = process.env.NEXT_PUBLIC_QIE_FORGE_SWAP_TOKENS;
if (!raw) return [];
return raw
.split(",")
.map((entry) => entry.trim())
.filter(Boolean)
.map((entry) => {
const [symbol, address, decimals] = entry.split(":");
return { symbol, address: address as `0x${string}`, decimals: Number(decimals || 18) };
});
}