Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove from batch issues #369

Merged
merged 4 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions contracts/src/BorrowerOperations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1034,17 +1034,21 @@ contract BorrowerOperations is LiquityBase, AddRemoveManagers, IBorrowerOperatio
uint256 _maxUpfrontFee
) public override {
_requireIsNotShutDown();
_requireCallerIsBorrower(_troveId);

LocalVariables_removeFromBatch memory vars;
vars.troveManager = troveManager;
vars.sortedTroves = sortedTroves;

vars.batchManager = interestBatchManagerOf[_troveId];
_requireTroveIsActive(vars.troveManager, _troveId);
_requireCallerIsBorrower(_troveId);
_requireValidAnnualInterestRate(_newAnnualInterestRate);

vars.batchManager = _requireIsInBatch(_troveId);
delete interestBatchManagerOf[_troveId];

// Remove trove from Batch in SortedTroves
vars.sortedTroves.removeFromBatch(_troveId);
// Reinsert as single trove
vars.sortedTroves.insert(_troveId, _newAnnualInterestRate, _upperHint, _lowerHint);

vars.trove = vars.troveManager.getLatestTroveData(_troveId);
Expand Down
15 changes: 4 additions & 11 deletions contracts/src/test/TestContracts/InvariantsTestHandler.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {
LatestTroveData t;
address batchManager;
uint256 batchManagementFee;
bool wasOpen;
bool wasActive;
bool premature;
uint256 upfrontFee;
Expand Down Expand Up @@ -1930,7 +1929,6 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {
v.t = v.c.troveManager.getLatestTroveData(v.troveId);
v.batchManager = _batchManagerOf[i][v.troveId];
v.batchManagementFee = v.c.troveManager.getLatestBatchData(v.batchManager).accruedManagementFee;
v.wasOpen = _isOpen(i, v.troveId);
v.wasActive = _isActive(i, v.troveId);
v.premature = _timeSinceLastBatchInterestRateAdjustment[i][v.batchManager] < INTEREST_RATE_ADJ_COOLDOWN;
v.upfrontFee = v.batchManager != address(0)
Expand Down Expand Up @@ -1984,15 +1982,6 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {

// Effects (system)
_mintYield(i, v.pendingInterest, v.upfrontFee);
} catch Error(string memory reason) {
v.errorString = reason;

// Justify failures
if (reason.equals("ERC721: invalid token ID")) {
assertFalse(v.wasOpen, "Open Trove should have an NFT");
} else {
revert(reason);
}
} catch (bytes memory revertData) {
bytes4 selector;
(selector, v.errorString) = _decodeCustomError(revertData);
Expand Down Expand Up @@ -2781,6 +2770,10 @@ contract InvariantsTestHandler is BaseHandler, BaseMultiCollateralTest {
return (selector, "BorrowerOperations.TroveInBatch()");
}

if (selector == BorrowerOperations.TroveNotInBatch.selector) {
return (selector, "BorrowerOperations.TroveNotInBatch()");
}

if (selector == BorrowerOperations.InterestNotInRange.selector) {
return (selector, "BorrowerOperations.InterestNotInRange()");
}
Expand Down
45 changes: 45 additions & 0 deletions contracts/src/test/interestBatchManagement.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,51 @@ contract InterestBatchManagementTest is DevTestSetup {
assertEq(annualInterestRate, newAnnualInterestRate, "Wrong interest rate");
}

function testRemoveBatchManagerFailsIfNewRateIsTooLow() public {
uint256 troveId = openTroveAndJoinBatchManager();
(,,,,,,,, address tmBatchManagerAddress,) = troveManager.Troves(troveId);
LatestTroveData memory trove = troveManager.getLatestTroveData(troveId);
uint256 annualInterestRate = trove.annualInterestRate;

uint256 newAnnualInterestRate = 4e15;
assertEq(tmBatchManagerAddress, B, "Wrong batch manager in TM");
assertNotEq(newAnnualInterestRate, annualInterestRate, "New interest rate should be different");

vm.startPrank(A);
vm.expectRevert(BorrowerOperations.InterestRateTooLow.selector);
borrowerOperations.removeFromBatch(troveId, newAnnualInterestRate, 0, 0, 1e24);
vm.stopPrank();
}

function testRemoveBatchManagerFailsIfNewRateIsTooHigh() public {
uint256 troveId = openTroveAndJoinBatchManager();
(,,,,,,,, address tmBatchManagerAddress,) = troveManager.Troves(troveId);
LatestTroveData memory trove = troveManager.getLatestTroveData(troveId);
uint256 annualInterestRate = trove.annualInterestRate;

uint256 newAnnualInterestRate = 101e16;
assertEq(tmBatchManagerAddress, B, "Wrong batch manager in TM");
assertNotEq(newAnnualInterestRate, annualInterestRate, "New interest rate should be different");

vm.startPrank(A);
vm.expectRevert(BorrowerOperations.InterestRateTooHigh.selector);
borrowerOperations.removeFromBatch(troveId, newAnnualInterestRate, 0, 0, 1e24);
vm.stopPrank();
}

function testRemoveBatchManagerFailsIfTroveNotInBatch() public {
uint256 troveId = openTroveNoHints100pct(A, 100e18, 5000e18, 5e16);
(,,,,,,,, address tmBatchManagerAddress,) = troveManager.Troves(troveId);

uint256 newAnnualInterestRate = 4e16;
assertEq(tmBatchManagerAddress, address(0), "Wrong batch manager in TM");

vm.startPrank(A);
vm.expectRevert(BorrowerOperations.TroveNotInBatch.selector);
borrowerOperations.removeFromBatch(troveId, newAnnualInterestRate, 0, 0, 1e24);
vm.stopPrank();
}

function testOnlyBorrowerCanSetBatchManager() public {
registerBatchManager(A);
registerBatchManager(B);
Expand Down
Loading