# TermRepoCollateralManager.sol

## TermRepoCollateralManager

This contract enforces margin maintenance rules for adding/withdrawing, repurchasing and liquidating collateral

*This contract belongs to the Term Servicer group of contracts and is specific to a Term Repo deployment*

### ADMIN\_ROLE

```solidity
bytes32 ADMIN_ROLE
```

### AUCTION\_LOCKER

```solidity
bytes32 AUCTION_LOCKER
```

### DEVOPS\_ROLE

```solidity
bytes32 DEVOPS_ROLE
```

### INITIALIZER\_ROLE

```solidity
bytes32 INITIALIZER_ROLE
```

### SERVICER\_ROLE

```solidity
bytes32 SERVICER_ROLE
```

### ROLLOVER\_MANAGER

```solidity
bytes32 ROLLOVER_MANAGER
```

### ROLLOVER\_TARGET\_AUCTIONEER\_ROLE

```solidity
bytes32 ROLLOVER_TARGET_AUCTIONEER_ROLE
```

### termRepoId

```solidity
bytes32 termRepoId
```

### liquidationsPaused

```solidity
bool liquidationsPaused
```

### termContractPaired

```solidity
bool termContractPaired
```

### numOfAcceptedCollateralTokens

```solidity
uint8 numOfAcceptedCollateralTokens
```

### deMinimisMarginThreshold

```solidity
uint256 deMinimisMarginThreshold
```

### liquidateDamangesDueToProtocol

```solidity
uint256 liquidateDamangesDueToProtocol
```

### netExposureCapOnLiquidation

```solidity
uint256 netExposureCapOnLiquidation
```

### termRepoServicer

```solidity
contract ITermRepoServicer termRepoServicer
```

### purchaseToken

```solidity
address purchaseToken
```

### termPriceOracle

```solidity
contract ITermPriceOracle termPriceOracle
```

### termRepoLocker

```solidity
contract ITermRepoLocker termRepoLocker
```

### termController

```solidity
contract ITermController termController
```

### emitter

```solidity
contract ITermEventEmitter emitter
```

### collateralTokens

```solidity
address[] collateralTokens
```

### encumberedCollateralBalances

```solidity
mapping(address => uint256) encumberedCollateralBalances
```

### maintenanceCollateralRatios

```solidity
mapping(address => uint256) maintenanceCollateralRatios
```

### initialCollateralRatios

```solidity
mapping(address => uint256) initialCollateralRatios
```

### liquidatedDamages

```solidity
mapping(address => uint256) liquidatedDamages
```

### lockedCollateralLedger

```solidity
mapping(address => mapping(address => uint256)) lockedCollateralLedger
```

### isCollateralTokenAccepted

```solidity
modifier isCollateralTokenAccepted(address token)
```

### whileLiquidationsNotPaused

```solidity
modifier whileLiquidationsNotPaused()
```

### notTermContractPaired

```solidity
modifier notTermContractPaired()
```

### constructor

```solidity
constructor() public
```

### initialize

```solidity
function initialize(string termRepoId_, uint256 liquidateDamangesDueToProtocol_, uint256 netExposureCapOnLiquidation_, uint256 deMinimisMarginThreshold_, address purchaseToken_, struct Collateral[] collateralTokens_, contract ITermEventEmitter emitter_, address termInitializer_) external
```

### pairTermContracts

```solidity
function pairTermContracts(address termRepoLocker_, address termRepoServicer_, address termAuctionBidLocker_, address termAuction_, address termController_, address termPriceOracle_, address termRepoRolloverManager_, address devopsMultisig_, address adminWallet_) external
```

### externalLockCollateral

```solidity
function externalLockCollateral(address collateralToken, uint256 amount) external
```

#### Parameters

| Name            | Type    | Description                                 |
| --------------- | ------- | ------------------------------------------- |
| collateralToken | address | The address of the collateral token to lock |
| amount          | uint256 | The amount of collateral token to lock      |

### externalUnlockCollateral

```solidity
function externalUnlockCollateral(address collateralToken, uint256 amount) external
```

#### Parameters

| Name            | Type    | Description                                   |
| --------------- | ------- | --------------------------------------------- |
| collateralToken | address | The address of the collateral token to unlock |
| amount          | uint256 | The amount of collateral token to unlock      |

### batchLiquidation

```solidity
function batchLiquidation(address borrower, uint256[] closureAmounts) external
```

#### Parameters

| Name           | Type       | Description                                                                                                                                                            |
| -------------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| borrower       | address    | The address of the borrower                                                                                                                                            |
| closureAmounts | uint256\[] | An array specifying the amounts of Term Repo exposure the liquidator proposes to cover in liquidation; an amount is required to be specified for each collateral token |

### batchLiquidationWithRepoToken

```solidity
function batchLiquidationWithRepoToken(address borrower, uint256[] closureRepoTokenAmounts) external
```

#### Parameters

