Skip to content

Commit

Permalink
Invariants Improvement (#1054)
Browse files Browse the repository at this point in the history
* Reduce max pool debt in invariants to 1e32 and add loans settlement for kicked borrowers if pool debt is greater than max pool debt

* Fix collateral to pledge in erc20 invariants
  • Loading branch information
prateek105 authored Jan 11, 2024
1 parent 9b52f52 commit f0b163c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ abstract contract UnboundedBasicERC20PoolHandler is UnboundedBasicPoolHandler, B

uint256 bucket = depositIndex - 1;
uint256 price = _poolInfo.indexToPrice(bucket);
uint256 collateralToPledge = ((COLLATERALIZATION_FACTOR * amount_ + price / 2) / price) * 101 / 100 + 1;
uint256 collateralToPledge = ((COLLATERALIZATION_FACTOR * amount_ + price / 2) / price) * 110 / 100 + _erc20Pool.collateralScale();

// ensure actor always has amount of collateral to pledge
_ensureCollateralAmount(_actor, collateralToPledge);
Expand Down
25 changes: 24 additions & 1 deletion tests/forge/invariants/base/handlers/unbounded/BaseHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ abstract contract BaseHandler is Test {
address internal _actor;
uint256 internal _lenderBucketIndex;
uint256 internal _limitIndex;
uint256 internal maxPoolDebt = uint256(vm.envOr("MAX_POOL_DEBT", uint256(1e55)));
uint256 internal maxPoolDebt = uint256(vm.envOr("MAX_POOL_DEBT", uint256(1e32)));

// deposits invariant test state
uint256[7389] internal fenwickDeposits;
Expand Down Expand Up @@ -217,6 +217,29 @@ abstract contract BaseHandler is Test {
time_ = constrictToRange(time_, 0, vm.envOr("SKIP_TIME", uint256(24 hours)));
vm.warp(block.timestamp + time_);
} else {
// settle kicked loans if pool debt exceeds configured max debt
// max loans settlement that can be done to prevent running out of gas
uint256 maxLoansSettlement = 5;

while(maxPoolDebt < poolDebt && maxLoansSettlement > 0) {
address kickedBorrower = _getAuctionInfo(address(0)).head;

if (kickedBorrower != address(0)) {
uint256 kickTime = _getAuctionInfo(kickedBorrower).kickTime;

if (kickTime < block.timestamp - 73 hours) {
try _pool.settle(kickedBorrower, buckets.length()) {
} catch (bytes memory err) {
_ensurePoolError(err);
}
}
}

(poolDebt, , ,) = _pool.debtInfo();

--maxLoansSettlement;
}

// repay from loans if pool debt exceeds configured max debt
// max repayments that can be done to prevent running out of gas
uint256 maxLoansRepayments = 5;
Expand Down

0 comments on commit f0b163c

Please sign in to comment.