> 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/borroweroperations.md).

# BorrowerOperations

## **Write Methods**

```
function fetchBalances() public returns (SystemBalances memory balances)
```

Fetches collateral, debt, and price data for all active collaterals.

| Return Value | Type             | Description                                          |
| ------------ | ---------------- | ---------------------------------------------------- |
| `balances`   | `SystemBalances` | Struct containing collateral, debt, and price arrays |

```
function openTrove(
        ITroveManager troveManager,
        address account,
        uint256 _maxFeePercentage,
        uint256 _collateralAmount,
        uint256 _debtAmount,
        uint256 _upperHint,
        uint256 _lowerHint,
        address frontEndId,
        address referrerId,
        bool useNative
    ) external payable
```

Opens a new trove by depositing collateral and borrowing debt tokens.

| Parameter           | Type            | Description                                         |
| ------------------- | --------------- | --------------------------------------------------- |
| `troveManager`      | `ITroveManager` | TroveManager for the collateral                     |
| `account`           | `address`       | Owner of the new trove                              |
| `_maxFeePercentage` | `uint256`       | Max borrowing fee percentage (e.g., 1e18 = 100%)    |
| `_collateralAmount` | `uint256`       | Amount of collateral to deposit                     |
| `_debtAmount`       | `uint256`       | Amount of debt to borrow                            |
| `_upperHint`        | `uint256`       | Hint for trove insertion (gas optimization)         |
| `_lowerHint`        | `uint256`       | Hint for trove insertion (gas optimization)         |
| `frontEndId`        | `address`       | Address of the front-end operator (optional)        |
| `referrerId`        | `address`       | Address of the referrer (optional)                  |
| `useNative`         | `bool`          | Whether to use native ETH (if collateral is native) |

```
function addColl(
        ITroveManager troveManager,
        uint256 _troveId,
        address account,
        uint256 _collateralAmount,
        uint256 _upperHint,
        uint256 _lowerHint,
        bool useNative
    ) external payable
```

Adds collateral to an existing trove.

| Parameter           | Type            | Description                     |
| ------------------- | --------------- | ------------------------------- |
| `troveManager`      | `ITroveManager` | TroveManager for the collateral |
| `_troveId`          | `uint256`       | ID of the trove to modify       |
| `account`           | `address`       | Owner of the trove              |
| `_collateralAmount` | `uint256`       | Amount of collateral to add     |
| `_upperHint`        | `uint256`       | Hint for trove reinsertion      |
| `_lowerHint`        | `uint256`       | Hint for trove reinsertion      |
| `useNative`         | `bool`          | Whether to use native ETH       |

```
function withdrawColl(
        ITroveManager troveManager,
        address account,
        uint256 _collWithdrawal,
        uint256 _upperHint,
        uint256 _lowerHint,
        uint256 _troveId,
        bool _useNative
    ) external
```

Withdraws collateral from an existing trove.

| Parameter         | Type            | Description                      |
| ----------------- | --------------- | -------------------------------- |
| `troveManager`    | `ITroveManager` | TroveManager for the collateral  |
| `account`         | `address`       | Owner of the trove               |
| `_collWithdrawal` | `uint256`       | Amount of collateral to withdraw |
| `_upperHint`      | `uint256`       | Hint for trove reinsertion       |
| `_lowerHint`      | `uint256`       | Hint for trove reinsertion       |
| `_troveId`        | `uint256`       | ID of the trove to modify        |
| `_useNative`      | `bool`          | Whether to receive native ETH    |

<br>

```
function withdrawDebt(
        ITroveManager troveManager,
        address account,
        uint256 _maxFeePercentage,
        uint256 _debtAmount,
        uint256 _upperHint,
        uint256 _lowerHint,
        uint256 _troveId,
        bool _useNative
    ) external
```

Borrows more debt tokens from an existing trove.

| Parameter           | Type            | Description                     |
| ------------------- | --------------- | ------------------------------- |
| `troveManager`      | `ITroveManager` | TroveManager for the collateral |
| `account`           | `address`       | Owner of the trove              |
| `_maxFeePercentage` | `uint256`       | Max borrowing fee percentage    |
| `_debtAmount`       | `uint256`       | Amount of debt to borrow        |
| `_upperHint`        | `uint256`       | Hint for trove reinsertion      |
| `_lowerHint`        | `uint256`       | Hint for trove reinsertion      |
| `_troveId`          | `uint256`       | ID of the trove to modify       |
| `_useNative`        | `bool`          | Whether to use native ETH       |

```
function repayDebt(
        ITroveManager troveManager,
        address account,
        uint256 _debtAmount,
        uint256 _upperHint,
        uint256 _lowerHint,
        uint256 _troveId
    ) external
```

Repays debt tokens to an existing trove.

| Parameter      | Type            | Description                     |
| -------------- | --------------- | ------------------------------- |
| `troveManager` | `ITroveManager` | TroveManager for the collateral |
| `account`      | `address`       | Owner of the trove              |
| `_debtAmount`  | `uint256`       | Amount of debt to repay         |
| `_upperHint`   | `uint256`       | Hint for trove reinsertion      |
| `_lowerHint`   | `uint256`       | Hint for trove reinsertion      |
| `_troveId`     | `uint256`       | ID of the trove to modify       |