| Name                    | Type       | Description                                                                                                                                                                                 |
| ----------------------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| borrower                | address    | The address of the borrower                                                                                                                                                                 |
| closureRepoTokenAmounts | uint256\[] | An array specifying the amounts of Term Repo Tokens the liquidator proposes to cover borrower repo exposure in liquidation; an amount is required to be specified for each collateral token |

### batchDefault

```solidity
function batchDefault(address borrower, uint256[] closureAmounts) external
```

#### Parameters

| Name           | Type       | Description                                                                                                                                                            |
| -------------- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| borrower       | address    | The address of the borrower                                                                                                                                            |
| closureAmounts | uint256\[] | An array specifying the amounts of Term Repo exposure the liquidator proposes to cover in liquidation; an amount is required to be specified for each collateral token |

### calculateMintableExposure

```solidity
function calculateMintableExposure(address collateralToken, uint256 amountToLock) external view returns (struct ExponentialNoError.Exp)
```

#### Parameters

| Name            | Type    | Description                                   |
| --------------- | ------- | --------------------------------------------- |
| collateralToken | address | The collateral token address of tokens locked |
| amountToLock    | uint256 | The amount of collateral tokens to lock       |

### getCollateralBalances

```solidity
function getCollateralBalances(address borrower) external view returns (address[], uint256[])
```

#### Parameters

| Name     | Type    | Description                 |
| -------- | ------- | --------------------------- |
| borrower | address | The address of the borrower |

#### Return Values

| Name | Type       | Description                                                     |
| ---- | ---------- | --------------------------------------------------------------- |
| \[0] | address\[] | An array of collateral token addresses                          |
| \[1] | uint256\[] | An array collateral token balances locked on behalf of borrower |

### getCollateralBalance

```solidity
function getCollateralBalance(address borrower, address collateralToken) external view returns (uint256)
```

#### Parameters

| Name            | Type    | Description                           |
| --------------- | ------- | ------------------------------------- |
| borrower        | address | The address of the borrower           |
| collateralToken | address | The collateral token address to query |

#### Return Values

| Name | Type    | Description                                                        |
| ---- | ------- | ------------------------------------------------------------------ |
| \[0] | uint256 | uint256 The amount of collateralToken locked on behalf of borrower |

### encumberedCollateralRemaining

```solidity
function encumberedCollateralRemaining() external view returns (bool)
```

#### Return Values

| Name | Type | Description                                                                |
| ---- | ---- | -------------------------------------------------------------------------- |
| \[0] | bool | bool A boolean that tests whether any encumbered collateral remains locked |

### auctionLockCollateral

```solidity
function auctionLockCollateral(address bidder, address collateralToken, uint256 amount) external
```

#### Parameters

| Name            | Type    | Description                                       |
| --------------- | ------- | ------------------------------------------------- |
| bidder          | address | The bidder's address                              |
| collateralToken | address | The address of the token to be used as collateral |
| amount          | uint256 | The amount of the token to lock                   |

### auctionUnlockCollateral

```solidity
function auctionUnlockCollateral(address bidder, address collateralToken, uint256 amount) external
```

#### Parameters

| Name            | Type    | Description                                 |
| --------------- | ------- | ------------------------------------------- |
| bidder          | address | The bidder's address                        |
| collateralToken | address | The address of the token used as collateral |
| amount          | uint256 | The amount of collateral tokens to unlock   |

### acceptRolloverCollateral

```solidity
function acceptRolloverCollateral(address borrower, address collateralToken, uint256 amount) external
```

#### Parameters

| Name            | Type    | Description                             |
| --------------- | ------- | --------------------------------------- |
| borrower        | address | The address of the borrower             |
| collateralToken | address | The address of a collateral token       |
| amount          | uint256 | The amount of collateral tokens to lock |

### transferRolloverCollateral

```solidity
function transferRolloverCollateral(address borrower, uint256 rolloverProportion, address rolloverTermRepoLocker) external returns (address[], uint256[])
```

#### Parameters

| Name                   | Type    | Description                                                                                       |
| ---------------------- | ------- | ------------------------------------------------------------------------------------------------- |
| borrower               | address | The borrower's address                                                                            |
| rolloverProportion     | uint256 | The proportion of the collateral to be unlocked, equal to the proportion of the collateral repaid |
| rolloverTermRepoLocker | address | The address of the new TermRepoLocker contract to roll into                                       |

#### Return Values

| Name | Type       | Description                                                                                                    |
| ---- | ---------- | -------------------------------------------------------------------------------------------------------------- |
| \[0] | address\[] | An array representing a list of accepted collateral token addresses                                            |
| \[1] | uint256\[] | An array containing the amount of collateral tokens to pairoff and transfer to new TermRepoLocker to roll into |

### approveRolloverAuction

```solidity
function approveRolloverAuction(address rolloverAuction) external
```

#### Parameters

| Name            | Type    | Description                         |
| --------------- | ------- | ----------------------------------- |
| rolloverAuction | address | The address of the rollover auction |

### unlockCollateralOnRepurchase

```solidity
function unlockCollateralOnRepurchase(address borrower) external
```

