{
 "item": "stable-deposits-caps",
 "commit": "ff1c192b3798542997f600091f2dc73ff05e4c40",
 "forge": "forge 1.8.3 (older than 1.9: --use solar unavailable; ran with the repo's solc 0.8.38, via_ir, --match-path test/scratch/*). Fuzz/seq tests in test/scratch reuse test/utils/StableKit.sol (StableBase: real MinterHub, StableModule, ERC4626Adapter over a TroubleVault mock).",
 "verdict": "findings",
 "findings": [
  {
   "id": "SDC-1",
   "severity": "low",
   "title": "rebalance's vault guard is skipped while nothing is booked: the first deposit into a fresh or emptied vault (incl. a successor applied by anyone) goes into a vault that releases nothing; and 1 wei released counts as paying",
   "property": "P1 (and C2: 'rebalance guards the vault, not only the leg')",
   "claim": "C2",
   "files": [
    "src/peg/StableModule.sol:347",
    "src/peg/StableModule.sol:593",
    "src/peg/StableModule.sol:705",
    "src/peg/StableModule.sol:984"
   ],
   "sequence": "(a) A leg with an adapter and booked == 0 (every leg opens empty at seed 0; or after SET_YIELD's apply sets (adapter, booked) = (a, 0) at :984; or after a rebalance/pull empties booked). The vault accepts deposits but releases nothing (maxWithdraw 0: a queue, a paused withdraw, a lending vault at 100% utilisation). Anyone mints, anyone calls rebalance: _snap (:593) only reads the adapter when booked != 0, so s.vault = 0 and _sound(:705) is vacuously true and (booked - vault)*1e6 > booked is 0 > 0; 30,000 of 40,000 moved into the vault (test 1). (b) Same through governance ordering: the Safe proposes SET_YIELD to successor B; during the 48 h B stops paying; in one block a stranger applyPending(SET_YIELD|1) then rebalance: 30,000 into B, liquid() 0 (test 2). (c) With booked > 0, a vault releasing 1 wei passes `s.vaultLiquid != 0`: booked 30,000 -> 36,750 (test 3). Fuzz (test 4): any first mint 14k-50k, any gate 0-50k, any delay, any caller: deposit > what the vault can pay back.",
   "expected": "No call moves reserve into a vault that cannot pay it (C2; README:660-661 says rebalance never deposits into a vault that 'releases nothing now').",
   "actual": "Deposit of 30,000e18 into a vault with liquid() == 0; minting on the leg closes (_sound false) and exits are priced on idle only: maxRedeem fell to 10,042e18 of 40,000 minted, so 75% of the leg's reserve is locked until the vault pays. README:660 covers only a vault 'that holds part of the reserve'; a vault holding none (booked 0) and one paying 1 wei are not covered.",
   "reachableAtDeploy": false,
   "fix": "After the deposit in rebalance, re-snap and require the vault to release what was just put in (e.g. `s = _snap(l); if (s.vaultLiquid < dep) revert Impaired();` or require liquid() >= booked x some ratio), so the guard reads the vault in every case, including booked == 0; or probe IYieldAdapter(a).liquid() after deposit. Day 1: only sUSDS (USDS leg, yieldBps 7,500), whose withdraw does not queue, so not reachable at deploy config; reachable for any vault SET_YIELD lists later, by any account ordering apply + rebalance.",
   "test": {
    "source": "// SPDX-License-Identifier: MIT\npragma solidity 0.8.38;\nimport {IERC20} from \"@openzeppelin/contracts/token/ERC20/IERC20.sol\";\nimport {IERC4626} from \"@openzeppelin/contracts/interfaces/IERC4626.sol\";\nimport {StableBase, StableViews, TroubleVault} from \"../utils/StableKit.sol\";\nimport {StableModule} from \"../../src/peg/StableModule.sol\";\nimport {ERC4626Adapter} from \"../../src/peg/ERC4626Adapter.sol\";\n\ncontract SdcDet is StableBase {\n    using StableViews for StableModule;\n\n    function setUp() public {\n        _setUpStable();\n    }\n\n    /// C2 / P1: with nothing booked the vault is never read, so rebalance deposits into a vault that releases nothing.\n    function test_C2_firstDepositIntoVaultThatCannotPay() public {\n        vault.setGate(0); // the vault accepts deposits but releases nothing now (maxWithdraw 0)\n        _mint(alice, address(usds), 40_000e18);\n        assertEq(mod.leg(1).booked, 0);\n        vm.prank(stranger);\n        try mod.rebalance(address(usds)) returns (uint256 inVault) {\n            emit log_named_uint(\"deposited into a vault releasing 0\", inVault);\n            emit log_named_uint(\"adapter.liquid()\", adapter.liquid());\n            emit log_named_uint(\"idle left\", usds.balanceOf(address(mod)));\n            emit log_named_uint(\"maxRedeem now\", mod.maxRedeem(address(usds)));\n            assertFalse(mod.mintOpen(address(usds)), \"minting closed by the deposit\");\n            revert(\"P1/C2: reserve moved into a vault that cannot pay\");\n        } catch (bytes memory e) {\n            assertEq(bytes4(e), StableModule.Impaired.selector);\n        }\n    }\n\n    /// The same through a matured SET_YIELD applied by anyone, then anyone's rebalance, in one block.\n    function test_C2_maturedSuccessorAppliedThenDepositInSameBlock() public {\n        TroubleVault v2 = new TroubleVault(IERC20(address(usds)));\n        usds.mint(address(this), 100_000_000e18);\n        usds.approve(address(v2), type(uint256).max);\n        v2.deposit(100_000_000e18, address(0xbeef));\n        ERC4626Adapter b = new ERC4626Adapter(address(mod), IERC4626(address(v2)));\n        _mint(alice, address(usds), 40_000e18);\n        mod.rebalance(address(usds));\n        _propose(abi.encode(uint8(SET_YIELD | 1), address(b), uint16(7_500), uint16(2_500), uint128(10_000e18), uint16(2_000)));\n        _pass(48 hours);\n        v2.setGate(0); // the successor stops paying during the 48 h\n        vm.startPrank(stranger);\n        mod.applyPending(SET_YIELD | 1);\n        try mod.rebalance(address(usds)) returns (uint256 inVault) {\n            vm.stopPrank();\n            emit log_named_uint(\"deposited into successor releasing 0\", inVault);\n            emit log_named_uint(\"successor liquid()\", b.liquid());\n            revert(\"P1/C2: reserve moved into a successor vault that cannot pay\");\n        } catch (bytes memory e) {\n            vm.stopPrank();\n            assertEq(bytes4(e), StableModule.Impaired.selector);\n        }\n    }\n\n    /// C2 as written: a vault that releases 1 wei counts as releasing.\n    function test_C2_vaultReleasingOneWeiTakesMore() public {\n        _mint(alice, address(usds), 40_000e18);\n        mod.rebalance(address(usds));\n        vault.setGate(1);\n        _pass(1 days);\n        _mint(bob, address(usds), 9_000e18);\n        uint256 b0 = mod.leg(1).booked;\n        try mod.rebalance(address(usds)) {\n            emit log_named_uint(\"booked before\", b0);\n            emit log_named_uint(\"booked after\", mod.leg(1).booked);\n            revert(\"deposit into a vault releasing 1 wei\");\n        } catch (bytes memory e) {\n            assertEq(bytes4(e), StableModule.Impaired.selector);\n        }\n    }\n\n    /// C2 fuzzed: any first mint, any gate, any delay, any caller: a deposit must leave the vault able to pay it.\n    function testFuzz_C2_depositLeavesVaultAblePay(uint256 amt, uint256 gate, uint256 dt, uint256 who) public {\n        amt = bound(amt, 14_000e18, 50_000e18);\n        gate = bound(gate, 0, 50_000e18);\n        address w = [alice, bob, carol, stranger][who % 4];\n        vault.setGate(gate);\n        _mint(alice, address(usds), amt);\n        _pass(bound(dt, 0, 3 days));\n        vm.prank(w);\n        try mod.rebalance(address(usds)) returns (uint256 inVault) {\n            assertGe(adapter.liquid(), inVault, \"deposit into a vault that cannot pay it back\");\n        } catch {}\n    }\n\n}\n",
    "ran": "forge test --match-path test/scratch/SdcDet.t.sol --match-test C2 --fuzz-runs 1000 -vv",
    "result": "[FAIL: P1/C2: reserve moved into a vault that cannot pay] test_C2_firstDepositIntoVaultThatCannotPay() logs: deposited into a vault releasing 0: 30000e18, adapter.liquid(): 0, idle left: 10000e18, maxRedeem now: 10042e18 | [FAIL: P1/C2: reserve moved into a successor vault that cannot pay] test_C2_maturedSuccessorAppliedThenDepositInSameBlock() logs: deposited into successor releasing 0: 30000e18, successor liquid(): 0 | [FAIL: deposit into a vault releasing 1 wei] test_C2_vaultReleasingOneWeiTakesMore() logs: booked before 30000e18, booked after 36750e18 | [FAIL: deposit into a vault that cannot pay it back: 4145380716279525367043 < 17467773922824880238718] testFuzz_C2_depositLeavesVaultAblePay (runs: 1)"
   }
  }
 ],
 "claims": [
  {
   "id": "C1",
   "result": "holds",
   "evidence": "Every write of Leg.minted: mint :220 (raise, checked m > l.cap -> OverCap :219), redeem :280 (lower), poke :325 (raise, by hub.restore's return, bounded by c - m :324), _report :850 (lower), _addLeg (new leg, 0). No other writer in src/ or script/. poke's restore checks `good` (band, freeze) and _sound but not l.halted (:321): runs on a halted leg by design (README:676). test_C4_dipRestore: the leg stays halted while poke restores minted to its pre-dip value. Fuzz testFuzz_seq (3000 runs x 60 random steps) asserts any rise of minted ends <= cap: holds."
  },
  {
   "id": "C2",
   "result": "refuted",
   "evidence": "Finding SDC-1: the guard at :347 reads the vault only through _snap, which skips the adapter when booked == 0 (:593), and accepts vaultLiquid of 1 wei. Deposits of 30,000 into a vault releasing 0 (first deposit, and a successor applied by a stranger then rebalanced in the same block), and 6,750 more into a vault releasing 1 wei. With booked > 0 and a read under booked by > 1 ppm, or on a halted leg, the deposit reverts Impaired (fuzz testFuzz_seq P1 assertions: holds)."
  },
  {
   "id": "C3",
   "result": "holds",
   "evidence": "abandonVault :418-428 returns without effect when l.adapter != adapter (test: abandonVault(1, 0x1234) left the adapter listed) and never calls the adapter: with the adapter's code etched to INVALID (0xfe) the abandon succeeded, strandedOf set, leg halted, R >= minted (test_C3_abandonNamesAndNeverCalls PASS). It reads only the stable's balanceOf and calls hub.reportLoss. Note: it _clear()s a pending SET_YIELD even when it does nothing (:420) \u2014 a Safe-only act, not a finding."
  },
  {
   "id": "C4",
   "result": "holds",
   "evidence": "test_C4_dipRestore: a 2% read dip poked by a stranger booked 572.5e18 hub loss and halted the leg; the next poke after the read came back restored it (lossOf 0, minted back to its pre-dip value); the halt stays (as written, README:867). test_C4_dipPaidByOtherLeg: a stranger's poke of the OTHER leg (USDC) paid the USDS dip's 2.5e18 from USDC's owned value (29.48 -> 26.98) and the USDS leg then owns it (30.0): value moves between legs, the module total is unchanged, no loss sticks. No sequence in testFuzz_seq (dips, recoveries, redeems, lowerCaps, close, matured SET_CAPS/REOPEN applied by a stranger, same block and 1 s..2 d warps) left the module's row owing while both legs read whole except where the cap was lowered (Safe). sUSDS dip: reasoned only from README:867 (sUSDS previewRedeem is chi-based); [fork] NOT RUN."
  },
  {
   "id": "C5",
   "result": "holds",
   "evidence": "Computed (test_C5_borrowedCaps PASS): EthConfig.get borrowedCaps 36,000 = lines 26,000 + house 5,000 + fixed 3,000 + migrator 2,000 (lender 0); RobinhoodConfig.launch 9,000 = lines 4,000 + lender 5,000; BaseConfig.launch 5,000 = lines 5,000; total 50,000 = BOOTSTRAP; maxBorrowed(0) = 50,000. Module caps: eth 150,000 (USDC 100k + USDS 50k), 4663 50,000, Base 50,000. Bound on the keeper's min(minted, liquid) over the stables an exit can pay (keeper is off-chain, not in this commit; SafeBatch.liquidReserve script/launch/SafeBatch.s.sol:271 is the same formula): an unpoked fall reads correctly (liquid uses min(booked, assets)); an unreadable vault counts 0 in the module (_view) but liquidReserve calls assets() raw and reverts; a freeze reads 0 only when the flag returns exactly 1. Over-reads found: (i) an issuer PAUSE (USDC is pausable) is not a freeze flag: test_C5_issuerPauseReadsFullLiquid: 19,992 still counted while redeem reverts \u2014 up to the whole idle of the leg (<= leg cap: 100k eth USDC, 50k Base USDC); (ii) after a refusal cut or lowerYield(i,0) the vault's liquid part is counted while an exit larger than idle returns 0 until anyone's rebalance (test_C5_cutShareVaultCountedUntilRebalance: figure 39,972, 20k exit paid 0, after rebalance 19,978) \u2014 one public call; (iii) a deposit parked over the Safe's read: testFuzz_C5_parkedDeposit (500 runs, base 0-50k, deposit 1k-50k) round-trip cost <= 0.6% of the figure added; 49,949 added cost 102.10 USDC (0.204%). Each 1 parked adds 2.3 of borrowed room under the rule (README:695-697: Safe judges on the lowest value propose->apply). (iv) exit fees: payable <= liquid less <= feeMax (0.5% launch, 1% MAX_EXIT_FEE). Not a finding: the rule is a Safe/keeper procedure (README:678)."
  }
 ],
 "holds": [
  {
   "property": "P1 No call moves reserve into a halted leg's vault or one reading short",
   "tried": "Only rebalance :350-351 moves idle into an adapter (SET_YIELD apply :981 and recover :438 and _pull move out). testFuzz_seq 3000 runs x 60 steps (mint, redeem, poke, rebalance by 4 accounts; vault gain/loss/dip/gate; lowerCaps, close, SET_CAPS and REOPEN proposed and applied by a stranger; donations; feed out of band; warps 0 s/1 s/12 s/1 h/1 d/2 d): every step that grew the adapter's shares had the leg not halted, the vault read within 1 ppm of booked, R >= minted: holds for halted/short. Broken for 'cannot pay' only (SDC-1)."
  },
  {
   "property": "P2 No call raises minted_i past cap_i; a lowered cap stops both",
   "tried": "test_P2_lowerCapsStopsBoth: lowerCaps to minted-1 after a dip, poke after recovery restores nothing (c > m false :321), mint reverts OverCap. Fuzz asserts every rise ends <= cap, with lowerCaps to 50/100/150% of minted and matured SET_CAPS applied by a stranger in any order: holds. Mints served from held imdUSD (:223) do not raise minted. sum(minted) == hub row outstanding asserted after every step: holds."
  },
  {
   "property": "P3 poke's restore pays a row's loss from owned value only, never minting past the hub room",
   "tried": "Fuzz asserts after each poke: hub.usedOf(row) never rises, each leg's minted rise <= its pre-poke owned value; restore leaves used_s = outstanding+bad+covered unchanged (MinterHub.restore :213-233), covered mint-back stops on a frozen row. test_C4_dipPaidByOtherLeg: another leg's owned value pays, within that leg's cap room: holds."
  },
  {
   "property": "P4 launch peg rule caps total 50,000 (36,000 / 9,000 / 5,000)",
   "tried": "test_C5_borrowedCaps computes LaunchPegRule.borrowedCaps on EthConfig.get, RobinhoodConfig.launch, BaseConfig.launch: 36,000 / 9,000 / 5,000 = 50,000 with the stated split: holds. No contract enforces it (as stated); keeper over-read bound in C5."
  }
 ],
 "notRun": [
  "[fork] sUSDS cannot dip (C4): NOT RUN (no RPC used); reasoned from README:867",
  "forge --use solar: forge 1.8.3 has no solar; compiled with solc 0.8.38 instead",
  "Off-chain keeper code (not in the commit): bound reasoned from SafeBatch.liquidReserve's identical formula"
 ]
}