# Players can gain more NFTs benefiting from that past remainder in subsequent locks

LockManager.unlock() never resets lockedToken.remainder, so a player's leftover remainder carries into the next lock, minting extra NFTs beyond the tokens locked.

- Payout: Audit contest
- Severity: medium
- Program: Munchables
- Found by: merlinboii
- Disclosed: 2024-06-21
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-05-munchables-findings/issues/73
- Library page: https://bugbountylibrary.com/r/c4%3A2024-05-munchables%3AM-03
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:code4rena sev:medium tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** LockManager.sol lock/unlock NFT accounting
**Root cause:** unlock() decrements lockedToken.quantity but leaves lockedToken.remainder untouched, so the prior remainder is re-added to the next _lock() quantity and counted toward NFT minting.
**Steps:**
1. Set nftCost=3e18 for address(0); lock 10e18 during lockdrop -> remainder=1e18, 3 NFTs.
2. After lockDuration, unlock 5e18 partially; remainder stays 1e18.
3. Re-lock 5e18: quantity=5e18+1e18=6e18 -> 2 more NFTs, remainder=0.
4. Total 5 NFTs for 10e18 locked vs 3 expected.
**How found:** Code reading of _lock()'s quantity = _quantity + lockedToken.remainder against unlock(), noticing unlock never clears remainder.
**Exploitation:** Player repeatedly locks/unlocks during or across lockdrop periods, carrying the stale remainder into each new lock to mint NFTs in excess of the nftCost-proportional amount.
**Escalation:** Repeat partial unlock/relock cycles across lockdrops to accumulate extra NFT reveals; impact bounded by remainder < nftCost.
**Insight:** When a struct field accumulates across calls (remainder), audit every state transition that reduces the paired quantity field for missing resets of the accumulator.

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

**Fully unlock then relock to gain extra NFTs**

```
1.  Lock **13e18** (`_quantity`) tokens

as [`uint256 quantity = _quantity + lockedToken.remainder :: L344`](https://github.com/code-423n4/2024-05-munchables/blob/main/src/managers/LockManager.sol#L344)

The state updates as follows:

*   `quantity` = 13e18(`_quantity`) + 0 (`lockedToken.remainder`) = **10e18**
*   `remainder` = 13e18 % 3e18 = 1e18
*   `numberNFTs` = (13e18 - 1e18) / 3e18 = **4** 📍

Updates to `lockedToken` struct and `LockManager` contract balance:

*   `lockedToken.remainder = remainder` = **1e18** 📍
*   `lockedToken.quantity += _quantity` = 0 + 13e18 = **13e18** 📍
*   `address(LockManager).balance` = **13e18** 📍

This shows that locking of **13e18** tokens can retrieve **4** NFTs.

After the `lockDuration` has passed, the player can unlock the locked tokens.

2.  Unlock **13e18** tokens **fully**:

*   [`L416::lockedToken.quantity -= _quantity`](https://github.com/code-423n4/2024-05-munchables/blob/main/src/managers/LockManager.sol#L416) = 13e18 - 13e18 = **0** 📍
*   `address(LockManager).balance` = **0** 📍

3.  Lock **11e18** tokens

as [`uint256 quantity = _quantity + lockedToken.remainder (prev remainder#1);`](https://github.com/code-423n4/2024-05-munchables/blob/main/src/managers/LockManager.sol#L344)

*   `quantity` = 11e18(`_quantity`) + 1e18 (`lockedToken.remainder`) = **12e18**
*   `remainder` = 12e18 % 3e18 = 0
*   `numberNFTs` = (12e18 - 0) / 3e18 = **4** 📍

Updates to `lockedToken` and `LockManager` contract balance:

*   `lockedToken.remainder = remainder` = **0** 📍
*   `lockedToken.quantity += _quantity` = 0 + 11e18 = **11e18** 📍
*   `address(LockManager).balance` = **11e18** 📍

In total, the player retrieves **4** new NFTs from locking 11e18 tokens. Compared to the first locking of **13e18** tokens, the second lock yields similar power to gain **4** NFTs, benefiting from the remainder of the previous locking round.
```

