Skip to content

Commit

Permalink
SEN-139 | feat: use one fixed point math lib (#214)
Browse files Browse the repository at this point in the history
* feat: use one fixed point math lib

* fix: remove prb-math

* fix: add constant

* fix: use mulWadDown

* fix: rename constant
  • Loading branch information
r0ohafza authored Jul 12, 2022
1 parent b269b42 commit 14af006
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 0 additions & 1 deletion lib/prb-math
Submodule prb-math deleted from e33a04
18 changes: 10 additions & 8 deletions src/core/DefaultRateModel.sol
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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)
);
}

Expand All @@ -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);
}
}
8 changes: 4 additions & 4 deletions src/core/RiskEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ 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
@notice Risk engine is a sentiment utility contract used by the protocol to
analyze the health factor of a given account.
*/
contract RiskEngine is Ownable, IRiskEngine {
using PRBMathUD60x18 for uint;
using FixedPointMathLib for uint;

/* -------------------------------------------------------------------------- */
/* STATE VARIABLES */
Expand Down Expand Up @@ -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)
Expand All @@ -189,6 +189,6 @@ contract RiskEngine is Ownable, IRiskEngine {
returns (bool)
{
return (accountBorrows == 0) ? true :
(accountBalance.div(accountBorrows) > balanceToBorrowThreshold);
(accountBalance.divWadDown(accountBorrows) > balanceToBorrowThreshold);
}
}

0 comments on commit 14af006

Please sign in to comment.