Skip to content

Commit

Permalink
feat: helper function for calculating debt-in-front
Browse files Browse the repository at this point in the history
  • Loading branch information
danielattilasimon committed Aug 28, 2024
1 parent 351a849 commit 99b8b00
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions contracts/src/Interfaces/IMultiTroveGetter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@ interface IMultiTroveGetter {
uint256 snapshotBoldDebt;
}

struct DebtPerInterestRate {
uint256 interestRate;
uint256 debt;
}

function getMultipleSortedTroves(uint256 _collIndex, int256 _startIdx, uint256 _count)
external
view
returns (CombinedTroveData[] memory _troves);

function getDebtPerInterestRateAscending(uint256 _collIndex, uint256 _startId, uint256 _maxIterations)
external
view
returns (DebtPerInterestRate[] memory, uint256 currId);
}
25 changes: 25 additions & 0 deletions contracts/src/MultiTroveGetter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity 0.8.18;

import "./Interfaces/ICollateralRegistry.sol";
import "./Interfaces/IMultiTroveGetter.sol";
import "./Types/BatchId.sol";

/* Helper contract for grabbing Trove data for the front end. Not part of the core Liquity system. */
contract MultiTroveGetter is IMultiTroveGetter {
Expand Down Expand Up @@ -112,4 +113,28 @@ contract MultiTroveGetter is IMultiTroveGetter {
currentTroveId = _sortedTroves.getPrev(currentTroveId);
}
}

function getDebtPerInterestRateAscending(uint256 _collIndex, uint256 _startId, uint256 _maxIterations)
external
view
returns (DebtPerInterestRate[] memory data, uint256 currId)
{
ITroveManager troveManager = collateralRegistry.getTroveManager(_collIndex);
require(address(troveManager) != address(0), "Invalid collateral index");

ISortedTroves sortedTroves = troveManager.sortedTroves();
assert(address(sortedTroves) != address(0));

data = new DebtPerInterestRate[](_maxIterations);
currId = _startId;

for (uint256 i = 0; i < _maxIterations; ++i) {
currId = sortedTroves.getPrev(currId);
if (currId == 0) break;

LatestTroveData memory trove = troveManager.getLatestTroveData(currId);
data[i].interestRate = trove.annualInterestRate;
data[i].debt = trove.entireDebt;
}
}
}

0 comments on commit 99b8b00

Please sign in to comment.