You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Exchange Rate is neglected in DS-RA Swaps causing Issues like Fund Loss
Summary
Failure to account for DS+CT to RA exchange rate will cause an incorrect valuation of user tokens for swappers as the contract will incorrectly calculate swap amounts and flash loan repayments.
function getAmountOutSellDS(AssetPair storageassetPair, uint256amount)
internalviewreturns (uint256amountOut, uint256repaymentAmount)
{
(uint112raReserve, uint112ctReserve) =getReservesSorted(assetPair);
// we calculate the repayment amount based on the imbalanced ct reserve since we borrow CT from the AMM
repaymentAmount = MinimalUniswapV2Library.getAmountIn(amount, raReserve, ctReserve - amount);
// the amountOut is essentially what the user receive, we can calculate this by simply subtracting the repayment amount// from the amount, since we're getting back the same RA amount as DS user buy, this works. to get the effective price per DS,// you would devide this by the DS amount user bought.// note that we subtract 1 to enforce uni v2 rules
amountOut = amount - repaymentAmount;
// enforce uni v2 rules, pay 1 wei more
amountOut -=1;
repaymentAmount +=1;
assert(amountOut + repaymentAmount == amount);
Flash Swap Initiation:
The __flashSwap function initiates the flash loan, borrowing the calculated amountOut of RA.
Flash Swap Callback (uniswapV2Call):
The redeemRaWithCtDs accounts for the exchange rate redeeming expected correct RA.
raAttributed (passed as extraData in the flash swap) is based on the incorrect amountOut calculated earlier.
The repayment amount is calculated based on this incorrect raAttributed, leading to an excess repayment to the flash loan provider.
A similar issue exists for the RA to DS swap process in getAmountOutBuyDS and afterFlashswapBuy.
While there is slippage protection via min amounts, but since preview methods also do the same computation, user provided min amounts will likely be wrong in the first place.
Internal pre-conditions
DS+CT to RA exchange rate needs to be greater than 1:1 which is expected as per spec.
External pre-conditions
No response
Attack Path
User calls swapDsforRa function with a certain amount of DS tokens
Contract calculates amountOut without considering the exchange rate
Contract initiates a flash loan for the undervalued amountOut of RA
In the callback, contract redeems RA from PSM without adjusting for exchange rate
Contract transfers the undervalued raAttributed amount to the user
Excess RA is used to repay the flash loan
Impact
The users suffer an approximate loss proportional to the difference between the actual exchange rate and 1:1 for DS for RA swap. Similar issue exist in RA for DS swap.
sherlock-admin3
changed the title
Gorgeous Chrome Locust - Exchange Rate is neglected in DS-RA Swaps causing Issues like Fund Loss
0xNirix - Exchange Rate is neglected in DS-RA Swaps causing Issues like Fund Loss
Sep 25, 2024
0xNirix
Medium
Exchange Rate is neglected in DS-RA Swaps causing Issues like Fund Loss
Summary
Failure to account for DS+CT to RA exchange rate will cause an incorrect valuation of user tokens for swappers as the contract will incorrectly calculate swap amounts and flash loan repayments.
Root Cause
User Initiates Swap:
At this point, amount is the DS amount, but it's not adjusted for the exchange rate.
Core Swap Logic in __swapDsforRa:
getAmountOutSellDS doesn't consider the exchange rate, potentially undervaluing amountOut.
https://github.com/sherlock-audit/2024-08-cork-protocol/blob/main/Depeg-swap/contracts/libraries/DsFlashSwap.sol#L166-L170
Flash Swap Initiation:
The __flashSwap function initiates the flash loan, borrowing the calculated amountOut of RA.
Flash Swap Callback (uniswapV2Call):
After Flash Swap Sell (__afterFlashswapSell):
The redeemRaWithCtDs accounts for the exchange rate redeeming expected correct RA.
raAttributed (passed as extraData in the flash swap) is based on the incorrect amountOut calculated earlier.
The repayment amount is calculated based on this incorrect raAttributed, leading to an excess repayment to the flash loan provider.
A similar issue exists for the RA to DS swap process in getAmountOutBuyDS and afterFlashswapBuy.
While there is slippage protection via min amounts, but since preview methods also do the same computation, user provided min amounts will likely be wrong in the first place.
Internal pre-conditions
External pre-conditions
No response
Attack Path
Impact
The users suffer an approximate loss proportional to the difference between the actual exchange rate and 1:1 for DS for RA swap. Similar issue exist in RA for DS swap.
PoC
No response
Mitigation
No response
Duplicate of #119
The text was updated successfully, but these errors were encountered: