The _convertAssetsToUsdc function is responsible for swapping an input asset into USDC using a decentralized exchange (DEX) adapter. While the function ensures asset approval, swap execution, and protocol fee deductions, it does not implement any form of slippage protection, which could result in suboptimal execution prices or losses due to market volatility.
The function calls dexSwapStrategy.executeSwapExactInputSingle and dexSwapStrategy.executeSwapExactInput without specifying a minimum expected output amount. Without a slippage limit, the swap could execute at an unexpectedly low rate, leading to significant losses if the market moves unfavorably between transaction submission and execution.
function _convertAssetsToUsdc(
uint128 dexSwapStrategyId,
address asset,
uint256 assetAmount,
bytes memory path,
address recipient,
address usdc
)
internal
returns (uint256 usdcOut)
{
if (assetAmount == 0) revert Errors.AssetAmountIsZero(asset);
if (asset == usdc) {
usdcOut = assetAmount;
} else {
DexSwapStrategy.Data storage dexSwapStrategy = DexSwapStrategy.loadExisting(dexSwapStrategyId);
IERC20(asset).approve(dexSwapStrategy.dexAdapter, assetAmount);
if (path.length == 0) {
SwapExactInputSinglePayload memory swapCallData = SwapExactInputSinglePayload({
tokenIn: asset,
tokenOut: usdc,
amountIn: assetAmount,
recipient: recipient
});
usdcOut = dexSwapStrategy.executeSwapExactInputSingle(swapCallData);
} else {
SwapExactInputPayload memory swapCallData = SwapExactInputPayload({
path: path,
tokenIn: asset,
tokenOut: usdc,
amountIn: assetAmount,
recipient: recipient
});
usdcOut = dexSwapStrategy.executeSwapExactInput(swapCallData);
}
MarketMakingEngineConfiguration.Data storage marketMakingEngineConfiguration =
MarketMakingEngineConfiguration.load();
uint256 settlementBaseFeeUsd = Collateral.load(usdc).convertUd60x18ToTokenAmount(
ud60x18(marketMakingEngineConfiguration.settlementBaseFeeUsdX18)
);
if (settlementBaseFeeUsd > 0) {
if (usdcOut < settlementBaseFeeUsd) {
revert Errors.FailedToPaySettlementBaseFee();
}
usdcOut -= settlementBaseFeeUsd;
marketMakingEngineConfiguration.distributeProtocolAssetReward(usdc, settlementBaseFeeUsd);
}
}
}