Steadefi

Steadefi
DeFiHardhatFoundryOracle
35,000 USDC
View results
Submission Details
Severity: low
Invalid

calcSwapForRepay Function

Summary

The calcSwapForRepay function within the GMX smart contract is intended to determine whether there is a need to swap tokens to cover a repayment and, if necessary, calculate the swap parameters. However, an issue in the logic of this function may lead to unintended consequences when certain conditions are met.

Vulnerability Details

The vulnerable code segment is as follows:

function calcSwapForRepay(
GMXTypes.Store storage self,
GMXTypes.RepayParams memory rp
) external view returns (bool, address, address, uint256) {
address _tokenFrom;
address _tokenTo;
uint256 _tokenToAmt;
//@audit-check: for condition where tokenA and tokenB are not enough
if (rp.repayTokenAAmt > self.tokenA.balanceOf(address(this))) {
// If more tokenA is needed for repayment
_tokenToAmt = rp.repayTokenAAmt - self.tokenA.balanceOf(address(this));
_tokenFrom = address(self.tokenB);
_tokenTo = address(self.tokenA);
return (true, _tokenFrom, _tokenTo, _tokenToAmt);
} else if (rp.repayTokenBAmt > self.tokenB.balanceOf(address(this))) {
// If more tokenB is needed for repayment
_tokenToAmt = rp.repayTokenBAmt - self.tokenB.balanceOf(address(this));
_tokenFrom = address(self.tokenA);
_tokenTo = address(self.tokenB);
return (true, _tokenFrom, _tokenTo, _tokenToAmt);
} else {
// If more there is enough to repay both tokens
return (false, address(0), address(0), 0);
}
}

Impact

The vulnerability in this code is related to the conditional checks for determining whether there is a need to swap either tokens. The issue arises when rp.repayTokenAAmt is greater than the balance of self.tokenA and rp.repayTokenBAmt is greater than the balance of self.tokenB. In this situation, the code may lead to unintended token swapping.

Tools Used

The impact of this vulnerability can be significant and includes the following potential consequences:

  • Unintended Token Swapping: When both rp.repayTokenAAmt and rp.repayTokenBAmt exceed the available balances of self.tokenA and self.tokenB, respectively, the function will incorrectly indicate the need for a token swap for one token without indicating for the other token. This can lead to unnecessary and unintended token swaps, incurring unnecessary gas costs.

  • Efficiency and Gas Costs: The incorrect token swaps triggered by this vulnerability can lead to inefficiency and increased gas costs on the blockchain when you might need to call the function multiple times, which can negatively impact users and the contract's overall performance.

Recommendations

To address this vulnerability and improve the efficiency and correctness of the calcSwapForRepay function, the following recommendation is advised:

  • Reevaluate the Logic: Review the logic of the calcSwapForRepay function to ensure that it correctly identifies cases where token swaps are needed. Specifically, consider scenarios where both rp.repayTokenAAmt and rp.repayTokenBAmt exceed the token balances and modify the logic to prevent unnecessary swaps in such cases.

Updates

Lead Judging Commences

hans Lead Judge over 1 year ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

INFO: Ensure repayment is feasible

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.