Skip to content
Merged
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
19 changes: 19 additions & 0 deletions certora/specs/Liveness.spec
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ function summarySafeTransferFrom(address token, address from, address to, uint25
}
}

function min(mathint a, mathint b) returns mathint {
return a < b ? a : b;
}

// Assume no fee.
// Summarize the accrue interest to avoid having to deal with reverts with absurdly high borrow rates.
function summaryAccrueInterest(env e, MorphoInternalAccess.MarketParams marketParams, MorphoInternalAccess.Id id) {
Expand Down Expand Up @@ -71,18 +75,21 @@ rule supplyChangesTokensAndShares(env e, MorphoInternalAccess.MarketParams marke

mathint sharesBefore = supplyShares(id, onBehalf);
mathint balanceBefore = myBalances[marketParams.loanToken];
mathint liquidityBefore = totalSupplyAssets(id) - totalBorrowAssets(id);

uint256 suppliedAssets;
uint256 suppliedShares;
suppliedAssets, suppliedShares = supply(e, marketParams, assets, shares, onBehalf, data);

mathint sharesAfter = supplyShares(id, onBehalf);
mathint balanceAfter = myBalances[marketParams.loanToken];
mathint liquidityAfter = totalSupplyAssets(id) - totalBorrowAssets(id);

assert assets != 0 => suppliedAssets == assets;
assert assets == 0 => suppliedShares == shares;
assert sharesAfter == sharesBefore + suppliedShares;
assert balanceAfter == balanceBefore + suppliedAssets;
assert liquidityAfter == liquidityBefore + suppliedAssets;
}

// Check that tokens and shares are properly accounted following a withdraw.
Expand All @@ -96,18 +103,21 @@ rule withdrawChangesTokensAndShares(env e, MorphoInternalAccess.MarketParams mar

mathint sharesBefore = supplyShares(id, onBehalf);
mathint balanceBefore = myBalances[marketParams.loanToken];
mathint liquidityBefore = totalSupplyAssets(id) - totalBorrowAssets(id);

uint256 withdrawnAssets;
uint256 withdrawnShares;
withdrawnAssets, withdrawnShares = withdraw(e, marketParams, assets, shares, onBehalf, receiver);

mathint sharesAfter = supplyShares(id, onBehalf);
mathint balanceAfter = myBalances[marketParams.loanToken];
mathint liquidityAfter = totalSupplyAssets(id) - totalBorrowAssets(id);

assert assets != 0 => withdrawnAssets == assets;
assert assets == 0 => withdrawnShares == shares;
assert sharesAfter == sharesBefore - withdrawnShares;
assert balanceAfter == balanceBefore - withdrawnAssets;
assert liquidityAfter == liquidityBefore - withdrawnAssets;
}

// Check that tokens and shares are properly accounted following a borrow.
Expand All @@ -121,18 +131,21 @@ rule borrowChangesTokensAndShares(env e, MorphoInternalAccess.MarketParams marke

mathint sharesBefore = borrowShares(id, onBehalf);
mathint balanceBefore = myBalances[marketParams.loanToken];
mathint liquidityBefore = totalSupplyAssets(id) - totalBorrowAssets(id);

uint256 borrowedAssets;
uint256 borrowedShares;
borrowedAssets, borrowedShares = borrow(e, marketParams, assets, shares, onBehalf, receiver);

mathint sharesAfter = borrowShares(id, onBehalf);
mathint balanceAfter = myBalances[marketParams.loanToken];
mathint liquidityAfter = totalSupplyAssets(id) - totalBorrowAssets(id);

assert assets != 0 => borrowedAssets == assets;
assert assets == 0 => borrowedShares == shares;
assert sharesAfter == sharesBefore + borrowedShares;
assert balanceAfter == balanceBefore - borrowedAssets;
assert liquidityAfter == liquidityBefore - borrowedAssets;
}

// Check that tokens and shares are properly accounted following a repay.
Expand All @@ -146,18 +159,24 @@ rule repayChangesTokensAndShares(env e, MorphoInternalAccess.MarketParams market

mathint sharesBefore = borrowShares(id, onBehalf);
mathint balanceBefore = myBalances[marketParams.loanToken];
mathint liquidityBefore = totalSupplyAssets(id) - totalBorrowAssets(id);

mathint borrowAssetsBefore = totalBorrowAssets(id);

uint256 repaidAssets;
uint256 repaidShares;
repaidAssets, repaidShares = repay(e, marketParams, assets, shares, onBehalf, data);

mathint sharesAfter = borrowShares(id, onBehalf);
mathint balanceAfter = myBalances[marketParams.loanToken];
mathint liquidityAfter = totalSupplyAssets(id) - totalBorrowAssets(id);

assert assets != 0 => repaidAssets == assets;
assert assets == 0 => repaidShares == shares;
assert sharesAfter == sharesBefore - repaidShares;
assert balanceAfter == balanceBefore + repaidAssets;
// Taking the min to handle the zeroFloorSub in the code.
assert liquidityAfter == liquidityBefore + min(repaidAssets, borrowAssetsBefore);
}

// Check that tokens and balances are properly accounted following a supplyCollateral.
Expand Down