Skip to content

Commit

Permalink
feat: executable liquidate, attempt prove buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
QGarchery committed Dec 9, 2024
1 parent a4210e9 commit ba2280f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
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/harness/MorphoHarness.sol",
"certora/harness/Util.sol"
],
"solc": "solc-0.8.19",
"verify": "MorphoHarness:certora/specs/LiquidateBuffer.spec",
"prover_args": [
"-depth 5",
"-mediumTimeout 5",
"-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:lia2]"
],
"rule_sanity": "basic",
"server": "production",
"msg": "Morpho Blue Liquidate Buffer"
}
10 changes: 10 additions & 0 deletions certora/harness/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
42 changes: 42 additions & 0 deletions certora/specs/LiquidateBuffer.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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;
}

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 repaidShares;
(seizedAssets, repaidShares) = liquidate(e, marketParams, borrower, seizedAssetsInput, repaidSharesInput, data);

require !priceChanged;
assert repaidShares * borrowerCollateral >= seizedAssets * borrowerShares;
// assert borrowerShares * newBorrowerCollateral >= newBorrowerShares * borrowerCollateral;
// assert newTotalShares * OldVirtualTotalBorrowAssets >= newTotalAssets * OldVirtualTotalBorrowShares;

}

0 comments on commit ba2280f

Please sign in to comment.