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.
64 lines
2.0 KiB
TypeScript
64 lines
2.0 KiB
TypeScript
import { encodeFunctionData } from "viem";
|
|
import { ERC20_ABI } from "./contracts";
|
|
import type { Eip1193Provider } from "./wallet";
|
|
|
|
/**
|
|
* Shared approve+send helpers for every wallet-signed write in this app
|
|
* (swap, vault deposit/withdraw/borrow/repay, lender supply/withdraw).
|
|
* Extracted once both the swap flow and the vault flow needed the exact
|
|
* same "check allowance, approve if short, then call" sequence, instead of
|
|
* re-deriving it per component.
|
|
*/
|
|
|
|
/**
|
|
* Reads the current on-chain allowance and sends an `approve` transaction
|
|
* first if it is insufficient for `amount`. Returns `true` if an approval
|
|
* transaction was actually sent, so callers can surface a "waiting for
|
|
* approval" status before the follow-up call. Approves the exact amount
|
|
* needed, never an unlimited allowance.
|
|
*/
|
|
export async function ensureAllowance(
|
|
provider: Eip1193Provider,
|
|
token: `0x${string}`,
|
|
owner: `0x${string}`,
|
|
spender: `0x${string}`,
|
|
amount: bigint,
|
|
): Promise<boolean> {
|
|
const allowanceData = encodeFunctionData({
|
|
abi: ERC20_ABI,
|
|
functionName: "allowance",
|
|
args: [owner, spender],
|
|
});
|
|
const allowanceRaw = (await provider.request({
|
|
method: "eth_call",
|
|
params: [{ to: token, data: allowanceData, from: owner }, "latest"],
|
|
})) as string;
|
|
const allowance = BigInt(allowanceRaw);
|
|
if (allowance >= amount) return false;
|
|
|
|
const approveData = encodeFunctionData({
|
|
abi: ERC20_ABI,
|
|
functionName: "approve",
|
|
args: [spender, amount],
|
|
});
|
|
await provider.request({
|
|
method: "eth_sendTransaction",
|
|
params: [{ from: owner, to: token, data: approveData }],
|
|
});
|
|
return true;
|
|
}
|
|
|
|
/** Sends a plain `eth_sendTransaction` with pre-encoded call data, returning the tx hash. */
|
|
export async function sendContractCall(
|
|
provider: Eip1193Provider,
|
|
from: `0x${string}`,
|
|
to: `0x${string}`,
|
|
data: `0x${string}`,
|
|
): Promise<string> {
|
|
const txHash = await provider.request({
|
|
method: "eth_sendTransaction",
|
|
params: [{ from, to, data }],
|
|
});
|
|
return String(txHash);
|
|
}
|