Skip to content

Commit 99c89e7

Browse files
grandizzymwcIan Harvey
authored
invariant_reserves failure when take / bucketTake (#1050)
* Add test_regression_failure_reserves_bucketTake_18precision * Add regular take test failure * adjust reserves tolerance to allow for large inflators * fix issue with extreme TPs and reserves accrusing * Add test_regression_reservesTakeAction_failure * added tolerance for large inflator value in reserves change for takeAuction * added comments to regression test sequences * Commented change to reserves error margin * fixed up invariants.md to reflect code --------- Co-authored-by: mwc <[email protected]> Co-authored-by: Ian Harvey <[email protected]>
1 parent e39f93a commit 99c89e7

File tree

3 files changed

+3393
-2
lines changed

3 files changed

+3393
-2
lines changed

tests/INVARIANTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
- **R7**: Exchange rates are unchanged under depositTakes
6565
- **R8**: Exchange rates are unchanged under arbTakes
6666

67-
## Reserves (margin of 1e15 on comparisons)
67+
## Reserves (margin of 1e13 on comparisons)
6868
- **RE1**: Reserves are unchanged by pledging collateral
6969
- **RE2**: Reserves are unchanged by removing collateral
7070
- **RE3**: Reserves increase upon every deposit and when moving quote tokens into a lower-priced bucket.

tests/forge/invariants/base/handlers/unbounded/UnboundedLiquidationPoolHandler.sol

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,15 @@ abstract contract UnboundedLiquidationPoolHandler is BaseHandler {
196196
kickerReward += afterTakeVars.kickerBond - beforeTakeVars.kickerBond;
197197
}
198198

199+
// Reserves can increase by up to 2e-18 (1/5e17) due to rounding error in inflator value multiplied with t0Debt
200+
(uint256 inflator, ) = _pool.inflatorInfo();
201+
reservesErrorMargin = Math.max(reservesErrorMargin, inflator/5e17);
202+
199203
// **RE7**: Reserves increase with the quote token paid by taker.
200204
increaseInReserves += totalBalanceAfterTake - totalBalanceBeforeTake;
201205

202206
// **RE9**: Reserves unchanged by takes and bucket takes below TP(at the time of kick)
203-
if (auctionInfo.auctionPrice < auctionInfo.debtToCollateral) {
207+
if (auctionInfo.auctionPrice < Maths.min(auctionInfo.debtToCollateral, auctionInfo.neutralPrice)) {
204208
increaseInReserves = 0;
205209
decreaseInReserves = 0;
206210
}
@@ -294,6 +298,10 @@ abstract contract UnboundedLiquidationPoolHandler is BaseHandler {
294298
reservesErrorMargin = (beforeTakeVars.deposit - afterTakeVars.deposit) / auctionInfo.auctionPrice;
295299
}
296300

301+
// Reserves can increase by up to 2e-18 (1/5e17) due to rounding error in inflator value multiplied with t0Debt
302+
(uint256 inflator, ) = _pool.inflatorInfo();
303+
reservesErrorMargin = Math.max(reservesErrorMargin, inflator/5e17);
304+
297305
// In case of bucket take, collateral is taken at bucket price.
298306
uint256 takePrice = _priceAt(bucketIndex_);
299307

0 commit comments

Comments
 (0)