From 14af006a2f3e4de58bcef574e40a408315c49984 Mon Sep 17 00:00:00 2001 From: Rooh Afza <96720500+r0ohafza@users.noreply.github.com> Date: Tue, 12 Jul 2022 12:22:10 -0700 Subject: [PATCH] SEN-139 | feat: use one fixed point math lib (#214) * feat: use one fixed point math lib * fix: remove prb-math * fix: add constant * fix: use mulWadDown * fix: rename constant --- .gitmodules | 3 --- lib/prb-math | 1 - src/core/DefaultRateModel.sol | 18 ++++++++++-------- src/core/RiskEngine.sol | 8 ++++---- 4 files changed, 14 insertions(+), 16 deletions(-) delete mode 160000 lib/prb-math diff --git a/.gitmodules b/.gitmodules index 1c412bd..c8c732a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ -[submodule "lib/prb-math"] - path = lib/prb-math - url = https://github.com/paulrberg/prb-math [submodule "lib/oracle"] path = lib/oracle url = https://github.com/sentimentxyz/oracle diff --git a/lib/prb-math b/lib/prb-math deleted file mode 160000 index e33a042..0000000 --- a/lib/prb-math +++ /dev/null @@ -1 +0,0 @@ -Subproject commit e33a042e4d1673fe9b333830b75c4765ccf3f5f2 diff --git a/src/core/DefaultRateModel.sol b/src/core/DefaultRateModel.sol index fcb31ba..35e4ff7 100644 --- a/src/core/DefaultRateModel.sol +++ b/src/core/DefaultRateModel.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.15; -import {PRBMathUD60x18} from "prb-math/PRBMathUD60x18.sol"; +import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol"; import {IRateModel} from "../interface/core/IRateModel.sol"; import {Errors} from "../utils/Errors.sol"; @@ -11,7 +11,7 @@ import {Errors} from "../utils/Errors.sol"; per block */ contract DefaultRateModel is IRateModel { - using PRBMathUD60x18 for uint; + using FixedPointMathLib for uint; /// @notice Constant coefficients with 18 decimals uint immutable c1; @@ -21,6 +21,8 @@ contract DefaultRateModel is IRateModel { /// @notice Number of seconds per year uint immutable secsPerYear; + uint constant SCALE = 1e18; + /** @notice Contract constructor @param _c1 constant coefficient, default value = 1 * 1e17 @@ -55,13 +57,13 @@ contract DefaultRateModel is IRateModel { returns (uint) { uint util = _utilization(liquidity, borrows); - return c3.mul( + return c3.mulWadDown( ( - util.mul(c1) - + util.powu(32).mul(c1) - + util.powu(64).mul(c2) + util.mulWadDown(c1) + + util.rpow(32, SCALE).mulWadDown(c1) + + util.rpow(64, SCALE).mulWadDown(c2) ) - .div(secsPerYear) + .divWadDown(secsPerYear) ); } @@ -71,6 +73,6 @@ contract DefaultRateModel is IRateModel { returns (uint) { uint totalAssets = liquidity + borrows; - return (totalAssets == 0) ? 0 : borrows.div(totalAssets); + return (totalAssets == 0) ? 0 : borrows.divWadDown(totalAssets); } } \ No newline at end of file diff --git a/src/core/RiskEngine.sol b/src/core/RiskEngine.sol index bfbc804..463d94b 100644 --- a/src/core/RiskEngine.sol +++ b/src/core/RiskEngine.sol @@ -8,9 +8,9 @@ import {IERC20} from "../interface/tokens/IERC20.sol"; import {ILToken} from "../interface/tokens/ILToken.sol"; import {IAccount} from "../interface/core/IAccount.sol"; import {IRegistry} from "../interface/core/IRegistry.sol"; -import {PRBMathUD60x18} from "prb-math/PRBMathUD60x18.sol"; import {IRiskEngine} from "../interface/core/IRiskEngine.sol"; import {IAccountManager} from "../interface/core/IAccountManager.sol"; +import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol"; /** @title Risk Engine @@ -18,7 +18,7 @@ import {IAccountManager} from "../interface/core/IAccountManager.sol"; analyze the health factor of a given account. */ contract RiskEngine is Ownable, IRiskEngine { - using PRBMathUD60x18 for uint; + using FixedPointMathLib for uint; /* -------------------------------------------------------------------------- */ /* STATE VARIABLES */ @@ -180,7 +180,7 @@ contract RiskEngine is Ownable, IRiskEngine { view returns (uint) { - return oracle.getPrice(token).mul(amt); + return oracle.getPrice(token).mulWadDown(amt); } function _isAccountHealthy(uint accountBalance, uint accountBorrows) @@ -189,6 +189,6 @@ contract RiskEngine is Ownable, IRiskEngine { returns (bool) { return (accountBorrows == 0) ? true : - (accountBalance.div(accountBorrows) > balanceToBorrowThreshold); + (accountBalance.divWadDown(accountBorrows) > balanceToBorrowThreshold); } } \ No newline at end of file