From d6665791a2229e24cad0818a447c5579af75c6c9 Mon Sep 17 00:00:00 2001 From: Michael De Luca <35537333+deluca-mike@users.noreply.github.com> Date: Sun, 12 Dec 2021 18:36:25 -0500 Subject: [PATCH] fix: `getExpectedAmount` gas savings (C4 #62) (#45) * chore: Remove TODO on pullFunds (c4 #10) * chore: strict 0.8.7 (c4 #23) * fix: Add named returns where missing (c4 #25) * fix: `handleClaimOfRepossessed` gas optimization (c4 #66) * fix: `getExpectedAmount` gas savings (c4 #62) * formatting: use loan instead of loanAddress Co-authored-by: Lucas Manuel --- contracts/DebtLocker.sol | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/contracts/DebtLocker.sol b/contracts/DebtLocker.sol index 5c2b0ca..90706bc 100644 --- a/contracts/DebtLocker.sol +++ b/contracts/DebtLocker.sol @@ -236,19 +236,24 @@ contract DebtLocker is IDebtLocker, DebtLockerStorage, MapleProxied { } function getExpectedAmount(uint256 swapAmount_) external view override whenProtocolNotPaused returns (uint256 returnAmount_) { - address collateralAsset = IMapleLoanLike(_loan).collateralAsset(); - address fundsAsset = IMapleLoanLike(_loan).fundsAsset(); + address loan = _loan; + address collateralAsset = IMapleLoanLike(loan).collateralAsset(); + address fundsAsset = IMapleLoanLike(loan).fundsAsset(); + + address globals = _getGlobals(); + + uint8 collateralAssetDecimals = IERC20Like(collateralAsset).decimals(); uint256 oracleAmount = swapAmount_ - * IMapleGlobalsLike(_getGlobals()).getLatestPrice(collateralAsset) // Convert from `fromAsset` value. - * 10 ** uint256(IERC20Like(fundsAsset).decimals()) // Convert to `toAsset` decimal precision. - * (10_000 - _allowedSlippage) // Multiply by allowed slippage basis points - / IMapleGlobalsLike(_getGlobals()).getLatestPrice(fundsAsset) // Convert to `toAsset` value. - / 10 ** uint256(IERC20Like(collateralAsset).decimals()) // Convert from `fromAsset` decimal precision. - / 10_000; // Divide basis points for slippage - - uint256 minRatioAmount = swapAmount_ * _minRatio / 10 ** IERC20Like(collateralAsset).decimals(); + * IMapleGlobalsLike(globals).getLatestPrice(collateralAsset) // Convert from `fromAsset` value. + * uint256(10) ** uint256(IERC20Like(fundsAsset).decimals()) // Convert to `toAsset` decimal precision. + * (uint256(10_000) - _allowedSlippage) // Multiply by allowed slippage basis points + / IMapleGlobalsLike(globals).getLatestPrice(fundsAsset) // Convert to `toAsset` value. + / uint256(10) ** uint256(collateralAssetDecimals) // Convert from `fromAsset` decimal precision. + / uint256(10_000); // Divide basis points for slippage. + + uint256 minRatioAmount = swapAmount_ * _minRatio / 10 ** collateralAssetDecimals; return oracleAmount > minRatioAmount ? oracleAmount : minRatioAmount; }