Skip to content

Commit

Permalink
Merge branch 'upgrade-0' of github.com:morpho-dao/morpho-v1 into reco…
Browse files Browse the repository at this point in the history
…ver/collateral-check
  • Loading branch information
MerlinEgalite committed Oct 26, 2022
2 parents 5f2500c + 4f35c1a commit 380a301
Show file tree
Hide file tree
Showing 172 changed files with 659 additions and 771 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NETWORK=eth-mainnet
DEPLOYER_PRIVATE_KEY=
TENDERLY_SECRET_KEY=
ETHERSCAN_API_KEY=
ALCHEMY_KEY=
25 changes: 1 addition & 24 deletions .github/actions/ci-foundry/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ inputs:
network:
description: The network against which to run the forge test suite.
required: true
gasReport:
description: Whether to display gas reports.
default: true
required: true

runs:
using: composite
Expand Down Expand Up @@ -48,28 +44,9 @@ runs:
key: ${{ github.base_ref || github.ref_name }}-foundry-${{ inputs.protocol }}-${{ inputs.network }} # always keep compiled contracts from base branch

- name: Run tests
run: make gas-report | tee ${{ inputs.protocol }}.${{ inputs.network }}.gasreport.ansi
run: make test
shell: bash
env:
PROTOCOL: ${{ inputs.protocol }}
NETWORK: ${{ inputs.network }}
ALCHEMY_KEY: ${{ inputs.alchemyKey }}
FOUNDRY_FUZZ_SEED: 0x${{ github.event.pull_request.base.sha || github.sha }}

- name: Compare gas reports
if: inputs.gasReport == 'true'
uses: Rubilmax/foundry-gas-diff@v3.9
with:
report: ${{ inputs.protocol }}.${{ inputs.network }}.gasreport.ansi
ignore: test-foundry/**/*
header: |
# Morpho-${{ inputs.protocol }} gas impacts (${{ inputs.network }})
id: gas_diff

- name: Add gas diff to sticky comment
if: inputs.gasReport == 'true' && (github.event_name == 'pull_request' || github.event_name == 'pull_request_target')
uses: marocchino/sticky-pull-request-comment@v2
with:
header: ${{ inputs.protocol }} (${{ inputs.network }})
message: ${{ steps.gas_diff.outputs.markdown }}
delete: ${{ !steps.gas_diff.outputs.markdown }} # delete the comment in case changes no longer impacts gas costs
1 change: 0 additions & 1 deletion .github/workflows/ci-foundry-aave-v3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
# alchemyKey: ${{ secrets.ALCHEMY_KEY }}
# protocol: aave-v3
# network: polygon-mainnet
# gasReport: false

