# Multicall does not work as intended

Size multicall measured borrowAToken supply via contract balance instead of totalSupply, so the cap invariant never triggered, letting users bypass the borrowAToken deposit cap.

- Payout: Audit contest
- Severity: medium
- Program: Size
- Found by: prapandey031
- Disclosed: 2024-09-16
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-06-size-findings/issues/238
- Library page: https://bugbountylibrary.com/r/c4%3A2024-06-size%3AM-01
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** Size.sol multicall / CapsLibrary invariant
**Root cause:** Multicall.sol captures borrowATokenSupplyBefore/After using borrowAToken.balanceOf(address(this)) instead of totalSupply(), so the cap check compares the contract's own balance (often 0) against the cap and never enters the revert branch.
**Steps:**
1. Set borrowAToken cap = 1Me6, current totalSupply = 990ke6, Size contract balance = 0.
2. Call `multicall([deposit(500ke6 USDC), repay(debtPositionId)])`.
3. Deposit mints ~500ke6 borrowAToken to borrower; isMulticall skips validateBorrowATokenCap.
4. Repay transfers 100ke6 borrowAToken to Size contract, reducing debtToken supply by 100ke6.
5. After loop: borrowATokenSupplyAfter = balanceOf(Size) = 100ke6, which is < cap 1Me6, so validateBorrowATokenIncreaseLteDebtTokenDecrease returns without reverting.
6. Invariant (supply increase 500ke6 <= debt decrease 100ke6) is violated; totalSupply now 1.49Me6 > cap.
7. Alternatively, deposit-only multicall: balanceOf(Size)=0 before and after, cap check skipped, totalSupply exceeds cap.
**How found:** Code review of Multicall.sol: noticed supply-before/after read from balanceOf(address(this)) rather than totalSupply(), and traced how a deposit+repay sequence leaves the contract balance below the cap while totalSupply exceeds it.
**Exploitation:** Any user calls the permissionless multicall with a deposit (and optionally repay) to push borrowAToken totalSupply above the configured cap, breaking the supply<=debt-decrease invariant and the deposit limit.
**Insight:** When auditing batch/multicall cap or invariant checks, verify the accounting variable is a global supply (totalSupply) not a local balance (balanceOf(this)); a contract's own balance can stay near zero across a deposit+repay pair, silently defeating cap comparisons.

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

**Multicall uses balanceOf instead of totalSupply**

```
function multicall(State storage state, bytes[] calldata data) internal returns (bytes[] memory results) {
        state.data.isMulticall = true;

        uint256 borrowATokenSupplyBefore = state.data.borrowAToken.balanceOf(address(this));
        uint256 debtTokenSupplyBefore = state.data.debtToken.totalSupply();

        results = new bytes[](data.length);
        for (uint256 i = 0; i < data.length; i++) {
            results[i] = Address.functionDelegateCall(address(this), data[i]);
        }

        uint256 borrowATokenSupplyAfter = state.data.borrowAToken.balanceOf(address(this));
        uint256 debtTokenSupplyAfter = state.data.debtToken.totalSupply();

        state.validateBorrowATokenIncreaseLteDebtTokenDecrease(
            borrowATokenSupplyBefore, debtTokenSupplyBefore, borrowATokenSupplyAfter, debtTokenSupplyAfter
        );

        state.data.isMulticall = false;
    }
}
```

