> For the complete documentation index, see [llms.txt](https://bitprotocol.gitbook.io/bitprotocol/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bitprotocol.gitbook.io/bitprotocol/resources/technical-documentation/trovemanager.md).

# TroveManager

## **Write Methods**

```
function collectInterests() external
```

Mints accumulated interest to the fee receiver. Reverts if no interest is payable.

```
function fetchPrice() public returns (uint256)
```

Fetches the latest collateral price from the price feed contract.

**Return Values:**

| Value   | Type    | Description                                     |
| ------- | ------- | ----------------------------------------------- |
| `price` | uint256 | Current price of collateral from the price feed |

```
function getEntireSystemBalances() external returns (uint256, uint256, uint256)
```

Returns a snapshot of the system's total collateral, debt, and current price in one call.

**Return Values:**

| Value             | Type    | Description                                    |
| ----------------- | ------- | ---------------------------------------------- |
| `totalCollateral` | uint256 | Sum of active + defaulted collateral           |
| `totalDebt`       | uint256 | Sum of active + defaulted debt (with interest) |
| `price`           | uint256 | Current collateral price from oracle           |

```
function claimCollateral(uint256 _troveId, address _receiver, bool _useNative) external
```

Claims remaining collateral from a closed trove. Only callable by trove owner.

**Input Parameters:**

| Parameter    | Type    | Description                   |
| ------------ | ------- | ----------------------------- |
| `_troveId`   | uint256 | ID of the trove               |
| `_receiver`  | address | Address to receive collateral |
| `_useNative` | bool    | Whether to send native ETH    |

**Return Values:**

| Value | Type | Description                      |
| ----- | ---- | -------------------------------- |
| None  | -    | Function does not return a value |

```
function collectFrontEndInterest(address frontEndId) external {
```

Allows front-end operators to collect accrued interest.

**Input Parameters:**

| Parameter    | Type    | Description                |
| ------------ | ------- | -------------------------- |
| `frontEndId` | address | Front-end operator address |

**Return Values:**

| Value | Type | Description                      |
| ----- | ---- | -------------------------------- |
| None  | -    | Function does not return a value |

## **View Methods**

```
function loadPrice() public view returns (uint256)
```

Returns the most recent price without updating it (gas-efficient).

**Return Values:**

| Value   | Type    | Description                                |
| ------- | ------- | ------------------------------------------ |
| `price` | uint256 | Last recorded price of collateral (cached) |

```
function getWeekAndDay() public view returns (uint256, uint256)
```

Calculates the current week and day based on the system’s start time.

**Return Values:**

| Value  | Type    | Description                       |
| ------ | ------- | --------------------------------- |
| `week` | uint256 | Current week since system start   |
| `day`  | uint256 | Current day within the week (0–6) |

```
function getTotalMints(uint256 week) external view returns (uint32[7] memory)
```

Returns the volume of debt minted per day in a specific week.

**Input Parameters:**

| Parameter | Type    | Description          |
| --------- | ------- | -------------------- |
| `week`    | uint256 | Week number to query |

**Return Values:**

| Value   | Type       | Description                              |
| ------- | ---------- | ---------------------------------------- |
| `mints` | uint32\[7] | Array of daily mint amounts for the week |

```
function getTroveOwnersCount() external view returns (uint256)
```

Returns the length of the `TroveIds` array.

**Return Values:**

| Value   | Type    | Description                   |
| ------- | ------- | ----------------------------- |
| `count` | uint256 | Total number of active troves |

```
function getTroveFromTroveOwnersArray(uint256 arrayIndex) external view returns (uint256)
```

Returns a trove ID from the active troves array.

**Input Parameters:**

| Parameter    | Type    | Description                   |
| ------------ | ------- | ----------------------------- |
| `arrayIndex` | uint256 | Index in the `TroveIds` array |

**Return Values:**

| Value     | Type    | Description                        |
| --------- | ------- | ---------------------------------- |
| `troveId` | uint256 | ID of the trove at the given index |

```
function getAllTrovesByOwner(address account) external view returns (Trove[] memory)
```

Fetches all trove data (collateral, debt, status, etc.) for a given owner.

**Input Parameters:**

| Parameter | Type    | Description         |
| --------- | ------- | ------------------- |
| `account` | address | Owner of the troves |

**Return Values:**

| Value    | Type     | Description                            |
| -------- | -------- | -------------------------------------- |
| `troves` | Trove\[] | Array of all troves owned by `account` |

```
function getTrove(uint256 _troveId) public view returns (Trove memory)
```

Returns the complete data structure for a specific trove.

**Input Parameters:**

| Parameter  | Type    | Description     |
| ---------- | ------- | --------------- |
| `_troveId` | uint256 | ID of the trove |

**Return Values:**

| Value   | Type  | Description                                                                                                                                                                                                                                                                                                                               |
| ------- | ----- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `trove` | Trove | <p>Struct containing:<br>- <code>debt</code> (uint256)<br>- <code>coll</code> (uint256)<br>- <code>stake</code> (uint256)<br>- <code>status</code> (Status enum)<br>- <code>arrayIndex</code> (uint128)<br>- <code>activeInterestIndex</code> (uint256)<br>- <code>frontEndId</code> (address)<br>- <code>referrerId</code> (address)</p> |

```
function getTroveStatus(uint256 _troveId) external view returns (uint256)
```

Returns the status of a trove (e.g., `active`, `closedByLiquidation`).

**Input Parameters:**

| Parameter  | Type    | Description     |
| ---------- | ------- | --------------- |
| `_troveId` | uint256 | ID of the trove |

**Return Values:**

| Value    | Type    | Description                                |
| -------- | ------- | ------------------------------------------ |
| `status` | uint256 | Trove status (0–4, matching `Status` enum) |

```
function getTroveStake(uint256 _troveId) external view returns (uint256)
```

Returns the stake (collateral-weighted value) of a trove.

**Input Parameters:**

| Parameter  | Type    | Description     |
| ---------- | ------- | --------------- |
| `_troveId` | uint256 | ID of the trove |

**Return Values:**

| Value   | Type    | Description                |
| ------- | ------- | -------------------------- |
| `stake` | uint256 | Stake amount for the trove |

```
function getTroveCollAndDebt(uint256 _troveId) public view returns (uint256 coll, uint256 debt)
```

Returns trove collateral and debt (view).

**Input Parameters:**

| Parameter  | Type    | Description     |
| ---------- | ------- | --------------- |
| `_troveId` | uint256 | ID of the trove |

**Return Values:**

| Value  | Type    | Description                   |
| ------ | ------- | ----------------------------- |
| `coll` | uint256 | Total collateral in the trove |
| `debt` | uint256 | Total debt in the trove       |

```
function getEntireDebtAndColl(uint256 _troveId) public view
        returns (
            uint256 debt,
            uint256 coll,
            uint256 pendingDebtReward,
            uint256 pendingCollateralReward
        )
```

Returns the full state of a trove, including pending rewards.

**Input Parameters:**

| Parameter  | Type    | Description     |
| ---------- | ------- | --------------- |
| `_troveId` | uint256 | ID of the trove |

**Return Values:**

| Value                     | Type    | Description                                  |
| ------------------------- | ------- | -------------------------------------------- |
| `debt`                    | uint256 | Total debt (including pending rewards)       |
| `coll`                    | uint256 | Total collateral (including pending rewards) |
| `pendingDebtReward`       | uint256 | Pending debt redistribution reward           |
| `pendingCollateralReward` | uint256 | Pending collateral redistribution reward     |

```
function getEntireSystemColl() public view returns (uint256)
```

Returns the total collateral locked in the system.

**Return Values:**

| Value             | Type    | Description                          |
| ----------------- | ------- | ------------------------------------ |
| `totalCollateral` | uint256 | Sum of active + defaulted collateral |

```
function getEntireSystemDebt() public view returns (uint256)
```

Returns the total debt in the system, including pending interest.

**Return Values:**

| Value       | Type    | Description                                            |
| ----------- | ------- | ------------------------------------------------------ |
| `totalDebt` | uint256 | Sum of active + defaulted debt (with accrued interest) |

```
function getNominalICR(uint256 _troveId) public view returns (uint256)
```

Returns nominal collateral ratio (view).

**Input Parameters:**

| Parameter  | Type                               | Description                 |
| ---------- | ---------------------------------- | --------------------------- |
| `_troveId` | uint256                            | ID of the trove             |
| `_price`   | uint256 (only for `getCurrentICR`) | Current price of collateral |

**Return Values:**

| Value | Type    | Description                           |
| ----- | ------- | ------------------------------------- |
| `ICR` | uint256 | Collateral ratio (nominal or current) |

```
function getCurrentICR(uint256 _troveId, uint256 _price) public view returns (uint256)
```

**Input Parameters:**

| Parameter  | Type                               | Description                 |
| ---------- | ---------------------------------- | --------------------------- |
| `_troveId` | uint256                            | ID of the trove             |
| `_price`   | uint256 (only for `getCurrentICR`) | Current price of collateral |

**Return Values:**

| Value | Type    | Description                           |
| ----- | ------- | ------------------------------------- |
| `ICR` | uint256 | Collateral ratio (nominal or current) |

```
function getTotalActiveCollateral() public view returns (uint256)
```

Sum of collateral in all non-liquidated/non-redeemed troves.

**Return Values:**

| Value              | Type    | Description                                             |
| ------------------ | ------- | ------------------------------------------------------- |
| `activeCollateral` | uint256 | Total collateral in active troves (excluding defaulted) |

```
function getTotalActiveDebt() public view returns (uint256)
```

Calculates active debt with compounded interest since last update.

**Return Values:**

| Value        | Type    | Description                                              |
| ------------ | ------- | -------------------------------------------------------- |
| `activeDebt` | uint256 | Total debt in active troves (including accrued interest) |

```
function getPendingCollAndDebtRewards(
        uint256 _troveId
    ) public view returns (uint256, uint256)
```

Computes unclaimed rewards from liquidations affecting the trove.

**Input Parameters:**

| Parameter  | Type    | Description     |
| ---------- | ------- | --------------- |
| `_troveId` | uint256 | ID of the trove |

**Return Values:**

| Value               | Type    | Description                             |
| ------------------- | ------- | --------------------------------------- |
| `pendingCollateral` | uint256 | Pending collateral from redistributions |
| `pendingDebt`       | uint256 | Pending debt from redistributions       |

```
function getTroveNFT() view external returns (ITroveNFT)
```

Returns the address of the TroveNFT contract (used for ownership checks).

**Return Values:**

| Value      | Type      | Description                                      |
| ---------- | --------- | ------------------------------------------------ |
| `troveNFT` | ITroveNFT | <p></p><p>Interface of the TroveNFT contract</p> |

```
function hasPendingRewards(uint256 _troveId) public view returns (bool)
```

Checks if a trove has unclaimed redistribution rewards.

**Input Parameters:**

| Parameter  | Type    | Description     |
| ---------- | ------- | --------------- |
| `_troveId` | uint256 | ID of the trove |

**Return Values:**

| Value        | Type | Description                         |
| ------------ | ---- | ----------------------------------- |
| `hasRewards` | bool | `true` if trove has pending rewards |

<br>

```
function getBorrowingRate() public view returns (uint256)
```

Returns the borrowing fee rate (e.g., 0.5% as `5000000000000000`).

**Return Values:**

| Value  | Type    | Description                                  |
| ------ | ------- | -------------------------------------------- |
| `rate` | uint256 | Current borrowing rate (based on `baseRate`) |

```
function getBorrowingRateWithDecay() public view returns (uint256)
```

Estimates the borrowing rate if no new fees are charged.

**Return Values:**

| Value  | Type    | Description                            |
| ------ | ------- | -------------------------------------- |
| `rate` | uint256 | Borrowing rate after decay (simulated) |

```
function getBorrowingFee(uint256 _debt) external view returns (uint256)
```

Calculates the fee to borrow `_debt`.

**Input Parameters:**

| Parameter | Type    | Description                      |
| --------- | ------- | -------------------------------- |
| `_debt`   | uint256 | Debt amount to calculate fee for |

**Return Values:**

| Value | Type    | Description                               |
| ----- | ------- | ----------------------------------------- |
| `fee` | uint256 | Borrowing fee for `_debt` at current rate |

```
function getBorrowingFeeWithDecay(uint256 _debt) external view returns (uint256)
```

Estimates the borrowing fee after base rate decay.

**Input Parameters:**

| Parameter | Type    | Description                      |
| --------- | ------- | -------------------------------- |
| `_debt`   | uint256 | Debt amount to calculate fee for |

**Return Values:**

| Value | Type    | Description                     |
| ----- | ------- | ------------------------------- |
| `fee` | uint256 | Fee for `_debt` at decayed rate |

```
function calcDecayedBaseRate() external view returns (uint256) 
```

Simulates the decay of `baseRate` over time.

**Return Values:**

| Value         | Type    | Description           |
| ------------- | ------- | --------------------- |
| `decayedRate` | uint256 | Base rate after decay |

```
function claimableReward(address account) external view returns (uint256)
```

Aggregates pending rewards (debt + minting) for an account.

**Input Parameters:**

| Parameter  | Type    | Description                |
| ---------- | ------- | -------------------------- |
| `receiver` | address | Address to receive rewards |

**Return Values:**

| Value    | Type    | Description           |
| -------- | ------- | --------------------- |
| `amount` | uint256 | Total rewards claimed |

```
function getCompositeDebt(uint256 _debt) external view returns (uint256)
```

**Input Parameters:**

| Parameter | Type    | Description      |
| --------- | ------- | ---------------- |
| `_debt`   | uint256 | Base debt amount |

**Return Values:**

| Value           | Type    | Description             |
| --------------- | ------- | ----------------------- |
| `compositeDebt` | uint256 | Debt + gas compensation |
| `netDebt`       | uint256 | Debt - gas compensation |

```
function getNetDebt(uint256 _debt) external view returns (uint256) 
```

**Input Parameters:**

| Parameter | Type    | Description      |
| --------- | ------- | ---------------- |
| `_debt`   | uint256 | Base debt amount |

**Return Values:**

| Value           | Type    | Description             |
| --------------- | ------- | ----------------------- |
| `compositeDebt` | uint256 | Debt + gas compensation |
| `netDebt`       | uint256 | Debt - gas compensation |

```
function getCollGasCompensation(uint256 _entireColl) external view returns (uint256)
```

**Input Parameters:**

| Parameter     | Type    | Description               |
| ------------- | ------- | ------------------------- |
| `_entireColl` | uint256 | Total collateral in trove |

**Return Values:**

| Value          | Type    | Description                 |
| -------------- | ------- | --------------------------- |
| `compensation` | uint256 | Collateral reserved for gas |

## Structs and Enums

```
struct VolumeData {
        uint32 amount;
        uint32 week;
        uint32 day;
    }
```

```
struct EmissionId {
        uint16 debt;
        uint16 minting;
    }
```

```
struct Trove {
        uint256 debt;
        uint256 coll;
        uint256 stake;
        Status status;
        uint128 arrayIndex;
        uint256 activeInterestIndex;
        address frontEndId;
        address referrerId;
    }
```

```
struct RewardSnapshot {
        uint256 collateral;
        uint256 debt;
    }
```

```
enum TroveManagerOperation {
        open,
        close,
        adjust,
        liquidate,
        redeemCollateral
    }
```

```
enum Status {
        nonExistent,
        active,
        closedByOwner,
        closedByLiquidation,
        closedByRedemption
    }
```

## Events

Note: on the Oasis Sapphire blockchain all events are disabled for privacy preservation purposes

```
event TroveUpdated(
        address indexed _borrower,
        uint256 indexed _troveId,
        uint256 _debt,
        uint256 _coll,
        uint256 _stake,
        TroveManagerOperation _operation
    )
```

```
event BaseRateUpdated(uint256 _baseRate)
```

```
event LastFeeOpTimeUpdated(uint256 _lastFeeOpTime)
```

```
event TotalStakesUpdated(uint256 _newTotalStakes)
```

```
event SystemSnapshotsUpdated(
        uint256 _totalStakesSnapshot,
        uint256 _totalCollateralSnapshot
    )
```

```
event LTermsUpdated(uint256 _L_collateral, uint256 _L_debt)
```

```
event TroveSnapshotsUpdated(uint256 _L_collateral, uint256 _L_debt)
```

```
event TroveIndexUpdated(uint256 _troveId, uint256 _newIndex)
```

```
event CollateralSent(address _to, uint256 _amount)
```

```
event RewardClaimed(
        address indexed account,
        address indexed recipient,
        uint256 claimed
    )
```

```
event FrontEndInterestCollected(address indexed frontEndId, uint256 amount)
```