```
function adjustTrove(
        ITroveManager troveManager,
        address account,
        uint256 _maxFeePercentage,
        uint256 _collDeposit,
        uint256 _collWithdrawal,
        uint256 _debtChange,
        bool _isDebtIncrease,
        uint256 _upperHint,
        uint256 _lowerHint,
        uint256 _troveId,
        bool _useNative
    ) external payable
```

Adjusts a trove's collateral and/or debt in a single transaction.

| Parameter           | Type            | Description                               |
| ------------------- | --------------- | ----------------------------------------- |
| `troveManager`      | `ITroveManager` | TroveManager for the collateral           |
| `account`           | `address`       | Owner of the trove                        |
| `_maxFeePercentage` | `uint256`       | Max borrowing fee percentage              |
| `_collDeposit`      | `uint256`       | Amount of collateral to add (if any)      |
| `_collWithdrawal`   | `uint256`       | Amount of collateral to withdraw (if any) |
| `_debtChange`       | `uint256`       | Amount of debt to add/repay               |
| `_isDebtIncrease`   | `bool`          | `true` to borrow, `false` to repay        |
| `_upperHint`        | `uint256`       | Hint for trove reinsertion                |
| `_lowerHint`        | `uint256`       | Hint for trove reinsertion                |
| `_troveId`          | `uint256`       | ID of the trove to modify                 |
| `_useNative`        | `bool`          | Whether to use native ETH                 |

<br>

```
function closeTrove(
        ITroveManager troveManager,
        address account,
        uint256 _troveId,
        bool _useNative
    ) external
```

Closes an existing trove by repaying all debt and withdrawing all collateral.

| Parameter      | Type            | Description                     |
| -------------- | --------------- | ------------------------------- |
| `troveManager` | `ITroveManager` | TroveManager for the collateral |
| `account`      | `address`       | Owner of the trove              |
| `_troveId`     | `uint256`       | ID of the trove to close        |
| `_useNative`   | `bool`          | Whether to receive native ETH   |

<br>

```
function getGlobalSystemBalances()
        external
        returns (uint256 totalPricedCollateral, uint256 totalDebt)
```

Returns the total priced collateral and total debt across all collaterals.

| Return Value            | Type      | Description                                            |
| ----------------------- | --------- | ------------------------------------------------------ |
| `totalPricedCollateral` | `uint256` | Total value of all collaterals (priced in debt tokens) |
| `totalDebt`             | `uint256` | Total system debt                                      |

## **View Methods**

```
function getTCR() external returns (uint256 globalTotalCollateralRatio)
```

Returns the global Total Collateral Ratio (TCR).

| Return Value                 | Type      | Description                             |
| ---------------------------- | --------- | --------------------------------------- |
| `globalTotalCollateralRatio` | `uint256` | The global Total Collateral Ratio (TCR) |

```
function checkRecoveryMode(uint256 TCR) public view returns (bool)
```

Checks if the system is in Recovery Mode (TCR < CCR).

| Parameter | Type      | Description                     |
| --------- | --------- | ------------------------------- |
| `TCR`     | `uint256` | Total Collateral Ratio to check |

| Return Value | Type   | Description                                       |
| ------------ | ------ | ------------------------------------------------- |
| `bool`       | `bool` | `true` if TCR < CCR (Recovery Mode), else `false` |

## Structs and Enums

```
struct TroveManagerData {
        IERC20 collateralToken;
        uint16 index;
        bool isNative;
    }
```

```
struct SystemBalances {
        uint256[] collaterals;
        uint256[] debts;
        uint256[] prices;
    }
```

```
struct LocalVariables_adjustTrove {
        uint256 price;
        uint256 totalPricedCollateral;
        uint256 totalDebt;
        uint256 collChange;
        uint256 netDebtChange;
        bool isCollIncrease;
        uint256 debt;
        uint256 coll;
        uint256 newDebt;
        uint256 newColl;
        uint256 stake;
        uint256 debtChange;
        address account;
        uint256 MCR;
    }
```

```
struct LocalVariables_adjustTrove_params {
        ITroveManager troveManager;
        address account;
        uint256 _maxFeePercentage;
        uint256 _collDeposit;
        uint256 _collWithdrawal;
        uint256 _debtChange;
        bool _isDebtIncrease;
        uint256 _upperHint;
        uint256 _lowerHint;
        uint256 _troveId;
        bool _useNative;
    }
```

```
struct LocalVariables_openTrove {
        uint256 price;
        uint256 totalPricedCollateral;
        uint256 totalDebt;
        uint256 netDebt;
        uint256 compositeDebt;
        uint256 ICR;
        uint256 NICR;
        uint256 stake;
        uint256 arrayIndex;
    }
```

```
enum BorrowerOperation {
        openTrove,
        closeTrove,
        adjustTrove
    }
```

## Events

```
event BorrowingFeePaid(
        address indexed borrower,
        IERC20 collateralToken,
        uint256 amount
    );
```

```
event CollateralConfigured(
        ITroveManager troveManager,
        IERC20 collateralToken
    );
```

```
event TroveManagerRemoved(ITroveManager troveManager);
```
