# When LockManager.lockOnBehalf is called from MigrationManager, the user's reminder will be set to 0, resulting in fewer received MunchableNFTs

MigrationManager calling LockManager.lockOnBehalf resets the user's accumulated remainder to 0, so leftover tokens never carry over and fewer MunchableNFTs are minted.

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

**Target:** LockManager/MigrationManager Solidity contracts
**Root cause:** In _lock, the remainder calculation and NFT minting are skipped when msg.sender == migrationManager, but lockedToken.remainder is still overwritten with the uninitialized local remainder (0), discarding any previously accumulated remainder.
**Steps:**
1. Alice locks 1.9e18 ETH via lm.lock; remainder accumulates.
2. Skip lockDuration, Alice unlocks 0.1e18.
3. MigrationManager calls lm.lockOnBehalf{value:1e18}(address(0),1e18,alice).
4. In _lock, msg.sender==migrationManager skips remainder calc, then sets lockedToken.remainder = 0.
5. Alice locks remaining 0.2e18; total 2e18 but only 1 unrevealed NFT because prior remainder was lost.
6. unlockTime also reset, blocking Alice from unlocking the stranded funds.
**How found:** Code-reading of _lock's conditional remainder/NFT calculation gated on msg.sender != migrationManager, noticing lockedToken.remainder is unconditionally overwritten with the skipped (zero) local variable.
**Exploitation:** No attacker profit; a user who accumulated a non-zero remainder and then has NFTs migrated during a lockdrop loses that remainder, receiving fewer NFTs and being forced to wait an extra lockDuration to unlock.
**Insight:** When a function branches on caller to skip computation, check whether the skipped local variables are still written to storage afterward — a skipped calculation often means a zeroed state write.

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

**Foundry test proving remainder reset on migration**

```
function test_reminderWillBeResetWhenLockMigrateNFTMigrationManager() public {
        deployContracts();

        vm.prank(alice);
        amp.register(MunchablesCommonLib.Realm(3), address(0));

        uint256 lockAmount = 1.9e18;
    
        deal(alice, 2e18);
        deal(address(migm), 2e18);

        uint256 totalLocked;

        vm.prank(alice);
        lm.lock{value: lockAmount}(address(0), lockAmount);
        totalLocked += lockAmount;

        // skip lockDuration
        skip(lm.getPlayerSettings(alice).lockDuration);

        // Alice can unlock here
        vm.prank(alice);
        lm.unlock(address(0), 0.1e18);
        totalLocked -= 0.1e18;

        vm.prank(address(migm));
        lm.lockOnBehalf{value: 1e18}(address(0), 1e18, alice);

        // Alice cannot unlock here, because MigrationManager has reset the unlockTime
        vm.expectRevert();
        vm.prank(alice);
        lm.unlock(address(0), 0.1e18);

        // But also, if lock the remaining amount to fill the remainder, won't receive an NFT.
        vm.prank(alice);
        lm.lock{value: 0.2e18}(address(0), 0.2e18);
        totalLocked += 0.2e18;

        console.log("totalLocked: ", totalLocked);
        console.log("unrevealedNFTs: ", nfto.getUnrevealedNFTs(alice));
    }
```

**MigrationManager _migrateNFTs calling lockOnBehalf**

```
function _migrateNFTs(
    address _user,
    address _tokenLocked,
    uint256[] memory tokenIds
) internal {
    // ... 

    uint256 quantity = (totalLockAmount * discountFactor) / 10e12;
    if (_tokenLocked == address(0)) {
        _lockManager.lockOnBehalf{value: quantity}(
            _tokenLocked,
            quantity,
            _user
        );
    } else if (_tokenLocked == address(WETH)) {
        WETH.approve(address(_lockManager), quantity);
        _lockManager.lockOnBehalf(_tokenLocked, quantity, _user);
    } else if (_tokenLocked == address(USDB)) {
        USDB.approve(address(_lockManager), quantity);
        _lockManager.lockOnBehalf(_tokenLocked, quantity, _user);
    }
    emit MigrationSucceeded(_user, migratedTokenIds, newTokenIds);
}
```

**LockManager _lock remainder reset logic**

```
uint256 quantity = _quantity + lockedToken.remainder;
@>  uint256 remainder;
    uint256 numberNFTs;
    uint32 _lockDuration = playerSettings[_lockRecipient].lockDuration;

    if (_lockDuration == 0) {
        _lockDuration = lockdrop.minLockDuration;
    }
    if (
        lockdrop.start <= uint32(block.timestamp) &&
        lockdrop.end >= uint32(block.timestamp)
    ) {
        if (
            _lockDuration < lockdrop.minLockDuration ||
            _lockDuration >
            uint32(configStorage.getUint(StorageKey.MaxLockDuration))
        ) revert InvalidLockDurationError();
@>      if (msg.sender != address(migrationManager)) {
            // calculate number of nfts
@>          remainder = quantity % configuredToken.nftCost;
            numberNFTs = (quantity - remainder) / configuredToken.nftCost;

            if (numberNFTs > type(uint16).max) revert TooManyNFTsError();

            // Tell nftOverlord that the player has new unopened Munchables
            nftOverlord.addReveal(_lockRecipient, uint16(numberNFTs));
        }
    }
    
    // ...

@>  lockedToken.remainder = remainder;
    lockedToken.quantity += _quantity;
```

