Reallocation incorrectly sends the exceed quoteTokens to Market contract instead of reallocator
mediumLines of code
https://github.com/code-423n4/2024-05-predy/blob/main/src/libraries/logic/ReallocationLogic.sol#L69
Vulnerability details
Impact
Loss of funds (quoteToken) for reallocator if the swapped quoteTokens exceeds requirement during reallocation.
Bug Description
See the reallocation flowchart here: https://docs.predy.finance/predy-v6/dev/architecture/flowchart#reallocate.
The reallocator would call a Market contract, which triggers the reallocation logic in PredyPool. Then tokens would then be swapped accordingly using the SettlementCallbackLib.
For baseToken, there is a check that the diff in baseToken (before/after the swap) must be equal to what is requried for reallocation. For quoteToken, if the swap generates more quoteToken than what is required (e.g. the exceedsQuote > 0 case in the following code), the quoteTokens should be transferred back to the initial reallocator, since it was the reallocator who was performing the swap.
However, the quoteTokens are transferred back to msg.sender, which is the Market contract, and would cause a loss of funds for the reallocator.
solidityfunction reallocate(GlobalDataLibrary.GlobalData storage globalData, uint256 pairId, bytes memory settlementData) external returns (bool isRangeChanged) { ... { int256 deltaPositionBase; int256 deltaPositionQuote; (relocationOccurred, isRangeChanged, deltaPositionBase, deltaPositionQuote) = Perp.reallocate(pairStatus, pairStatus.sqrtAssetStatus); if (deltaPositionBase != 0) { globalData.initializeLock(pairId); globalData.callSettlementCallback(settlementData, deltaPositionBase); (int256 settledQuoteAmount, int256 settledBaseAmount) = globalData.finalizeLock(); int256 exceedsQuote = settledQuoteAmount + deltaPositionQuote; if (exceedsQuote < 0) { revert IPredyPool.QuoteTokenNotSettled(); } if (settledBaseAmount + deltaPositionBase != 0) { revert IPredyPool.BaseTokenNotSettled(); } if (exceedsQuote > 0) { > ERC20(pairStatus.quotePool.token).safeTransfer(msg.sender, uint256(exceedsQuote)); } } ... } ... }
Proof of Concept
N/A
Tools Used
Manual review
Recommended Mitigation Steps
Send the tokens to the reallocator.
Assessed type
Token-Transfer