#### Parameters

| Name     | Type    | Description                 |
| -------- | ------- | --------------------------- |
| borrower | address | The address of the borrower |

### journalBidCollateralToCollateralManager

```solidity
function journalBidCollateralToCollateralManager(address borrower, address[] collateralTokenAddresses, uint256[] collateralTokenAmounts) external
```

#### Parameters

| Name                     | Type       | Description                 |
| ------------------------ | ---------- | --------------------------- |
| borrower                 | address    | The address of the borrower |
| collateralTokenAddresses | address\[] | Collateral token addresses  |
| collateralTokenAmounts   | uint256\[] | Collateral token amounts    |

### mintOpenExposureLockCollateral

```solidity
function mintOpenExposureLockCollateral(address borrower, address collateralToken, uint256 amount) external
```

#### Parameters

| Name            | Type    | Description                 |
| --------------- | ------- | --------------------------- |
| borrower        | address | The address of the borrower |
| collateralToken | address | Collateral token addresse   |
| amount          | uint256 | Collateral token amount     |

### reopenToNewAuction

```solidity
function reopenToNewAuction(struct TermAuctionGroup termAuctionGroup) external
```

#### Parameters

| Name             | Type                    | Description                   |
| ---------------- | ----------------------- | ----------------------------- |
| termAuctionGroup | struct TermAuctionGroup | A struct of auction contracts |

### pauseLiquidations

```solidity
function pauseLiquidations() external
```

### unpauseLiquidations

```solidity
function unpauseLiquidations() external
```

### isBorrowerInShortfall

```solidity
function isBorrowerInShortfall(address borrower) public view returns (bool)
```

#### Parameters

| Name     | Type    | Description                 |
| -------- | ------- | --------------------------- |
| borrower | address | The address of the borrower |

#### Return Values

| Name | Type | Description                                                                  |
| ---- | ---- | ---------------------------------------------------------------------------- |
| \[0] | bool | Boolean testing whether the given borrower is in shortfall or margin deficit |

### getCollateralMarketValue

```solidity
function getCollateralMarketValue(address borrower) public view returns (uint256)
```

#### Parameters

| Name     | Type    | Description                 |
| -------- | ------- | --------------------------- |
| borrower | address | The address of the borrower |

#### Return Values

| Name | Type    | Description                                                         |
| ---- | ------- | ------------------------------------------------------------------- |
| \[0] | uint256 | The market value of borrower's locked collateral denominated in USD |

### \_lockCollateral

```solidity
function _lockCollateral(address borrower, address collateralToken, uint256 amount) internal
```

### \_unlockCollateral

```solidity
function _unlockCollateral(address borrower, address collateralToken, uint256 amount, bool decrementEncumberedCollateral) internal
```

### \_partialUnlockCollateral

```solidity
function _partialUnlockCollateral(address borrower, uint256 unlockProportion, address destinationwallet) internal returns (uint256[])
```

### \_validateBatchLiquidationForFullLiquidation

```solidity
function _validateBatchLiquidationForFullLiquidation(address borrower, address liquidator, uint256[] closureTokenAmounts) internal returns (bool)
```

*A helper function to validate various conditions required to liquidate*

#### Return Values

| Name | Type | Description                                                           |
| ---- | ---- | --------------------------------------------------------------------- |
| \[0] | bool | A boolean for whether borrower position eligible for full liquidation |

### \_unencumberRemainingBorrowerCollateralOnZeroObligation

```solidity
function _unencumberRemainingBorrowerCollateralOnZeroObligation(address borrower) internal
```

### \_withinNetExposureCapOnLiquidation

```solidity
function _withinNetExposureCapOnLiquidation(address borrower) internal view returns (bool)
```

### \_collateralSeizureAmounts

```solidity
function _collateralSeizureAmounts(uint256 amountToCover_, address collateralToken) internal view returns (uint256, uint256)
```

*returns total amount of collateral seized in liquidation and the amount of that total going protocol*

### \_transferLiquidationCollateral

```solidity
function _transferLiquidationCollateral(address borrower, address liquidator, address collateralAddress, uint256 closureAmount, uint256 collateralSeizureAmount, uint256 collateralSeizureProtocolShare, bool isDefault) internal
```

*A helper function to transfer tokens and update relevant state variables and mappings*

### \_isAcceptedCollateralToken

```solidity
function _isAcceptedCollateralToken(address token_) internal view returns (bool)
```

### \_usdValueOfBalances

```solidity
function _usdValueOfBalances(mapping(address => uint256) _tokenBalances) internal view returns (uint256)
```

### \_authorizeUpgrade

```solidity
function _authorizeUpgrade(address impl) internal
```

*required override by the OpenZeppelin UUPS module*

#### Parameters

| Name | Type    | Description                        |
| ---- | ------- | ---------------------------------- |
| impl | address | new impl address for proxy upgrade |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.term.finance/0.5.32/term-repo-class/term-servicer-group/termrepocollateralmanager.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
