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

fix: Make batch shares rounding favour the system #549

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion contracts/src/TroveManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

pragma solidity 0.8.24;

import "openzeppelin-contracts/contracts/utils/math/Math.sol";

import "./Interfaces/ITroveManager.sol";
import "./Interfaces/IAddressesRegistry.sol";
import "./Interfaces/IStabilityPool.sol";
Expand Down Expand Up @@ -1793,7 +1795,7 @@ contract TroveManager is LiquityBase, ITroveManager, ITroveEvents {
// To avoid rebasing issues, let’s make sure the ratio debt / shares is not too high
_requireBelowMaxSharesRatio(currentBatchDebtShares, _batchDebt, _checkBatchSharesRatio);

batchDebtSharesDelta = currentBatchDebtShares * debtIncrease / _batchDebt;
batchDebtSharesDelta = Math.ceilDiv(currentBatchDebtShares * debtIncrease, _batchDebt);
}

Troves[_troveId].batchDebtShares += batchDebtSharesDelta;
Expand Down
19 changes: 5 additions & 14 deletions contracts/src/test/rebasingBatchShares.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract RebasingBatchShares is DevTestSetup {
LatestBatchData memory b4Batch = troveManager.getLatestBatchData(address(B));

// TODO: Open A, Mint 1 extra (forgiven to A)
_addOneDebtAndEnsureItDoesntMintShares(ATroveId, A);
_addOneDebtAndEnsureItMintsShares(ATroveId, A);
/// @audit MED impact

LatestBatchData memory afterBatch = troveManager.getLatestBatchData(address(B));
Expand Down Expand Up @@ -178,6 +178,7 @@ contract RebasingBatchShares is DevTestSetup {
closeTrove(A, ATroveId); // Close A (also remove from batch is fine)

LatestTroveData memory troveAfter = troveManager.getLatestTroveData(BTroveId);
//assertGt(troveAfter.entireDebt, troveBefore.entireDebt, "we're rebasing");
assertEq(troveAfter.entireDebt, troveBefore.entireDebt, "rebasing is not working");
assertEq(troveAfter.entireDebt, 0, "trove B was closed");
}
Expand All @@ -192,18 +193,8 @@ contract RebasingBatchShares is DevTestSetup {
assertLt(b4BatchDebtShares, afterBatchDebtShares, "Shares should increase");
}

function _addDebtAndEnsureItDoesntMintShares(uint256 troveId, address caller, uint256 amt) internal {
(,,,,,,,,, uint256 b4BatchDebtShares) = troveManager.Troves(troveId);

withdrawBold100pct(caller, troveId, amt);

(,,,,,,,,, uint256 afterBatchDebtShares) = troveManager.Troves(troveId);

assertEq(b4BatchDebtShares, afterBatchDebtShares, "Same Shares");
}

function _addOneDebtAndEnsureItDoesntMintShares(uint256 troveId, address caller) internal {
_addDebtAndEnsureItDoesntMintShares(troveId, caller, 1);
function _addOneDebtAndEnsureItMintsShares(uint256 troveId, address caller) internal {
_addDebtAndEnsureItMintsShares(troveId, caller, 1);
}

function _getLatestBatchDebt(address batch) internal view returns (uint256) {
Expand Down Expand Up @@ -272,7 +263,7 @@ contract RebasingBatchShares is DevTestSetup {
if (WITH_INTEREST) {
vm.warp(block.timestamp + 12);
}
_addOneDebtAndEnsureItDoesntMintShares(BTroveId, B);
_addOneDebtAndEnsureItMintsShares(BTroveId, B);

(uint256 debtBefore,,,,,,, uint256 allBatchDebtSharesBefore) = troveManager.getBatch(B);
uint256 sharesBeforeRepay = _getBatchDebtShares(BTroveId);
Expand Down
Loading