{
 "item": "peg-toll-hook",
 "commit": "ff1c192b3798542997f600091f2dc73ff05e4c40",
 "forge": "forge 1.8.3 (older than 1.9: no --use solar; solc 0.8.38 installed and used via foundry.toml, via_ir)",
 "verdict": "findings",
 "findings": [
  {
   "id": "peg-toll-hook-F1",
   "severity": "low",
   "title": "Splitting a sell at the -1% wall pays about 14% of the toll: the known 'about a third' (README:1064) is worse than written",
   "property": "P1",
   "claim": "C2",
   "files": [
    "src/peg/PegHook.sol:130",
    "src/peg/PegHook.sol:142",
    "src/peg/PegHook.sol:199",
    "src/peg/PegHook.sol:205",
    "README.md:1064"
   ],
   "sequence": "Pool at $1. Seller wants to push to -1.05%. One exact-in sale ending at -1.05% is tolled at 5.75% (the wall) on its whole output: 5,930.07 USDC. The same sale split into 48 slices (or any 20..100) in one block/transaction, each tolled at the curve where it ends (0 inside the band, <1.5% on the soft leg, the 5% wall only on the last thin slices) pays 846.18 USDC = 14.26% of the single-swap toll. Even two swaps (-0.99% then -1.05%) pay 30.2%. Same pool end state, same stable sold.",
   "expected": "README:1064 accepts 'a push split into pieces pays about a third of the toll' (wave 1, curve was linear to 20% at -2%). A split pays about a third or more.",
   "actual": "With the day-1 curve's step from 1.5% to 5% at -1% (PegHook.sol:199-205), a push ending just past the wall can be split to pay ~14% of the toll (end ticks -101..-105, 20..100 pieces: fuzz fails at every bound tried). At -2% (test_C2_splitAtCap) the split pays 33.95%, as written. The wall's 5% step is therefore almost entirely avoidable: the stable module (sink) receives ~1/7 of the toll the curve prices.",
   "reachableAtDeploy": true,
   "fix": "Price the toll on the path, not the end: charge the integral of the curve between start and end price (read start sqrtPrice in a beforeSwap or from transient storage), or at least charge each swap from max(start,end) deviation for the part beyond the wall; otherwise amend README:1064 to state the wall can be split down to ~1/7.",
   "test": {
    "source": "// test/scratch/Toll.t.sol (excerpt). Base: TollScratch is PegSystem (test/utils/PegSystem.sol): a real v4 PoolManager, the PegHook at a mined\n// CREATE2 address, the pool opened at $1 with 2e19 liquidity over +/-1000 ticks, sink = a real StableModule (one USDC leg); run for both token\n// orders (TollScratch0: quote 0xFFfF..; TollScratch1: quote 0x..1001). setUp copies PegHook.t.sol's (MockERC20 etched instead of deployCodeTo,\n// which forge 1.8.3 refused: 'multiple matching artifacts'). _moveTo(t) = one exact-in swap to `t` ticks from the peg.\n    /// C2: a sell from $1 to just past the -1% wall, one swap vs `n` pieces. README:1064 says a split pays about a third.\n    function testFuzz_C2_splitAtWall(uint8 endT, uint8 n) public {\n        int24 end_ = -int24(uint24(bound(endT, 101, 105)));\n        uint256 pieces = bound(n, 20, 100);\n        uint256 snap = vm.snapshotState();\n        _moveTo(end_);\n        uint256 single = _tolls();\n        vm.revertToState(snap);\n        for (uint256 i = 1; i <= pieces; ++i) {\n            int24 t = int24(int256(i)) * end_ / int24(int256(pieces));\n            _moveTo(t);\n        }\n        _moveTo(end_);\n        uint256 split = _tolls();\n        emit log_named_uint(\"single toll (USDC units)\", single);\n        emit log_named_uint(\"split toll  (USDC units)\", split);\n        emit log_named_uint(\"split / single (bps)\", split * 10_000 / single);\n        assertGe(split * 3, single, \"a split pays under a third of the toll\");\n    }\n    /// Finding: two swaps in one block, stop just inside the -1% wall, then step over it.\n    function test_F1_twoSwapsJumpTheWall() public {\n        uint256 snap = vm.snapshotState();\n        _moveTo(-105); // one sale, $1 -> -1.05%\n        uint256 single = _claims(_quoteCur());\n        vm.revertToState(snap);\n        _moveTo(-99); // same sale in two: $1 -> -0.99% (soft leg, <1.5%) ...\n        _moveTo(-105); // ... then -0.99% -> -1.05% (the wall, on the small rest only)\n        uint256 split = _claims(_quoteCur());\n        emit log_named_uint(\"one swap toll (USDC units)\", single);\n        emit log_named_uint(\"two swaps toll (USDC units)\", split);\n        emit log_named_uint(\"two / one (bps)\", split * 10_000 / single);\n        assertGe(split * 3, single, \"README:1064 'about a third' broken\");\n    }\n",
    "ran": "forge test --match-path test/scratch/Toll.t.sol --match-test 'splitAtWall|F1' -vv (forge 1.8.3, solc 0.8.38)",
    "result": "FAIL both orders. testFuzz_C2_splitAtWall: 'a split pays under a third of the toll: 2538552684 < 5930071304; args=[10, 48]' logs single 5930071304, split 846184228, split/single 1426 bps. test_F1_twoSwapsJumpTheWall: '5367785430 < 5930071304' two/one 3017 bps. test_C2_splitAtCap PASS (3395 bps)."
   }
  }
 ],
 "claims": [
  {
   "id": "C1",
   "result": "holds",
   "evidence": "test_C1_edges (both token orders) walks sqrtPrice from peg*(1-1.3%) to peg*(1+1.3%) in 0.005% steps (price +/-2.6%) both directions and checks surchargeFor against the NatSpec curve (0 inside dev<=0.25%; 15000*(dev-25bp)/75bp to 1%; below: 50000+150000*(dev-1%)/1% capped at 200000 from -2%; above: flat 15000; 0 toward $1); exact $1 -> 0 and feeFor = 100. PASS. Note the curve is a step at -1% (1.5% -> 5%), which is what F1 exploits."
  },
  {
   "id": "C2",
   "result": "refuted",
   "evidence": "Split: see F1 (14.26% of the toll at -1.05%, 30% with two swaps): worse than README:1064's 'about a third'. Exact-out vs exact-in: testFuzz_C2_towardAndExactOut (256 runs x2 orders, start -400..400 ticks, 1k-150k USDC) asserts exact-out cost per quote >= 96% of exact-in for the same pool move: PASS (toll on unspecified amount: (1+s) vs 1/(1-s), ratio 1-s^2 >= 0.96). Toward $1 ending inside the band never paid (same fuzz): PASS."
  },
  {
   "id": "C3",
   "result": "holds",
   "evidence": "testFuzz_C3_sweepWhole (256 runs x2): 6 random excursions within +/-4%, then sweep of both currencies by a fuzzed caller: hook claims 0, sink (StableModule) balance rises by exactly the claims, hook holds no ERC20, supply unchanged. PASS. Only writers of the hook's claims: afterSwap mint (PegHook.sol:143), unlockCallback burn (:159), and third-party ERC-6909 transfers to the hook (also swept to sink). unlockCallback is onlyPoolManager; sweep inside another unlock reverts (AlreadyUnlocked); the take goes to the immutable sink only."
  },
  {
   "id": "C4",
   "result": "holds",
   "evidence": "testFuzz_P4_notes (256 runs): 24 interleaved ops (revenue, warps up to 40d, buys by 3 accounts incl. at the last second of the 7d window, redeems), tenors 30/90/180d, discount 1..2000bps: maturity stays open+7d+tenor, no redeem before it, outstanding <= 2x trailingRevenue after every successful buy, paid*1e12*1e4 >= face*(1e4-d) and paid >= 1 (priceOf rounds up, FeeNotes.sol:99), imdUSD supply unchanged by buy/redeem. PASS."
  },
  {
   "id": "C5",
   "result": "holds",
   "evidence": "testFuzz_C5_onePool (256 runs x2): re-initialising the same key (at peg or any sqrtPrice) reverts (PoolManager PoolAlreadyInitialized, the hook's state write rolls back with it); any other fee/tickSpacing with this hook at any sqrtPrice reverts (WrongPool/NotAtPeg, PegHook.sol:118-124). PASS."
  }
 ],
 "holds": [
  {
   "property": "P2",
   "tried": "testFuzz_C3_sweepWhole (fuzzed excursions, fuzzed sweeper, double sweep). Sink fixed at construction (StageGenesis._tollSink = StableModule, Treasury only if no module); the module counts idle reserve by balanceOf (StableModule._snap), so swept USDC is reserve and swept imdUSD is held. Hook has no other token path; a direct ERC20 donation to the hook would be stuck but is the donor's loss, not protocol money."
  },
  {
   "property": "P3",
   "tried": "testFuzz_C5_onePool; constructor requires address bits == FLAGS exactly (PegHook.sol:79), getHookPermissions matches; PoolManager validates the address at initialize. Unused callbacks revert but their flags are not set, so they are never called."
  },
  {
   "property": "P4",
   "tried": "testFuzz_P4_notes and testFuzz_meter (256 runs: 30 interleaved revenue/warp ops up to 50d; Treasury.trailingRevenue equals a model sum of the last 90 UTC days incl. today after every op). Revenue is only interest the Ledger mints to the Treasury (LedgerBase.sol:297-299); payNote transfers, never mints (Treasury.sol:170-175); open is Parameters-only after 48h with tenor in {30,90,180}d and discount>0 (ParametersRules.sol:106-109). Only writers of outstanding: buy/redeem; readers: capacity and Treasury.retireReserve keep."
  },
  {
   "property": "P1 (except the split level in F1)",
   "tried": "test_C1_edges, testFuzz_C2_towardAndExactOut, test_C2_splitAtCap; afterSwap tolls the unspecified currency (specifiedIs0 = amountSpecified<0 == zeroForOne, PegHook.sol:136) at the END price and direction; no liquidity/donate flags, module mint/redeem never touch the pool. A swap crossing $1 is tolled on its whole amount (known, README:1064)."
  }
 ],
 "notRun": [
  "No [fork]/[data] claims in this sheet; no fork run (NOT RUN). forge 1.8.3: --use solar unavailable; compiled with solc 0.8.38 via foundry.toml. Builder/sequencer ordering covered only by same-block multi-swap sequences in tests, not by a mempool simulation."
 ]
}