Before this, liquidate() paid every liquidator the exact same flat liquidationBonusBps regardless of how underwater a position was -- no extra incentive to rush the positions most likely to become unrecoverable bad debt over barely-underwater ones. Added an immutable liquidationBonusMaxBps + _dynamicLiquidationBonusBps(hf): the bonus linearly interpolates from the base rate (healthFactor == 1.0) up to the max (healthFactor <= 0.5), clamped at both ends. Setting max == base disables the curve entirely (flat, the exact pre-existing behavior) -- every pre-existing liquidation test passes unchanged under that default. Exposed liquidationBonusBpsFor(user) for bots/dashboards to read the real current rate without simulating liquidate(). Found and fixed a real bug within this same pass: liquidate()'s forward seize calculation picked up the new dynamic bonus, but the same function's collateral-exhausted branch still back-calculated coveredValueUsd off the old flat rate -- the two would have silently diverged for exactly the case most likely to hit that branch. Verified two ways: forge test adds 5 regression tests (constructor validation, flat-curve backward compatibility, exact closed-form interpolation/clamp-to-max math, a real liquidate() seizing the predicted amount) -- 52 -> 57/57 pass, zero regressions. Live on the persistent self-hosted demo chain (130.61.180.42:8647): deployed a separate vault, read liquidationBonusBpsFor at three real price points (500/757/2000bps exactly matching the closed-form math), then executed a real liquidate() tx seizing exactly 400 WQIE at the clamped 20% max bonus. Redeployed the live public demo (DeployDemo.s.sol now passes liquidationBonusMaxBps too) and updated the 5 affected NEXT_PUBLIC_QIE_FORGE_* Vercel address env vars -- in the process found and fixed a real latent bug where `echo | vercel env add` had been baking a trailing newline into every one of those address values, breaking vault-position reads until re-added with `printf '%s'`. Re-verified /, /swap, /pool, /vault all 200, /api/quote and /api/vault-position match every prior documented demo number exactly, plus the new liquidationBonusBps field. Re-ran scripts/liquidation-bot-demo.sh end-to-end against the updated deploy script: real on-chain bonus came back as 5.60% (above the 5% floor, correctly reflecting a barely- underwater 0.98 health factor), not the old flat 5%. Surfaced the new per-user bonus on the frontend's vault-position API and VaultCard (a real risk-dashboard improvement: "Liquidation bonus right now" next to health factor, shown once a position is actually underwater). npm test (33/33), next build, npx eslint . all clean. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
72 lines
3.2 KiB
TypeScript
72 lines
3.2 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { isAddress } from "viem";
|
|
import { getPublicClient } from "@/lib/viemClient";
|
|
import { getVaultAddress } from "@/lib/config";
|
|
import { VAULT_ABI } from "@/lib/contracts";
|
|
|
|
export async function GET(req: NextRequest) {
|
|
const vaultAddress = getVaultAddress();
|
|
if (!vaultAddress) {
|
|
return NextResponse.json({ error: "NOT_DEPLOYED", message: "No vault configured for this network yet." }, { status: 503 });
|
|
}
|
|
|
|
const user = new URL(req.url).searchParams.get("user");
|
|
if (!user || !isAddress(user)) {
|
|
return NextResponse.json({ error: "INVALID_REQUEST", message: "A valid `user` address is required" }, { status: 400 });
|
|
}
|
|
|
|
try {
|
|
const client = getPublicClient();
|
|
const [
|
|
position,
|
|
healthFactor,
|
|
currentDebt,
|
|
lenderShares,
|
|
totalLenderShares,
|
|
totalAssets,
|
|
totalDebtPrincipal,
|
|
liquidationBonusBps,
|
|
] = await Promise.all([
|
|
client.readContract({ address: vaultAddress, abi: VAULT_ABI, functionName: "positions", args: [user] }),
|
|
client.readContract({ address: vaultAddress, abi: VAULT_ABI, functionName: "healthFactor", args: [user] }),
|
|
client.readContract({ address: vaultAddress, abi: VAULT_ABI, functionName: "currentDebt", args: [user] }),
|
|
client.readContract({ address: vaultAddress, abi: VAULT_ABI, functionName: "lenderShares", args: [user] }),
|
|
client.readContract({ address: vaultAddress, abi: VAULT_ABI, functionName: "totalLenderShares" }),
|
|
client.readContract({ address: vaultAddress, abi: VAULT_ABI, functionName: "totalAssets" }),
|
|
client.readContract({ address: vaultAddress, abi: VAULT_ABI, functionName: "totalDebtPrincipal" }),
|
|
// Severity-scaled liquidation bonus this exact position would pay a
|
|
// liquidator right now (QieForgeVault.liquidationBonusMaxBps) — the
|
|
// real, current incentive rate, not the old flat
|
|
// `liquidationBonusBps` the frontend never actually surfaced before
|
|
// this feature existed.
|
|
client.readContract({
|
|
address: vaultAddress,
|
|
abi: VAULT_ABI,
|
|
functionName: "liquidationBonusBpsFor",
|
|
args: [user],
|
|
}),
|
|
]);
|
|
const [collateral, debtPrincipal, lastAccrual] = position as [bigint, bigint, bigint];
|
|
|
|
return NextResponse.json({
|
|
collateral: collateral.toString(),
|
|
debtPrincipal: debtPrincipal.toString(),
|
|
lastAccrual: lastAccrual.toString(),
|
|
healthFactor: (healthFactor as bigint).toString(),
|
|
currentDebt: (currentDebt as bigint).toString(),
|
|
// Lender-side pool stats (see LendCard.tsx) — global, not per-user,
|
|
// except lenderShares which is this user's own share balance.
|
|
lenderShares: (lenderShares as bigint).toString(),
|
|
totalLenderShares: (totalLenderShares as bigint).toString(),
|
|
totalAssets: (totalAssets as bigint).toString(),
|
|
totalDebtPrincipal: (totalDebtPrincipal as bigint).toString(),
|
|
liquidationBonusBps: (liquidationBonusBps as bigint).toString(),
|
|
});
|
|
} catch (err) {
|
|
return NextResponse.json(
|
|
{ error: "READ_FAILED", message: err instanceof Error ? err.message : "Unknown error" },
|
|
{ status: 502 },
|
|
);
|
|
}
|
|
}
|