Skip to content

Commit

Permalink
Merge pull request #1297 from morpho-dao/fix/staked-eth
Browse files Browse the repository at this point in the history
Staked ETH Refactor
  • Loading branch information
MerlinEgalite authored Sep 20, 2022
2 parents 1a56fcb + 27ecd34 commit baedaee
Show file tree
Hide file tree
Showing 17 changed files with 185 additions and 295 deletions.
30 changes: 0 additions & 30 deletions .github/workflows/ci-foundry-aave-v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,3 @@ jobs:
alchemyKey: ${{ secrets.ALCHEMY_KEY }}
protocol: aave-v2
network: eth-mainnet

morpho-aave-v2-polygon-mainnet:
name: polygon-mainnet
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- uses: ./.github/actions/ci-foundry
with:
alchemyKey: ${{ secrets.ALCHEMY_KEY }}
protocol: aave-v2
network: polygon-mainnet
gasReport: false

morpho-aave-v2-avalanche-mainnet:
name: avalanche-mainnet
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- uses: ./.github/actions/ci-foundry
with:
alchemyKey: ${{ secrets.ALCHEMY_KEY }}
protocol: aave-v2
network: avalanche-mainnet
gasReport: false
73 changes: 0 additions & 73 deletions config/avalanche-mainnet/aave-v2/Config.sol

This file was deleted.

