Skip to content

Commit

Permalink
fix: getExpectedAmount gas savings (C4 #62) (#45)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
deluca-mike and Lucas Manuel authored Dec 12, 2021
1 parent f446041 commit d666579
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions contracts/DebtLocker.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit d666579

Please sign in to comment.