# Reallocation incorrectly sends the exceed quoteTokens to Market contract instead of reallocator

PredyPool.reallocate sends excess quoteTokens to msg.sender (the Market contract) instead of the reallocator, permanently locking the reallocator's funds.

- Payout: Audit contest
- Severity: medium
- Program: Predy
- Found by: pkqs90
- Disclosed: 2024-07-16
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-05-predy-findings/issues/26
- Library page: https://bugbountylibrary.com/r/c4%3A2024-05-predy%3AM-07
- 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:** PredyPool.reallocate settlement callback
**Root cause:** In reallocate, when exceedsQuote > 0 the surplus quoteTokens are transferred to msg.sender, which is the BaseMarket contract that invoked reallocation, rather than the reallocator who performed the swap.
**Steps:**
1. Reallocator calls Market, which calls PredyPool.reallocate with settlementData.
2. Perp.reallocate returns deltaPositionBase/deltaPositionQuote; callSettlementCallback swaps tokens.
3. finalizeLock yields settledQuoteAmount; exceedsQuote = settledQuoteAmount + deltaPositionQuote > 0.
4. Code executes `ERC20(quoteToken).safeTransfer(msg.sender, exceedsQuote)` where msg.sender is the Market.
5. Market has no function to return these tokens, so the reallocator's surplus quoteTokens are locked.
**How found:** Code reading of the reallocate function's settlement flow, comparing the baseToken equality check against the quoteToken surplus handling and noticing msg.sender resolves to the Market rather than the reallocator.
**Exploitation:** Triggered whenever a reallocation swap produces more quoteToken than required; the surplus is sent to the Market contract and is unrecoverable, causing direct loss to the reallocator.
**Insight:** When auditing callback/settlement flows, trace who msg.sender actually is at the transfer site — intermediary contracts invoking pool logic make 'refund to msg.sender' silently misdirect funds to the caller, not the economic actor.

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

**Reallocate transfers excess quoteTokens to msg.sender**

```
if (exceedsQuote > 0) {
>                   ERC20(pairStatus.quotePool.token).safeTransfer(msg.sender, uint256(exceedsQuote));
```

