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] Liquidate buffer, with executable code #708

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft
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 @@ -23,6 +23,7 @@ jobs:
- ExchangeRate
- Health
- LibSummary
- LiquidateBuffer
- Liveness
- Reentrancy
- Reverts
Expand Down
19 changes: 19 additions & 0 deletions certora/confs/LiquidateBuffer.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"files": [
"certora/helpers/MorphoHarness.sol",
"certora/helpers/Util.sol"
],
"solc": "solc-0.8.19",
"verify": "MorphoHarness:certora/specs/LiquidateBuffer.spec",
"prover_args": [
"-depth 5",
"-mediumTimeout 20",
"-timeout 3600",
"-adaptiveSolverConfig false",
"-smt_nonLinearArithmetic true",
"-solvers [z3:def{randomSeed=1},z3:def{randomSeed=2},z3:def{randomSeed=3},z3:def{randomSeed=4},z3:def{randomSeed=5},z3:def{randomSeed=6},z3:def{randomSeed=7},z3:def{randomSeed=8},z3:def{randomSeed=9},z3:def{randomSeed=10}]"
],
"rule_sanity": "basic",
"server": "production",
"msg": "Morpho Blue Liquidate Buffer"
}
10 changes: 10 additions & 0 deletions certora/helpers/Util.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "../../src/libraries/UtilsLib.sol";

contract Util {
using MarketParamsLib for MarketParams;
using MathLib for uint256;

function wad() external pure returns (uint256) {
return WAD;
Expand All @@ -17,6 +18,15 @@ contract Util {
return MAX_FEE;
}

function oraclePriceScale() external pure returns (uint256) {
return ORACLE_PRICE_SCALE;
}

function lif(uint256 lltv) external pure returns (uint256) {
return
UtilsLib.min(MAX_LIQUIDATION_INCENTIVE_FACTOR, WAD.wDivDown(WAD - LIQUIDATION_CURSOR.wMulDown(WAD - lltv)));
}

function libId(MarketParams memory marketParams) external pure returns (Id) {
return marketParams.id();
}
Expand Down
48 changes: 48 additions & 0 deletions certora/specs/LiquidateBuffer.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: GPL-2.0-or-later

import "Health.spec";

methods {
function Util.lif(uint256) external returns (uint256) envfree;
function Util.oraclePriceScale() external returns (uint256) envfree;
function Util.wad() external returns (uint256) envfree;
function Morpho._isHealthy(MorphoHarness.MarketParams memory, MorphoHarness.Id,address) internal returns (bool) => NONDET;
function Morpho._accrueInterest(MorphoHarness.MarketParams memory, MorphoHarness.Id) internal => NONDET;
}

rule liquidateImprovePosition(env e, MorphoHarness.MarketParams marketParams, address borrower, uint256 seizedAssetsInput, uint256 repaidSharesInput, bytes data) {
// Assume no callback for simplicity.
require data.length == 0;

MorphoHarness.Id id = Util.libId(marketParams);

// We place ourselves at the last block for getting the following variables.
require lastUpdate(id) == e.block.timestamp;

uint256 borrowerShares = borrowShares(id, borrower);
require borrowerShares <= totalBorrowShares(id);

uint256 borrowerCollateral = collateral(id, borrower);
uint256 collateralPrice = mockPrice();
uint256 lif = Util.lif(marketParams.lltv);

uint256 borrowerAssets = summaryMulDivUp(borrowerShares, virtualTotalBorrowAssets(id), virtualTotalBorrowShares(id));
uint256 borrowerCollateralQuoted = summaryMulDivDown(borrowerCollateral, collateralPrice, Util.oraclePriceScale());

require summaryMulDivUp(lif, borrowerAssets, Util.wad()) < borrowerCollateralQuoted;
assert borrowerCollateral * collateralPrice * virtualTotalBorrowShares(id) * Util.wad() > borrowerShares * Util.oraclePriceScale() * virtualTotalBorrowAssets(id) * lif;

uint256 seizedAssets;
uint256 repaidAssets;
(seizedAssets, _) = liquidate(e, marketParams, borrower, seizedAssetsInput, repaidSharesInput, data);

// uint256 newBorrowerShares = borrowShares(id, borrower);
uint256 repaidShares = assert_uint256(borrowerShares - newBorrowerShares);

require !priceChanged;
require collateral(id, borrower) != 0;
assert repaidShares * borrowerCollateral >= seizedAssets * borrowerShares;
// assert borrowerShares * newBorrowerCollateral >= newBorrowerShares * borrowerCollateral;
// assert newTotalShares * OldVirtualTotalBorrowAssets >= newTotalAssets * OldVirtualTotalBorrowShares;

}
Loading