-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #708 from morpho-org/certora/exec-liquidate-buffer
[Certora] Liquidate buffer, with executable code
- Loading branch information
Showing
5 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ jobs: | |
- ExchangeRate | ||
- Health | ||
- LibSummary | ||
- LiquidateBuffer | ||
- Liveness | ||
- Reentrancy | ||
- Reverts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"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", | ||
"-destructiveOptimizations twostage", | ||
"-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}]" | ||
], | ||
"multi_assert_check": true, | ||
"rule_sanity": "basic", | ||
"server": "production", | ||
"msg": "Morpho Blue Liquidate Buffer" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
using Util as Util; | ||
|
||
methods { | ||
function extSloads(bytes32[]) external returns (bytes32[]) => NONDET DELETE; | ||
|
||
function lastUpdate(MorphoHarness.Id) external returns (uint256) envfree; | ||
function borrowShares(MorphoHarness.Id, address) external returns (uint256) envfree; | ||
function collateral(MorphoHarness.Id, address) external returns (uint256) envfree; | ||
function totalBorrowShares(MorphoHarness.Id) external returns (uint256) envfree; | ||
function totalBorrowAssets(MorphoHarness.Id) external returns (uint256) envfree; | ||
function virtualTotalBorrowAssets(MorphoHarness.Id) external returns uint256 envfree; | ||
function virtualTotalBorrowShares(MorphoHarness.Id) external returns uint256 envfree; | ||
|
||
function Util.libId(MorphoHarness.MarketParams) external returns (MorphoHarness.Id) envfree; | ||
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; | ||
|
||
function _.price() external => constantPrice expect uint256; | ||
} | ||
|
||
persistent ghost uint256 constantPrice; | ||
|
||
// Check that for a position with LTV < 1 / LIF, its health improves after a liquidation. | ||
rule liquidateImprovePosition(env e, MorphoHarness.MarketParams marketParams, address borrower, uint256 seizedAssetsInput, uint256 repaidSharesInput, bytes data) { | ||
// Assume no callback. | ||
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); | ||
// Safe require because of the sumBorrowSharesCorrect invariant. | ||
require borrowerShares <= totalBorrowShares(id); | ||
|
||
uint256 borrowerCollateral = collateral(id, borrower); | ||
uint256 lif = Util.lif(marketParams.lltv); | ||
uint256 virtualTotalAssets = virtualTotalBorrowAssets(id); | ||
uint256 virtualTotalShares = virtualTotalBorrowShares(id); | ||
|
||
// Let borrowerAssets = borrowerShares * virtualTotalAssets / virtualTotalShares | ||
// and borrowerCollateralQuoted = borrowerCollateral * constantPrice / Util.oraclePriceScale() | ||
// then the following line is the assumption borrowerAssets / borrowerCollateralQuoted < 1 / LIF. | ||
require borrowerCollateral * constantPrice * virtualTotalShares * Util.wad() > borrowerShares * Util.oraclePriceScale() * virtualTotalAssets * lif; | ||
|
||
uint256 seizedAssets; | ||
(seizedAssets, _) = liquidate(e, marketParams, borrower, seizedAssetsInput, repaidSharesInput, data); | ||
|
||
uint256 newBorrowerShares = borrowShares(id, borrower); | ||
uint256 newBorrowerCollateral = collateral(id, borrower); | ||
uint256 repaidShares = assert_uint256(borrowerShares - newBorrowerShares); | ||
uint256 newVirtualTotalAssets = virtualTotalBorrowAssets(id); | ||
uint256 newVirtualTotalShares = virtualTotalBorrowShares(id); | ||
|
||
// Hint for the prover to show that there is no bad debt realization. | ||
assert newBorrowerCollateral != 0; | ||
// Hint for the prover about the ratio used to close the position. | ||
assert repaidShares * borrowerCollateral >= seizedAssets * borrowerShares; | ||
// Prove that the ratio of shares of debt over collateral is smaller after the liquidation. | ||
assert borrowerShares * newBorrowerCollateral >= newBorrowerShares * borrowerCollateral; | ||
// Prove that the value of borrow shares is smaller after the liquidation. | ||
// Note that this is only shown for the case where there are still borrow positions on the markets. | ||
assert totalBorrowAssets(id) > 0 => newVirtualTotalShares * virtualTotalAssets >= newVirtualTotalAssets * virtualTotalShares; | ||
} |