{
 "item": "stable-exits-liquidity",
 "commit": "ff1c192b3798542997f600091f2dc73ff05e4c40",
 "forge": "forge Version: 1.8.3 (older than 1.9: --use solar refused, solc 0.8.38 already installed was used; tests compiled with --match-path test/unit/ItemSEL.t.sol on the repo's StableKit rig: TroubleVault = OZ ERC4626 mock, 100M other depositors, share rate made non-1 by a donation as sUSDS's is)",
 "verdict": "findings",
 "findings": [
  {
   "id": "SEL-1",
   "severity": "low",
   "title": "quoteRedeem promises 1 wei more than redeem pays for any exit that pulls from a vault with a non-1 share rate: redeem re-prices on the post-pull snapshot, which the vault's withdraw rounding (shares rounded up, previewRedeem rounded down) has cut by 1 unit of liquid and reserve; minOut = quote reverts Slippage",
   "property": "P4 (quoteRedeem and maxRedeem promise exactly what redeem pays in the same state)",
   "claim": "C3",
   "files": [
    "src/peg/StableModule.sol:243-253",
    "src/peg/StableModule.sol:265",
    "src/peg/StableModule.sol:472-479",
    "src/peg/StableModule.sol:719-728",
    "src/peg/ERC4626Adapter.sol:39-48"
   ],
   "sequence": "USDS leg with 30k of 40k in the vault (sUSDS-like share rate != 1). Same block: anyone calls quoteRedeem(usds, amt) for any amt > idle (an exit that needs a pull) -> q; then redeem(usds, amt, minOut=q, to). redeem pulls need = amt/scale - idle (line 248), re-snaps (line 249): the withdraw burned ceil(need) shares, so adapter.assets()/liquid() read 1 unit under (before - need); liquidWad (line 253) is 1 unit lower than the view's, the integral fee is higher and _exitOut returns q - 1.",
   "expected": "redeem(usds, amt, q, alice) pays q (P4: the quote is exactly what redeem pays in that state).",
   "actual": "amountIn 30090000000000000008180: quote 30041857544185449053999, redeem out 30041857544185449053998; with minOut = quote the exit reverts Slippage(q - 1). Fuzz: the first counterexample (8181) fails; it reproduces in the concrete test. Pull leaves liquid and reserve each 1 wei lower than the view assumed (logged). maxRedeem itself stays executable (testFuzz_P4_maxRedeemAnyRate, 256 runs, share rate up to +1e9 USDS): the slack is the fee rounding, so only the quote's out is off.",
   "reachableAtDeploy": true,
   "fix": "After a pull that succeeded, price the exit on the pre-pull liquid (liquidWad from the first _snap) and keep only the caps on the post-pull snapshot (out <= s.idle, _most), so the fee no longer moves with the vault's rounding; or have _exitState subtract one stable unit of vault liquid when amountIn/scale > idle and document the quote as a lower bound for pulls. Loss is dust (1 unit per pull, borne by the exiter), the practical harm is a quote used as minOut reverting.",
   "test": {
    "source": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.38;\n\nimport {StableBase, StableViews} from \"../utils/StableKit.sol\";\nimport {StableModule} from \"../../src/peg/StableModule.sol\";\nimport {IERC20} from \"@openzeppelin/contracts/token/ERC20/IERC20.sol\";\n\ncontract ItemSELTest is StableBase {\n    using StableViews for StableModule;\n\n    function setUp() public {\n        _setUpStable();\n        _mint(alice, address(usds), 40_000e18);\n        _mint(alice, address(usdc), 40_000e6);\n        // a share rate that is not 1 (sUSDS-like): withdraw burns ceil shares, previewRedeem floors\n        vault.accrue(31_415_926_535_897_932_384_626);\n        mod.rebalance(address(usds)); // 30k into the vault\n        _pass(1 days);\n    }\n\n    function testFuzz_P4_quoteEqRedeem_pull(uint256 amt) public {\n        uint256 idle = usds.balanceOf(address(mod));\n        amt = bound(amt, idle + 1, mod.maxRedeem(address(usds)));\n        (uint256 q,) = mod.quoteRedeem(address(usds), amt);\n        vm.prank(alice);\n        try mod.redeem(address(usds), amt, 0, alice) returns (uint256 out) {\n            assertEq(out, q, \"redeem pays the quote\");\n        } catch {\n            assertEq(q, 0, \"quoted an exit redeem refuses\");\n        }\n    }\n\n    function test_P4_quoteAsMinOutReverts() public {\n        uint256 idle = usds.balanceOf(address(mod));\n        uint256 amt = bound(8181, idle + 1, mod.maxRedeem(address(usds)));\n        (uint256 q,) = mod.quoteRedeem(address(usds), amt);\n        uint256 l0 = mod.liquid(address(usds));\n        uint256 r0 = mod.reserve(address(usds));\n        uint256 s0 = vm.snapshotState();\n        vm.prank(alice);\n        uint256 out = mod.redeem(address(usds), amt, 0, alice);\n        emit log_named_uint(\"amountIn\", amt);\n        emit log_named_uint(\"quote\", q);\n        emit log_named_uint(\"redeem out\", out);\n        vm.revertToState(s0);\n        // what the pull leaves: liquid and reserve after pulling the exit's need, before pricing\n        vm.prank(address(mod));\n        adapter.withdraw(amt - idle, address(mod));\n        emit log_named_uint(\"liquid lost to the pull's rounding\", l0 - mod.liquid(address(usds)) - 0);\n        emit log_named_uint(\"reserve lost to the pull's rounding\", r0 - mod.reserve(address(usds)));\n        vm.revertToState(s0);\n        vm.prank(alice);\n        vm.expectRevert(abi.encodeWithSelector(StableModule.Slippage.selector, q - 1));\n        mod.redeem(address(usds), amt, q, alice);\n    }\n}\n",
    "ran": "forge test --offline --match-path test/unit/ItemSEL.t.sol --match-test P4 -vv (forge 1.8.3, solc 0.8.38, via_ir)",
    "result": "[FAIL: redeem pays the quote: 30041857544185449053998 != 30041857544185449053999; counterexample: args=[8181]] testFuzz_P4_quoteEqRedeem_pull(uint256). [PASS] test_P4_quoteAsMinOutReverts() logs: amountIn 30090000000000000008180, quote 30041857544185449053999, redeem out 30041857544185449053998, liquid lost to the pull's rounding 1, reserve lost to the pull's rounding 1; expectRevert Slippage(q-1) with minOut = quote holds."
   }
  }
 ],
 "claims": [
  {
   "id": "C1",
   "result": "holds",
   "evidence": "No inflation/donation attack found. The module issues no shares: imdUSD is minted 1:1 less fee and an exit pays 1:1 less fee, so a donation to the module is owned value P that has no way out (StableModule.sol:20-23, no withdraw). testFuzz_C1_donation (256 runs: donor gives 1..1e6 USDC to the module in front of a depositor, tries to exit past its own imdUSD): donor never recovers it, the victim's mint+exit round trip loses at most the fees. Vault shares: ERC4626Adapter.deposit refuses unless vault.totalAssets() >= 100 x (module's assets + amount) (ERC4626Adapter.sol:17,53) and day 1 lists only sUSDS (script/DeployConfig.sol:233; Base and 4663 legs have no vault, :577, :776). A donation to the adapter is not counted: the vault reads min(booked, assets()) (StableModule.sol:601) and gains enter only through harvest under maxYieldBps (793-815). Note (not tested, governance listing): MIN_DEPTH measures totalAssets, which a donor can raise, not totalSupply, so it would not by itself stop a classic inflation on a fresh vault governance chose to list; rebalance's WouldShort (362) then bounds any loss to the leg's owned value."
  },
  {
   "id": "C2",
   "result": "holds",
   "evidence": "Confirmed as described and bounded. A short pay found by an exit leaves `short` computed on the pre-pull snapshot (StableModule.sol:238) so the exit is priced with f0 = feeMin on the idle alone (253-255), not feeMax. testFuzz_C2_shortPayBound (256 runs, shortBy 1..5000 USDS, exits idle+1..idle+25k): the undercharge against feeMax pricing is <= (feeMax - feeMin) x out (observed ~50 USDS on a 25k exit at the deploy curve 0.10%->0.50%), the leg is halted and its share cut to 0 by _pull (783-787), and a second exit does not call the vault again (booked unchanged), so it is one undercharge per broken vault; _most (269-272) still keeps R >= the remaining outstanding. Bound: <= (feeMax - feeMin) x the one exit's out <= 0.99% of the leg's idle (MAX_EXIT_FEE, line 65)."
  },
  {
   "id": "C3",
   "result": "refuted",
   "evidence": "A state exists (finding SEL-1): any successful pull from a vault whose share rate is not 1 (sUSDS's is not) makes redeem pay 1 wei less than quoteRedeem. In the named states the views match: testFuzz_P4_states (256 runs over yield cut, vault dip/loss, gate on liquid, close/halt, a matured SET_CURVE with any valid curve applied by a stranger in front, and after a refusal already met) found quote == out, quote 0 for every exit redeem refuses, nothing paid past maxRedeem; the freeze case is covered by the repo's FRStableFrozenViews test and README:865-866. The first refusing exit itself necessarily differs from the quote (a view cannot pull; README:640 covers its 0-return). maxRedeem's own exit executes at any share rate (testFuzz_P4_maxRedeemAnyRate, test_P4_maxRedeemExecutes)."
  },
  {
   "id": "C4",
   "result": "not-checked",
   "evidence": "[fork] NOT RUN: no fork RPC used in this run (no USDG 4663 / Base forks at this commit). The mock-side behaviour is what the tests above exercise."
  }
 ],
 "holds": [
  {
   "property": "P1 (exit at 1 - f while liquid; idle serves; refusing vault never blocks idle; never pays more than liquid)",
   "tried": "testFuzz_P1_stuckVaultIdleServes (256 runs, vault withdraw reverting, exits 1..idle USDS, 20M gas): always served, R >= minted after. testFuzz_P1_refusalIdleMaxIn (256 runs): every exit up to the idle-only maxRedeem (measured under lowerYield(1,0) in a snapshot) is served when the vault refuses the pull first (the pull is attempted because amountIn/scale > idle counts the fee). testFuzz_P4_states asserts out <= amountIn and out >= amountIn/1.01 - dust on every paid exit, any valid curve. Code: out <= s.idle unfrozen (266), out from _exitOut <= liquid (553), frozen paid from vault only. PullGas below 5.25M gas is README's PULL_GAS bullet (not a block: resend with more gas)."
  },
  {
   "property": "P2 (no mint/redeem sequence across legs, blocks or a share-rate move extracts more than fees allow)",
   "tried": "testFuzz_P2_noExtraction (256 runs x 8 actions): any order of mint USDC/USDS, redeem either leg, rebalance, vault accrue (share-rate rise up to 1M USDS), harvest and time jumps up to 2 days; bob's USDC+USDS+imdUSD value never rises, both legs keep R x scale >= minted. Analytically the round trip on one leg pays the integral of g + f >= min(feeMax, feeMin + mintFeeMax) > 0 per unit; a rise of the share rate is recognised only under maxYieldBps (793-815), a fall counts at once (601). Cross-leg mint-free-then-exit pays feeMin by design (lines 26-27, 35-38)."
  },
  {
   "property": "P3 (rounding favours the module)",
   "tried": "testFuzz_P3_slices (256 runs, 6-decimal USDC leg, 2..12 slices of up to 30k): slices never pay more than one exit, and R x 1e12 >= minted after every slice. Code: _exitFee rounds up (506, 511: ceil minus floor), out floors by scale (265), _exitOut verifies against the exact rounded cost (555-567), _most ceils what stays (740), mint fee rounds up (518-521) and out floors (710). The 1-wei pull rounding of SEL-1 is carried by the exiter (out lower), not the leg."
  },
  {
   "property": "P4 (views exactly as redeem)",
   "tried": "Fails by 1 wei on pull exits (SEL-1). Holds otherwise in testFuzz_P4_states, testFuzz_P4_maxRedeemAnyRate, test_P4_maxRedeemExecutes (see C3)."
  },
  {
   "property": "Writers/readers of the state the properties name",
   "tried": "Writers of minted/booked/yieldBps/idle (src only; script/launch/SafeBatch.s.sol:263-280 only reads leg()): mint 197-225, redeem 229-289 (minted, booked via _pull), poke 292-327 (_report, restore), rebalance 330-365, harvest/_harvest 368, 793-815, lowerYield 395-402, reportLoss 406-410, abandonVault 418-428, recover 433-441 (idle), _pull 763-789, _apply SET_CURVE/SET_YIELD/REOPEN 942-1002. No other contract in src/ calls the module's redeem/quoteRedeem/maxRedeem. Routes beyond the claims tested: rebalance and harvest interleaved with exits (P2 fuzz), a stranger's applyPending(SET_CURVE) in front of an exit (P4 states), close/lowerYield before exits, a refusal then the views."
  }
 ],
 "notRun": [
  "C4 [fork]: USDG (4663 ~128,334,220) and Base USDC (~65,212,173) fork suites NOT RUN at this commit",
  "Invariant (stateful) campaigns: property tests here are stateless fuzz over action sequences (256 runs each)"
 ]
}