morpho-aave-v3-avalanche-mainnet:
name: avalanche-mainnet
Expand Down
23 changes: 11 additions & 12 deletions .github/workflows/ci-hardhat-common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ on:
- foundry.toml
- remappings.txt
- .github/workflows/ci-hardhat-common.yml
paths-ignore:
- test/upgrades/**

jobs:
morpho-hardhat-tests:
Expand All @@ -27,15 +25,16 @@ jobs:
with:
submodules: recursive

- uses: actions/setup-node@v3
with:
node-version: 16
cache: yarn
- uses: actions/setup-node@v3
with:
node-version: 16
cache: yarn

- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Run tests
run: yarn test
env:
ALCHEMY_KEY: ${{ secrets.alchemyKey }}
- name: Run tests
run: yarn test
env:
NETWORK: eth-mainnet
ALCHEMY_KEY: ${{ secrets.ALCHEMY_KEY }}
48 changes: 48 additions & 0 deletions .github/workflows/ci-storage-check-aave-v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Storage layout checks

on:
push:
branches:
- main
- dev
pull_request:
paths:
- lib/**
- contracts/common/**
- contracts/aave-v2/**
- "*.lock"
- remappings.txt
- .github/workflows/ci-storage-check-aave-v2.yml

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

- uses: actions/setup-node@v3
with:
node-version: 16
cache: yarn

- name: Install dependencies
run: yarn install --frozen-lockfile
shell: bash

- name: Install Foundry
uses: onbjerg/foundry-toolchain@v1
with:
version: nightly

- name: Check Morpho storage layout
uses: Rubilmax/foundry-storage-check@v2.1
with:
contract: contracts/aave-v2/Morpho.sol:Morpho

- name: Check RewardsManager storage layout
uses: Rubilmax/foundry-storage-check@v2.1
with:
contract: contracts/aave-v2/RewardsManager.sol:RewardsManager
48 changes: 48 additions & 0 deletions .github/workflows/ci-storage-check-compound.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Storage layout checks

on:
push:
branches:
- main
- dev
pull_request:
paths:
- lib/**
- contracts/common/**
- contracts/compound/**
- "*.lock"
- remappings.txt
- .github/workflows/ci-storage-check-compound.yml

jobs:
morpho-compound:
name: morpho-compound
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- uses: actions/setup-node@v3
with:
node-version: 16
cache: yarn

- name: Install dependencies
run: yarn install --frozen-lockfile
shell: bash

- name: Install Foundry
uses: onbjerg/foundry-toolchain@v1
with:
version: nightly

- name: Check Morpho storage layout
uses: Rubilmax/foundry-storage-check@v2.1
with:
contract: contracts/compound/Morpho.sol:Morpho

- name: Check RewardsManager storage layout
uses: Rubilmax/foundry-storage-check@v2.1
with:
contract: contracts/compound/RewardsManager.sol:RewardsManager
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
name: Storage layout check (Morpho-Compound)
name: Storage layout snapshot check

on:
push:
branches:
- main
- dev
pull_request:
paths:
- lib/**
- contracts/common/**
- contracts/aave-v2/**
- "*.lock"
- remappings.txt
- .github/workflows/ci-storage-check-aave-v2.yml

jobs:
check:
name: storage layout check
name: morpho-aave-v2
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -29,14 +36,6 @@ jobs:
with:
version: nightly

- name: Foundry fork cache
uses: actions/cache@v3
with:
path: |
cache
out
key: foundry-${{ hashFiles('Makefile', 'foundry.toml') }} # where fork block numbers & RPC are stored

- name: Run tests
run: make storage-layout-check
env:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
name: Storage layout check (Morpho-Compound)
name: Storage layout snapshot check

on:
push:
branches:
- main
- dev
pull_request:
paths:
- lib/**
- contracts/common/**
- contracts/compound/**
- "*.lock"
- remappings.txt
- .github/workflows/ci-storage-check-compound.yml

jobs:
check:
name: storage layout check
name: morpho-compound
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -29,14 +36,6 @@ jobs:
with:
version: nightly

- name: Foundry fork cache
uses: actions/cache@v3
with:
path: |
cache
out
key: foundry-${{ hashFiles('Makefile', 'foundry.toml') }} # where fork block numbers & RPC are stored

- name: Run tests
run: make storage-layout-check
env:
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Morpho Core Protocol V1

[![Test](https://github.com/morpho-labs/morpho-contracts/actions/workflows/ci-foundry.yml/badge.svg)](https://github.com/morpho-dao/morpho-v1/actions/workflows/ci-foundry.yml)
[![Morpho-Compound](https://github.com/morpho-dao/morpho-v1/actions/workflows/ci-foundry-compound.yml/badge.svg)](https://github.com/morpho-dao/morpho-v1/actions/workflows/ci-foundry-compound.yml)
[![Morpho-AaveV2](https://github.com/morpho-dao/morpho-v1/actions/workflows/ci-foundry-aave-v2.yml/badge.svg)](https://github.com/morpho-dao/morpho-v1/actions/workflows/ci-foundry-aave-v2.yml)
[![Morpho-AaveV3](https://github.com/morpho-dao/morpho-v1/actions/workflows/ci-foundry-aave-v3.yml/badge.svg)](https://github.com/morpho-dao/morpho-v1/actions/workflows/ci-foundry-aave-v3.yml)

<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://i.imgur.com/uLq5V14.png">
Expand Down
Binary file not shown.
14 changes: 4 additions & 10 deletions contracts/aave-v2/EntryPositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ contract EntryPositionsManager is IEntryPositionsManager, PositionsManagerUtils

// Struct to avoid stack too deep.
struct SupplyVars {
bytes32 borrowMask;
uint256 remainingToSupply;
uint256 poolBorrowIndex;
uint256 toRepay;
Expand Down Expand Up @@ -91,17 +90,15 @@ contract EntryPositionsManager is IEntryPositionsManager, PositionsManagerUtils
) external {
if (_onBehalf == address(0)) revert AddressIsZero();
if (_amount == 0) revert AmountIsZero();
_updateIndexes(_poolToken);

SupplyVars memory vars;
vars.borrowMask = borrowMask[_poolToken];
if (!_isSupplying(userMarkets[_onBehalf], vars.borrowMask))
_setSupplying(_onBehalf, vars.borrowMask, true);
_updateIndexes(_poolToken);
_setSupplying(_onBehalf, borrowMask[_poolToken], true);

ERC20 underlyingToken = ERC20(market[_poolToken].underlyingToken);
underlyingToken.safeTransferFrom(_from, address(this), _amount);

Types.Delta storage delta = deltas[_poolToken];
SupplyVars memory vars;
vars.poolBorrowIndex = poolIndexes[_poolToken].poolBorrowIndex;
vars.remainingToSupply = _amount;

Expand Down Expand Up @@ -191,10 +188,7 @@ contract EntryPositionsManager is IEntryPositionsManager, PositionsManagerUtils
revert BorrowingNotEnabled();

_updateIndexes(_poolToken);

bytes32 borrowMask = borrowMask[_poolToken];
if (!_isBorrowing(userMarkets[msg.sender], borrowMask))
_setBorrowing(msg.sender, borrowMask, true);
_setBorrowing(msg.sender, borrowMask[_poolToken], true);

if (!_borrowAllowed(msg.sender, _poolToken, _amount)) revert UnauthorisedBorrow();

Expand Down
12 changes: 6 additions & 6 deletions contracts/aave-v2/ExitPositionsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {

if (!_withdrawAllowed(_supplier, _poolToken, toWithdraw)) revert UnauthorisedWithdraw();

_safeWithdrawLogic(_poolToken, toWithdraw, _supplier, _receiver, _maxGasForMatching);
_unsafeWithdrawLogic(_poolToken, toWithdraw, _supplier, _receiver, _maxGasForMatching);
}

/// @dev Implements repay logic with security checks.
Expand All @@ -165,7 +165,7 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {
uint256 toRepay = Math.min(_getUserBorrowBalanceInOf(_poolToken, _onBehalf), _amount);
if (toRepay == 0) revert UserNotMemberOfMarket();

_safeRepayLogic(_poolToken, _repayer, _onBehalf, toRepay, _maxGasForMatching);
_unsafeRepayLogic(_poolToken, _repayer, _onBehalf, toRepay, _maxGasForMatching);
}

/// @notice Liquidates a position.
Expand Down Expand Up @@ -237,8 +237,8 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {
.percentDiv(vars.liquidationBonus);
}

_safeRepayLogic(_poolTokenBorrowed, msg.sender, _borrower, amountToLiquidate, 0);
_safeWithdrawLogic(_poolTokenCollateral, amountToSeize, _borrower, msg.sender, 0);
_unsafeRepayLogic(_poolTokenBorrowed, msg.sender, _borrower, amountToLiquidate, 0);
_unsafeWithdrawLogic(_poolTokenCollateral, amountToSeize, _borrower, msg.sender, 0);

emit Liquidated(
msg.sender,
Expand All @@ -258,7 +258,7 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {
/// @param _supplier The address of the supplier.
/// @param _receiver The address of the user who will receive the tokens.
/// @param _maxGasForMatching The maximum amount of gas to consume within a matching engine loop.
function _safeWithdrawLogic(
function _unsafeWithdrawLogic(
address _poolToken,
uint256 _amount,
address _supplier,
Expand Down Expand Up @@ -412,7 +412,7 @@ contract ExitPositionsManager is IExitPositionsManager, PositionsManagerUtils {
/// @param _onBehalf The address of the account whose debt is repaid.
/// @param _amount The amount of token (in underlying).
/// @param _maxGasForMatching The maximum amount of gas to consume within a matching engine loop.
function _safeRepayLogic(
function _unsafeRepayLogic(
address _poolToken,
address _repayer,
address _onBehalf,
Expand Down
5 changes: 3 additions & 2 deletions contracts/aave-v2/InterestRatesManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ contract InterestRatesManager is IInterestRatesManager, MorphoStorage {
p2pGrowthFactor +
_params.reserveFactor.percentMul(poolBorrowGrowthFactor - p2pGrowthFactor);
} else {
// The case poolSupplyGrowthFactor > poolBorrowGrowthFactor happens because someone has done a flashloan on Aave:
// the peer-to-peer growth factors are set to the pool borrow growth factor.
// The case poolSupplyGrowthFactor > poolBorrowGrowthFactor happens because someone has done a flashloan on Aave, or the interests
// generated by the stable rate borrowing are high (making the supply rate higher than the variable borrow rate): the peer-to-peer
// growth factors are set to the pool borrow growth factor.
p2pSupplyGrowthFactor = poolBorrowGrowthFactor;
p2pBorrowGrowthFactor = poolBorrowGrowthFactor;
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/aave-v2/interfaces/IEntryPositionsManager.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GNU AGPLv3
pragma solidity ^0.8.0;
pragma solidity >=0.5.0;

interface IEntryPositionsManager {
function supplyLogic(
Expand Down
2 changes: 1 addition & 1 deletion contracts/aave-v2/interfaces/IExitPositionsManager.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GNU AGPLv3
pragma solidity ^0.8.0;
pragma solidity >=0.5.0;

interface IExitPositionsManager {
function withdrawLogic(
Expand Down
2 changes: 1 addition & 1 deletion contracts/aave-v2/interfaces/IGetterUnderlyingAsset.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: GNU AGPLv3
pragma solidity ^0.8.0;
pragma solidity >=0.5.0;

interface IGetterUnderlyingAsset {
function UNDERLYING_ASSET_ADDRESS() external view returns (address);
Expand Down
Loading

0 comments on commit 380a301

Please sign in to comment.