Skip to content

Commit

Permalink
feat(solidity): adding the solidity tracking snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
tomrpl committed Oct 30, 2023
1 parent 825826b commit 7b80e21
Show file tree
Hide file tree
Showing 9 changed files with 869 additions and 53 deletions.
7 changes: 4 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/morpho-blue"]
path = lib/morpho-blue
url = https://github.com/morpho-org/morpho-blue

[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
10 changes: 10 additions & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@forge-std/=lib/morpho-blue/lib/forge-std/src/

@morpho-blue/=lib/morpho-blue/src/
@morpho-blue-test/=lib/morpho-blue/test/

@snippets/=src/

@solmate/=lib/morpho-blue/lib/solmate/src/
solmate/=lib/morpho-blue/lib/permit2/lib/solmate/

12 changes: 0 additions & 12 deletions script/Counter.s.sol

This file was deleted.

14 changes: 0 additions & 14 deletions src/Counter.sol

This file was deleted.

117 changes: 117 additions & 0 deletions src/Snippets.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.0;

import {Id, IMorpho, MarketParams, Market} from "@morpho-blue/interfaces/IMorpho.sol";
import {IERC20} from "@morpho-blue/interfaces/IERC20.sol";
import {IIrm} from "@morpho-blue/interfaces/IIrm.sol";
import {IOracle} from "@morpho-blue/interfaces/IOracle.sol";

import {MorphoBalancesLib} from "@morpho-blue/libraries/periphery/MorphoBalancesLib.sol";
import {MarketParamsLib} from "@morpho-blue/libraries/MarketParamsLib.sol";
import {MorphoLib} from "@morpho-blue/libraries/periphery/MorphoLib.sol";
import {MathLib} from "@morpho-blue/libraries/MathLib.sol";
import {SharesMathLib} from "@morpho-blue/libraries/SharesMathLib.sol";
import {ORACLE_PRICE_SCALE} from "@morpho-blue/libraries/ConstantsLib.sol";

contract Snippets {
using MathLib for uint256;
using MorphoLib for IMorpho;
using MorphoBalancesLib for IMorpho;
using MarketParamsLib for MarketParams;
using SharesMathLib for uint256;
IMorpho public immutable morpho;

constructor(address morphoAddress) {
morpho = IMorpho(morphoAddress);
}

// INFORMATIONAL: No 'Total Supply' and no 'Total Borrow' functions to calculate on chain as there could be some weird oracles / markets created

// ---- VIEW FUNCTIONS ----

// OK - view function?
function supplyAPR(
MarketParams memory marketParams,
Market memory market
) public returns (uint256 supplyRate) {
(uint256 totalSupplyAssets, , uint256 totalBorrowAssets, ) = morpho
.expectedMarketBalances(marketParams);

// Get the borrow rate
uint256 borrowRate = IIrm(marketParams.irm).borrowRate(
marketParams,
market
);

// Get the supply rate
uint256 utilization = totalBorrowAssets == 0
? 0
: totalBorrowAssets.wDivUp(totalSupplyAssets);

supplyRate = borrowRate.wMulDown(1 ether - market.fee).wMulDown(
utilization
);
}

// OK - view function?
function borrowAPR(
MarketParams memory marketParams,
Market memory market
) public returns (uint256 borrowRate) {
borrowRate = IIrm(marketParams.irm).borrowRate(marketParams, market);
}

// OK
function supplyBalance(
MarketParams memory marketParams,
address user
) public view returns (uint256 totalSupplyBalance) {
totalSupplyBalance = morpho.expectedSupplyBalance(marketParams, user);
}

// OK
function borrowBalance(
MarketParams memory marketParams,
address user
) public view returns (uint256 totalBorrowBalance) {
totalBorrowBalance = morpho.expectedBorrowBalance(marketParams, user);
}

// OK
function collateralBalance(
Id marketId,
address user
) public view returns (uint256 totalCollateralBalance) {
(, , totalCollateralBalance) = morpho.position(marketId, user);
}

// OK
function marketTotalSupply(
MarketParams memory marketParams
) public view returns (uint256 totalSupplyAssets) {
totalSupplyAssets = morpho.expectedTotalSupply(marketParams);
}

// OK
function marketTotalBorrow(
MarketParams memory marketParams
) public view returns (uint256 totalBorrowAssets) {
totalBorrowAssets = morpho.expectedTotalBorrow(marketParams);
}

// OK
function userHealthFactor(
MarketParams memory marketParams,
Id id,
address user
) public view returns (uint256 healthFactor) {
uint256 collateralPrice = IOracle(marketParams.oracle).price();
uint256 collateral = morpho.collateral(id, user);
uint256 borrowed = morpho.expectedBorrowBalance(marketParams, user);

uint256 collateralNormalized = collateral
.mulDivDown(collateralPrice, ORACLE_PRICE_SCALE)
.wMulDown(marketParams.lltv);
healthFactor = collateralNormalized.wMulDown(borrowed);
}
}
165 changes: 165 additions & 0 deletions src/libraries/MarketLib.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.0;

// import {MarketParams} from "@morpho-blue/libraries/MarketParamsLib.sol";

// import {MathLib} from "@morpho-blue/libraries/MathLib.sol";

// /// @title MarketLib
// /// @author Morpho Labs
// /// @custom:contact [email protected]
// /// @notice Library used to ease market reads and writes.
// library MarketLib {
// using MathLib for uint256;

// /// @notice Returns whether the `market` is created or not.
// function isCreated(
// Types.Market memory market
// ) internal pure returns (bool) {
// return market.aToken != address(0);
// }

// /// @notice Returns whether supply is paused on `market` or not.
// function isSupplyPaused(
// Types.Market memory market
// ) internal pure returns (bool) {
// return market.pauseStatuses.isSupplyPaused;
// }

// /// @notice Returns whether supply collateral is paused on `market` or not.
// function isSupplyCollateralPaused(
// Types.Market memory market
// ) internal pure returns (bool) {
// return market.pauseStatuses.isSupplyCollateralPaused;
// }

// /// @notice Returns whether borrow is paused on `market` or not.
// function isBorrowPaused(
// Types.Market memory market
// ) internal pure returns (bool) {
// return market.pauseStatuses.isBorrowPaused;
// }

// /// @notice Returns whether repay is paused on `market` or not.
// function isRepayPaused(
// Types.Market memory market
// ) internal pure returns (bool) {
// return market.pauseStatuses.isRepayPaused;
// }

// /// @notice Returns whether withdraw is paused on `market` or not.
// function isWithdrawPaused(
// Types.Market memory market
// ) internal pure returns (bool) {
// return market.pauseStatuses.isWithdrawPaused;
// }

// /// @notice Returns whether withdraw collateral is paused on `market` or not.
// function isWithdrawCollateralPaused(
// Types.Market memory market
// ) internal pure returns (bool) {
// return market.pauseStatuses.isWithdrawCollateralPaused;
// }

// /// @notice Returns whether liquidate collateral is paused on `market` or not.
// function isLiquidateCollateralPaused(
// Types.Market memory market
// ) internal pure returns (bool) {
// return market.pauseStatuses.isLiquidateCollateralPaused;
// }

// /// @notice Returns whether liquidate borrow is paused on `market` or not.
// function isLiquidateBorrowPaused(
// Types.Market memory market
// ) internal pure returns (bool) {
// return market.pauseStatuses.isLiquidateBorrowPaused;
// }

// /// @notice Returns whether the `market` is deprecated or not.
// function isDeprecated(
// Types.Market memory market
// ) internal pure returns (bool) {
// return market.pauseStatuses.isDeprecated;
// }

// /// @notice Returns whether the peer-to-peer is disabled on `market` or not.
// function isP2PDisabled(
// Types.Market memory market
// ) internal pure returns (bool) {
// return market.pauseStatuses.isP2PDisabled;
// }

// /// @notice Returns the supply indexes of `market`.
// function getSupplyIndexes(
// Types.Market memory market
// ) internal pure returns (Types.MarketSideIndexes256 memory supplyIndexes) {
// supplyIndexes.poolIndex = uint256(market.indexes.supply.poolIndex);
// supplyIndexes.p2pIndex = uint256(market.indexes.supply.p2pIndex);
// }

// /// @notice Returns the borrow indexes of `market`.
// function getBorrowIndexes(
// Types.Market memory market
// ) internal pure returns (Types.MarketSideIndexes256 memory borrowIndexes) {
// borrowIndexes.poolIndex = uint256(market.indexes.borrow.poolIndex);
// borrowIndexes.p2pIndex = uint256(market.indexes.borrow.p2pIndex);
// }

// /// @notice Returns the indexes of `market`.
// function getIndexes(
// Types.Market memory market
// ) internal pure returns (Types.Indexes256 memory indexes) {
// indexes.supply = getSupplyIndexes(market);
// indexes.borrow = getBorrowIndexes(market);
// }

// /// @notice Returns the proportion of idle supply in `market` over the total peer-to-peer amount in supply.
// function proportionIdle(
// Types.Market memory market
// ) internal pure returns (uint256) {
// uint256 idleSupply = market.idleSupply;
// if (idleSupply == 0) return 0;

// uint256 totalP2PSupplied = market.deltas.supply.scaledP2PTotal.rayMul(
// market.indexes.supply.p2pIndex
// );

// // We take the minimum to handle the case where the proportion is rounded to greater than 1.
// return Math.min(idleSupply.rayDivUp(totalP2PSupplied), WadRayMath.RAY);
// }

// /// @notice Calculates the total quantity of underlyings truly supplied peer-to-peer on the given market.
// /// @param indexes The current indexes.
// /// @return The total peer-to-peer supply (total peer-to-peer supply - supply delta - idle supply).
// function trueP2PSupply(
// Types.Market memory market,
// Types.Indexes256 memory indexes
// ) internal pure returns (uint256) {
// Types.MarketSideDelta memory supplyDelta = market.deltas.supply;
// return
// supplyDelta
// .scaledP2PTotal
// .rayMul(indexes.supply.p2pIndex)
// .zeroFloorSub(
// supplyDelta.scaledDelta.rayMul(indexes.supply.poolIndex)
// )
// .zeroFloorSub(market.idleSupply);
// }

// /// @notice Calculates the total quantity of underlyings truly borrowed peer-to-peer on the given market.
// /// @param indexes The current indexes.
// /// @return The total peer-to-peer borrow (total peer-to-peer borrow - borrow delta).
// function trueP2PBorrow(
// Types.Market memory market,
// Types.Indexes256 memory indexes
// ) internal pure returns (uint256) {
// Types.MarketSideDelta memory borrowDelta = market.deltas.borrow;
// return
// borrowDelta
// .scaledP2PTotal
// .rayMul(indexes.borrow.p2pIndex)
// .zeroFloorSub(
// borrowDelta.scaledDelta.rayMul(indexes.borrow.poolIndex)
// );
// }
// }
24 changes: 0 additions & 24 deletions test/Counter.t.sol

This file was deleted.

Loading

0 comments on commit 7b80e21

Please sign in to comment.