Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Certora] Supply cap #419

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/certora.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
- Reentrancy
- Reverts
- Roles
- SupplyCap
- Timelock
- Tokens

Expand Down
28 changes: 28 additions & 0 deletions certora/confs/SupplyCap.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"files": [
"lib/morpho-blue/certora/harness/MorphoHarness.sol",
"certora/helpers/MetaMorphoHarness.sol",
"certora/helpers/Util.sol",
"certora/dispatch/ERC20Standard.sol",
],
"parametric_contracts": [
"MetaMorphoHarness",
],
"link": [
"MetaMorphoHarness:MORPHO=MorphoHarness",
],
"solc_map": {
"MorphoHarness": "solc-0.8.19",
"MetaMorphoHarness": "solc-0.8.21",
"Util": "solc-0.8.21",
"ERC20Standard": "solc-0.8.21",
},
"verify": "MetaMorphoHarness:certora/specs/SupplyCap.spec",
"loop_iter": "2",
"optimistic_loop": true,
"nondet_difficult_funcs": true,
"nondet_minimal_difficulty": "50",
"rule_sanity": "basic",
"server": "production",
"msg": "MetaMorpho Supply Cap",
}
4 changes: 4 additions & 0 deletions certora/helpers/Util.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ contract Util {
function libId(MarketParams memory marketParams) external pure returns (Id) {
return marketParams.id();
}

function toAssetsDown(uint256 shares, uint256 totalAssets, uint256 totalShares) external pure returns (uint256) {
return shares.toAssetsDown(totalAssets, totalShares);
}
}
65 changes: 65 additions & 0 deletions certora/specs/SupplyCap.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// SPDX-License-Identifier: GPL-2.0-or-later

using MorphoHarness as Morpho;
using Util as Util;

methods {
function MORPHO() external returns(address) envfree;
function config_(MetaMorphoHarness.Id) external returns(MetaMorphoHarness.MarketConfig) envfree;

function Morpho.idToMarketParams(MetaMorphoHarness.Id) external returns(address, address, address, address, uint256) envfree;
function Morpho.lastUpdate(MetaMorphoHarness.Id) external returns(uint256) envfree;
function Morpho.supplyShares(MetaMorphoHarness.Id, address) external returns(uint256) envfree;
function Morpho.totalSupplyAssets(MetaMorphoHarness.Id) external returns(uint256) envfree;
function Morpho.totalSupplyShares(MetaMorphoHarness.Id) external returns(uint256) envfree;

function Util.libId(MetaMorphoHarness.MarketParams) external returns(MetaMorphoHarness.Id) envfree;
function Util.toAssetsDown(uint256, uint256, uint256) external returns(uint256) envfree;

function _.expectedSupplyAssets(MetaMorphoHarness.MarketParams marketParams, address user) external => supplyAssets(marketParams, user) expect(uint256);

function _.balanceOf(address) external => DISPATCHER(true);
function _.transfer(address, uint256) external => DISPATCHER(true);
function _.transferFrom(address, address, uint256) external => DISPATCHER(true);
}

hook Sload uint184 cap config[KEY MetaMorphoHarness.Id id].cap {
address loanToken; address collateralToken; address oracle; address irm; uint256 lltv;
(loanToken, collateralToken, oracle, irm, lltv) = Morpho.idToMarketParams(id);

MetaMorphoHarness.MarketParams marketParams;
require loanToken == marketParams.loanToken;
require collateralToken == marketParams.collateralToken;
require oracle == marketParams.oracle;
require irm == marketParams.irm;
require lltv == marketParams.lltv;

require Util.libId(marketParams) == id;
}

function supplyAssets(MetaMorphoHarness.MarketParams marketParams, address user) returns uint256 {
MetaMorphoHarness.Id id = Util.libId(marketParams);
uint256 shares = Morpho.supplyShares(id, user);
uint256 totalSupplyAssets = Morpho.totalSupplyAssets(id);
uint256 totalSupplyShares = Morpho.totalSupplyShares(id);
require shares <= totalSupplyShares;
return Util.toAssetsDown(shares, totalSupplyAssets, totalSupplyShares);
}

rule respectSupplyCap(method f, env e, calldataarg args)
{
MetaMorphoHarness.MarketParams marketParams;
MetaMorphoHarness.Id id = Util.libId(marketParams);

address morpho = MORPHO();
uint256 cap = config_(id).cap;

require Morpho.lastUpdate(id) == e.block.timestamp;

require supplyAssets(marketParams, currentContract) <= cap;

f(e, args);
require assert_uint256(config_(id).cap) == cap;

assert supplyAssets(marketParams, currentContract) <= cap;
}
Loading