3 changes: 0 additions & 3 deletions config/eth-mainnet/BaseConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,4 @@ contract BaseConfig {
address constant sushi = 0x6B3595068778DD592e39A122f4f5a5cF09C90fE2;
address constant crv = 0xD533a949740bb3306d119CC777fa900bA034cd52;
address constant stEth = 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;

address constant stEthWhale = 0xDC24316b9AE028F1497c275EB9192a3Ea0f67022;
address constant stEthWhale2 = 0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0;
}
1 change: 1 addition & 0 deletions config/eth-mainnet/aave-v2/Config.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ contract Config is BaseConfig {
address constant aUsdt = 0x3Ed3B47Dd13EC9a98b44e6204A523E766B225811;
address constant aWbtc = 0x9ff58f4fFB29fA2266Ab25e75e2A8b3503311656;
address constant aWeth = 0x030bA81f1c18d280636F32af80b9AAd02Cf0854e;
address constant aStEth = 0x1982b2F5814301d4e9a8b0201555376e62F82428;

address constant wrappedNativeToken = wEth;
address constant aWrappedNativeToken = aWeth;
Expand Down
73 changes: 0 additions & 73 deletions config/polygon-mainnet/aave-v2/Config.sol

This file was deleted.

21 changes: 21 additions & 0 deletions contracts/aave-v2/InterestRatesManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.13;

import "./interfaces/aave/IAToken.sol";
import "./interfaces/lido/ILido.sol";

import "@morpho-dao/morpho-utils/math/PercentageMath.sol";
import "@morpho-dao/morpho-utils/math/WadRayMath.sol";
Expand All @@ -18,6 +19,16 @@ contract InterestRatesManager is IInterestRatesManager, MorphoStorage {
using PercentageMath for uint256;
using WadRayMath for uint256;

/// STORAGE ///

address public constant ST_ETH = 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;

uint256 public immutable ST_ETH_BASE_REBASE_INDEX;

constructor() {
ST_ETH_BASE_REBASE_INDEX = ILido(ST_ETH).getPooledEthByShares(WadRayMath.RAY);
}

/// STRUCTS ///

struct Params {
Expand Down Expand Up @@ -63,6 +74,16 @@ contract InterestRatesManager is IInterestRatesManager, MorphoStorage {
uint256 newPoolSupplyIndex = pool.getReserveNormalizedIncome(underlyingToken);
uint256 newPoolBorrowIndex = pool.getReserveNormalizedVariableDebt(underlyingToken);

if (underlyingToken == ST_ETH) {
uint256 stEthRebaseIndex = ILido(ST_ETH).getPooledEthByShares(WadRayMath.RAY);
newPoolSupplyIndex = newPoolSupplyIndex.rayMul(stEthRebaseIndex).rayDiv(
ST_ETH_BASE_REBASE_INDEX
);
newPoolBorrowIndex = newPoolBorrowIndex.rayMul(stEthRebaseIndex).rayDiv(
ST_ETH_BASE_REBASE_INDEX
);
}

Params memory params = Params(
p2pSupplyIndex[_poolToken],
p2pBorrowIndex[_poolToken],
Expand Down
4 changes: 4 additions & 0 deletions contracts/aave-v2/interfaces/IInterestRatesManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
pragma solidity >=0.8.0;

interface IInterestRatesManager {
function ST_ETH() external view returns (address);

function ST_ETH_BASE_REBASE_INDEX() external view returns (uint256);

function updateIndexes(address _marketAddress) external;
}
8 changes: 8 additions & 0 deletions contracts/aave-v2/interfaces/lido/ILido.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: GNU AGPLv3
pragma solidity ^0.8.0;

interface ILido {
function getPooledEthByShares(uint256 _sharesAmount) external view returns (uint256);

function submit(address _referral) external payable returns (uint256);
}
44 changes: 38 additions & 6 deletions contracts/aave-v2/lens/IndexesLens.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// SPDX-License-Identifier: GNU AGPLv3
pragma solidity 0.8.13;

import "../interfaces/IInterestRatesManager.sol";
import "../interfaces/lido/ILido.sol";

import "./LensStorage.sol";

/// @title IndexesLens.
/// @author Morpho Labs.
/// @custom:contact [email protected]
/// @notice Intermediary layer exposing endpoints to query live data related to the Morpho Protocol market indexes & rates.
abstract contract IndexesLens is LensStorage {
using WadRayMath for uint256;

/// PUBLIC ///

/// @notice Returns the updated peer-to-peer supply index.
Expand Down Expand Up @@ -78,10 +83,12 @@ abstract contract IndexesLens is LensStorage {
Types.PoolIndexes memory lastPoolIndexes = morpho.poolIndexes(_poolToken);
underlyingToken = market.underlyingToken;

(poolSupplyIndex, poolBorrowIndex) = _getPoolIndexes(market.underlyingToken);

InterestRatesModel.GrowthFactors memory growthFactors = InterestRatesModel
.computeGrowthFactors(
poolSupplyIndex = pool.getReserveNormalizedIncome(underlyingToken),
poolBorrowIndex = pool.getReserveNormalizedVariableDebt(underlyingToken),
poolSupplyIndex,
poolBorrowIndex,
lastPoolIndexes,
market.p2pIndexCursor,
market.reserveFactor
Expand Down Expand Up @@ -129,10 +136,12 @@ abstract contract IndexesLens is LensStorage {
Types.Delta memory delta = morpho.deltas(_poolToken);
Types.PoolIndexes memory lastPoolIndexes = morpho.poolIndexes(_poolToken);

(poolSupplyIndex, poolBorrowIndex) = _getPoolIndexes(market.underlyingToken);

InterestRatesModel.GrowthFactors memory growthFactors = InterestRatesModel
.computeGrowthFactors(
poolSupplyIndex = pool.getReserveNormalizedIncome(market.underlyingToken),
poolBorrowIndex = pool.getReserveNormalizedVariableDebt(market.underlyingToken),
poolSupplyIndex,
poolBorrowIndex,
lastPoolIndexes,
market.p2pIndexCursor,
market.reserveFactor
Expand Down Expand Up @@ -170,10 +179,12 @@ abstract contract IndexesLens is LensStorage {
Types.Delta memory delta = morpho.deltas(_poolToken);
Types.PoolIndexes memory lastPoolIndexes = morpho.poolIndexes(_poolToken);

(poolSupplyIndex, poolBorrowIndex) = _getPoolIndexes(market.underlyingToken);

InterestRatesModel.GrowthFactors memory growthFactors = InterestRatesModel
.computeGrowthFactors(
poolSupplyIndex = pool.getReserveNormalizedIncome(market.underlyingToken),
poolBorrowIndex = pool.getReserveNormalizedVariableDebt(market.underlyingToken),
poolSupplyIndex,
poolBorrowIndex,
lastPoolIndexes,
market.p2pIndexCursor,
market.reserveFactor
Expand All @@ -190,4 +201,25 @@ abstract contract IndexesLens is LensStorage {
})
);
}

/// @notice Returns the current pool indexes.
/// @param _underlyingToken The address of the underlying token.
/// @return poolSupplyIndex The pool supply index.
/// @return poolBorrowIndex The pool borrow index.
function _getPoolIndexes(address _underlyingToken)
internal
view
returns (uint256 poolSupplyIndex, uint256 poolBorrowIndex)
{
poolSupplyIndex = pool.getReserveNormalizedIncome(_underlyingToken);
poolBorrowIndex = pool.getReserveNormalizedVariableDebt(_underlyingToken);

if (_underlyingToken == ST_ETH) {
uint256 rebaseIndex = ILido(ST_ETH).getPooledEthByShares(WadRayMath.RAY);
uint256 baseRebaseIndex = morpho.interestRatesManager().ST_ETH_BASE_REBASE_INDEX();

poolSupplyIndex = poolSupplyIndex.rayMul(rebaseIndex).rayDiv(baseRebaseIndex);
poolBorrowIndex = poolBorrowIndex.rayMul(rebaseIndex).rayDiv(baseRebaseIndex);
}
}
}
1 change: 1 addition & 0 deletions contracts/aave-v2/lens/LensStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ abstract contract LensStorage {

uint16 public constant DEFAULT_LIQUIDATION_CLOSE_FACTOR = 5_000; // 50% in basis points.
uint256 public constant HEALTH_FACTOR_LIQUIDATION_THRESHOLD = 1e18; // Health factor below which the positions can be liquidated.
address public constant ST_ETH = 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;

IMorpho public immutable morpho;
ILendingPoolAddressesProvider public immutable addressesProvider;
Expand Down
4 changes: 2 additions & 2 deletions contracts/aave-v2/libraries/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ library Types {
// Variables are packed together to save gas (will not exceed their limit during Morpho's lifetime).
struct PoolIndexes {
uint32 lastUpdateTimestamp; // The last time the local pool and peer-to-peer indexes were updated.
uint112 poolSupplyIndex; // Last pool supply index.
uint112 poolBorrowIndex; // Last pool borrow index.
uint112 poolSupplyIndex; // Last pool supply index. Note that for the stEth market, the pool supply index is tweaked to take into account the staking rewards.
uint112 poolBorrowIndex; // Last pool borrow index. Note that for the stEth market, the pool borrow index is tweaked to take into account the staking rewards.
}

struct Market {
Expand Down
Loading

0 comments on commit baedaee

Please sign in to comment.