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] Exit liquidity #316

Merged
merged 3 commits into from
Aug 16, 2023
Merged
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 @@ -43,3 +43,4 @@ jobs:
script:
- verifyBlue.sh
- verifyBlueRatioMathSummary.sh
- verifyBlueExitLiquidity.sh
8 changes: 8 additions & 0 deletions certora/harness/MorphoHarness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ contract MorphoHarness is Morpho {
return totalSupplyShares[id] + SharesMathLib.VIRTUAL_SHARES;
}

function getVirtualTotalBorrow(Id id) external view returns (uint256) {
return totalBorrow[id] + SharesMathLib.VIRTUAL_ASSETS;
}

function getVirtualTotalBorrowShares(Id id) external view returns (uint256) {
return totalBorrowShares[id] + SharesMathLib.VIRTUAL_SHARES;
}

function getMarketId(Market memory market) external pure returns (Id) {
return market.id();
}
Expand Down
12 changes: 12 additions & 0 deletions certora/scripts/verifyBlueExitLiquidity.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

set -euxo pipefail

certoraRun \
certora/harness/MorphoHarness.sol \
--verify MorphoHarness:certora/specs/BlueExitLiquidity.spec \
--solc_allow_path src \
--loop_iter 3 \
--optimistic_loop \
--msg "Morpho Blue Exit Liquidity" \
"$@"
62 changes: 62 additions & 0 deletions certora/specs/BlueExitLiquidity.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
methods {
function supplyShares(MorphoHarness.Id, address) external returns uint256 envfree;
function getVirtualTotalSupply(MorphoHarness.Id) external returns uint256 envfree;
function getVirtualTotalSupplyShares(MorphoHarness.Id) external returns uint256 envfree;
function borrowShares(MorphoHarness.Id, address) external returns uint256 envfree;
function getVirtualTotalBorrow(MorphoHarness.Id) external returns uint256 envfree;
function getVirtualTotalBorrowShares(MorphoHarness.Id) external returns uint256 envfree;
function collateral(MorphoHarness.Id, address) external returns uint256 envfree;

function lastUpdate(MorphoHarness.Id) external returns uint256 envfree;

function mathLibMulDivDown(uint256, uint256, uint256) external returns uint256 envfree;
function mathLibMulDivUp(uint256, uint256, uint256) external returns uint256 envfree;
function getMarketId(MorphoHarness.Market) external returns MorphoHarness.Id envfree;
}

rule withdrawLiquidity(MorphoHarness.Market market, uint256 assets, uint256 shares, address onBehalf, address receiver) {
env e;
MorphoHarness.Id id = getMarketId(market);

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

uint256 initialShares = supplyShares(id, onBehalf);
uint256 initialTotalSupply = getVirtualTotalSupply(id);
uint256 initialTotalSupplyShares = getVirtualTotalSupplyShares(id);
uint256 owedAssets = mathLibMulDivDown(initialShares, initialTotalSupply, initialTotalSupplyShares);

uint256 withdrawnAssets;
withdrawnAssets, _ = withdraw(e, market, assets, shares, onBehalf, receiver);

assert withdrawnAssets <= owedAssets;
}

rule withdrawCollateralLiquidity(MorphoHarness.Market market, uint256 assets, address onBehalf, address receiver) {
env e;
MorphoHarness.Id id = getMarketId(market);

uint256 initialCollateral = collateral(id, onBehalf);

withdrawCollateral(e, market, assets, onBehalf, receiver);

assert assets <= initialCollateral;
}

rule repayLiquidity(MorphoHarness.Market market, uint256 assets, uint256 shares, address onBehalf, bytes data) {
env e;
MorphoHarness.Id id = getMarketId(market);

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

uint256 initialShares = borrowShares(id, onBehalf);
uint256 initialTotalBorrow = getVirtualTotalBorrow(id);
uint256 initialTotalBorrowShares = getVirtualTotalBorrowShares(id);
uint256 assetsDue = mathLibMulDivUp(initialShares, initialTotalBorrow, initialTotalBorrowShares);

uint256 repaidAssets;
repaidAssets, _ = repay(e, market, assets, shares, onBehalf, data);

require borrowShares(id, onBehalf) == 0;

assert repaidAssets >= assetsDue;
}