# Credit Facility

<https://github.com/Paracosm-Labs/mintdeals-contracts/blob/main/contracts/CreditFacility.sol>

The CreditFacility contract is a crucial component of the MintDeals platform, managing interactions with JustLend for asset management, collateral handling, borrowing, repaying, and yield claiming for increasing capacity.

### Key Functions

#### Asset Management

### 1. Supply Asset

```solidity
function supplyAsset(address cTokenAddress, uint256 amount, address beneficiary) external
```

* Allows users to supply a specific amount of an underlying asset to JustLend.
* Users receive cTokens in return.
* The asset can be supplied for the sender or on behalf of another user (beneficiary).

### 2. Redeem Asset

```solidity
function redeemAsset(address cTokenAddress, uint256 amount) external
```

* Enables users to withdraw (redeem) a specific amount of the underlying asset from the protocol.
* Ensures that the redemption doesn't cause under-collateralization.

#### Borrowing and Repayment

### 3. Borrow

```solidity
function borrow(address cTokenAddress, uint256 amount) external
```

* Allows users to borrow a specific amount of a stablecoin from JustLend.
* Checks if the borrowed amount is within the user's borrowing power.

### 4. Repay Borrow

```solidity
function repayBorrow(address cTokenAddress, uint256 amount, address beneficiary) external
```

* Enables users to repay a specific amount of borrowed stablecoin.
* Can be used to repay on behalf of another user (beneficiary).

#### Collateral Management

### 5. Enable As Collateral

```solidity
function enableAsCollateral(address cTokensAddress) external
```

* Allows admins to enter markets as collateral, adding assets to the liquidity calculation.

#### Configuration

### 6. Add CToken

```solidity
function addCToken(address _cTokenAddress, address _underlyingAsset, bool _isStablecoin, address _priceOracle) external
```

* Enables admins to add new cToken contracts to the system.

### 7. Update Collateral Factors

```solidity
function updateCollateralFactors(uint newStablecoinFactor, uint newNonStablecoinFactor) external
```

* Allows admins to update the collateral factors for stablecoins and non-stablecoins.

#### Utility Functions

### 8. Calculate Total Borrowing Power

```solidity
function calculateTotalBorrowingPower(address user) external view
```

* Calculates the total borrowing power of a specific user based on their deposits.

### 9. Calculate Total User Stablecoin Borrows

```solidity
function calculateTotalUserStablecoinBorrows(address user) public view
```

* Calculates the total amount of stablecoins borrowed by a user.

### 10. Calculate Total User Deposits

```solidity
function calculateTotalUserDeposits(address user) public view
```

* Calculates the total amount of stablecoin deposits and non-stablecoin deposits for a user.

### 11. Get User Reserve Valuation

```solidity
function getUserReserveValuation(address cTokenAddress, address user) public view
```

* Gets the USD valuation of a user's non-stablecoin asset deposits.

### Key Concepts

1. **cTokens**: Representation of supplied assets in the JustLend protocol.
2. **Collateral Factors**: Determine the borrowing power of supplied assets (different for stablecoins and non-stablecoins).
3. **Borrowing Power**: The maximum amount a user can borrow based on their supplied collateral.

This contract forms the backbone of MintDeals' credit facility, enabling businesses to leverage their membership fees as collateral for borrowing, while also managing asset supply, borrowing and repaying in a decentralized manner.
