Skip to content

Commit

Permalink
Fix HTP calculation in invariant logic (#1042)
Browse files Browse the repository at this point in the history
* deposits invariant failure on kick for both pool types

* 18-decimal deposit on kick failure

* Fix HTP calculation in _fenwickAccrueInterest

* Update test comments

---------

Co-authored-by: Ed Noepel <[email protected]>
  • Loading branch information
grandizzy and EdNoepel authored Dec 18, 2023
1 parent 952c083 commit 7506ef5
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
MAX_PRICE,
MIN_PRICE,
_indexOf,
_htp,
_isCollateralized
} from 'src/libraries/helpers/PoolHelper.sol';
import { Maths } from 'src/libraries/internal/Maths.sol';
Expand Down Expand Up @@ -546,7 +547,8 @@ abstract contract BaseHandler is Test {
if (pendingFactor == 1e18) return;

// get TP of worst loan
uint256 htp = _poolInfo.htp(address(_pool));
(uint256 inflator, ) = _pool.inflatorInfo();
uint256 htp = _htp(_getLoansInfo().maxT0DebtToCollateral, inflator);

uint256 accrualIndex;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,6 @@ contract RegressionTestLiquidationWith10BucketsERC20Pool is LiquidationERC20Pool

}


contract RegressionTestLiquidationWith20BucketsERC20Pool is LiquidationERC20PoolInvariants {

function setUp() public override {
Expand Down Expand Up @@ -2166,8 +2165,8 @@ contract RegressionTestReserveEvmRevertERC720Pool is LiquidationERC20PoolInvaria
invariant_auction();
}
}
contract RegressionTestLiquidationWith18QuotePrecision4CollateralPrecisionERC20Pool is LiquidationERC20PoolInvariants {

contract RegressionTestLiquidationWith18QuotePrecision4CollateralPrecisionERC20Pool is LiquidationERC20PoolInvariants {
function setUp() public override {
vm.setEnv("QUOTE_PRECISION", "18");
vm.setEnv("COLLATERAL_PRECISION", "4");
Expand All @@ -2193,7 +2192,6 @@ contract RegressionTestLiquidationWith18QuotePrecision4CollateralPrecisionERC20P

invariant_exchange_rate();
}

}

contract RegressionTestLiquidationWith8CollateralPrecisionERC20Pool is LiquidationERC20PoolInvariants {
Expand All @@ -2207,6 +2205,47 @@ contract RegressionTestLiquidationWith8CollateralPrecisionERC20Pool is Liquidati
_liquidationERC20PoolHandler.kickAuction(0, 17104183949941796511, 0, 280417724462859438244277310225109843561163957492078708458152152913172346);
_liquidationERC20PoolHandler.pledgeCollateral(0, 140265038788604003138859102577, 115792089237316195423570985008687907853269984665640564039457584007913129639932);
_liquidationERC20PoolHandler.bucketTake(5683872789780248582962257270041, 1, true, 115792089237316195423570985008687907853269984665640564039457584007913129639933, 115792089237316195423570985008687907853269984665640564039457584007913129639933);

invariant_exchange_rate();
}
}

contract RegressionTestLiquidationWith12QuotePrecisionERC20Pool is LiquidationERC20PoolInvariants {
function setUp() public override {
vm.setEnv("QUOTE_PRECISION", "12");
vm.setEnv("COLLATERAL_PRECISION", "18");
super.setUp();
}

/**
Test was failing with underflow. Resolved by invariant improvements in PR#955.
*/
function test_regression_take_exchange_rate_underflow() external {
_liquidationERC20PoolHandler.pullCollateral(1, 3, 115792089237316195423570985008687907853269984665640564039457584007913129639934);
_liquidationERC20PoolHandler.transferLps(115792089237316195423570985008687907853269984665640564039457584007913129639932, 45909730277988842415738220537184944537676, 1, 0, 0);
_liquidationERC20PoolHandler.kickAuction(70010871234460820744715912936890564712867991293425383626279454116593850533, 6499767, 28762655686049589541294942431851230420669838142, 1571345);
_liquidationERC20PoolHandler.addQuoteToken(181090242960530, 2377505175853220826817239127143875084418686811649475368310406086770995769749, 115792089237316195423570985008687907853269984665640564039457584007913129639932, 72033836997607978984292992820571);
_liquidationERC20PoolHandler.withdrawBonds(695237110902130656531864435588814770662540280753085713853371273119165032, 76187813332501807831094303, 12572696008628451448752492);
_liquidationERC20PoolHandler.transferLps(115792089237316195423570985008687907853269984665640564039457584007913129639935, 115792089237316195423570985008687907853269984665640564039457584007913129639934, 1, 2, 9);
_liquidationERC20PoolHandler.kickAuction(1999999999999999999999043403500806748920639352, 3061195076792591263023058803418546501128, 2379220920797631821002222390458479008558342658478194460908722057821, 31903477196625138712736675);
_liquidationERC20PoolHandler.takeAuction(2, 885557492487161046, 1558101439297927368671261, 1292998037045564783449417757876861668872);

invariant_exchange_rate();
}

/*
Test was failing because invariant logic for interest accrual was using the pending inflator to calculate HTP.
Fixed by calculating HTP using current pool inflator.
*/
function test_regression_failure_deposits_on_kick_1() external {
_liquidationERC20PoolHandler.drawDebt(2, 3221259065514816790487893458767898964, 4116413047421407597131232320880820919452009);
_liquidationERC20PoolHandler.withdrawBonds(1011868801812554352, 787959216853107061817696631727, 787949941098203131552111773821);
_liquidationERC20PoolHandler.lenderKickAuction(1013350938108023449, 709545743875727362579966764062968642140163108918930414591019589180725257, 1000019286889406901);
_liquidationERC20PoolHandler.pledgeCollateral(702496096288315654151051035260624996909623141681685150581265170070799900, 1025766506493781142223686516002, 1882017544514540473);
_liquidationERC20PoolHandler.addQuoteToken(184116457409845008802273399124418717054, 425420151153612354346298303754178903917488200526, 1524671197004160260571450442257224437845065293837405292129734683, 18328493294);
_liquidationERC20PoolHandler.pledgeCollateral(4184073188804616272013253095810669088604050280846044378968594414041510976844, 15476914321184942485933234133477, 999999999999999999999999999999999999999962315);
_liquidationERC20PoolHandler.kickAuction(2748385666284590326694, 1985130059088839548857545717, 2650077542301436190503272880334, 962354226098822);

invariant_fenwick();
}
}
Loading

0 comments on commit 7506ef5

Please sign in to comment.