> ## Documentation Index
> Fetch the complete documentation index at: https://docs.monolith.market/llms.txt
> Use this file to discover all available pages before exploring further.

# Factory

> Contract factory for deploying Monolith protocol components

The Factory contract is responsible for deploying all components of a Monolith instance and managing the global fee.

[Contract implementation](https://github.com/MonolithMarket/Monolith/blob/main/src/Factory.sol)

## Constructor

```solidity theme={null}
constructor(address _operator, uint256 _minDebtFloor)
```

Deploys the Factory and a shared `InterestModel` (accessible via `interestModel()`). Sets the initial operator and the global `minDebtFloor` that applies to every instance's `minDebt` parameter.

## User-Facing Functions

### deploy

```solidity theme={null}
function deploy(DeployParams memory params) external returns (address lender, address coin, address vault)
```

Deploys a complete Monolith lending system consisting of a lender, stablecoin, and vault contracts.

**Parameters:**

* `params`: DeployParams struct containing deployment configuration

**DeployParams Structure:**

```solidity theme={null}
struct DeployParams {
    string  name;                        // Token name (e.g., "USD Coin")
    string  symbol;                      // Token symbol (e.g., "USDC")
    address collateral;                  // Collateral token address
    address psmAsset;                    // PSM asset token address (address(0) disables PSM)
    address psmVault;                    // PSM ERC4626 vault (address(0) if unused)
    address feed;                        // Chainlink-style price feed address
    uint256 collateralFactor;            // Max LTV numerator in bps (≤8500 = 85%)
    uint256 minDebt;                     // Minimum debt per position (≥ factory.minDebtFloor())
    uint256 timeUntilImmutability;       // Seconds until operator params lock (max 1460 days)
    address operator;                    // Instance operator (address(0) for none)
    address manager;                     // Instance manager (address(0) for none)
    uint64  halfLife;                    // Interest rate half-life (12 hours to 30 days)
    uint16  targetFreeDebtRatioStartBps; // Target free-debt ratio lower bound (≥500, ≤ end)
    uint16  targetFreeDebtRatioEndBps;   // Target free-debt ratio upper bound (≤9500)
    uint16  redeemFeeBps;                // Redemption fee (≤500 = 5%)
    uint32  stalenessThreshold;          // Oracle max staleness in seconds
    uint16  maxBorrowDeltaBps;           // Share-accounting rounding tolerance (50–200)
    uint128 psmVaultMinTotalSupply;      // PSM vault minimum supply (required if psmVault set)
}
```

**Returns:**

* `lender`: Address of the deployed Lender contract
* `coin`:   Address of the deployed Coin (ERC‑20) contract
* `vault`:  Address of the deployed Vault (ERC‑4626) contract

**Requirements:**

* All bounds listed above are enforced by `Lender`'s constructor and revert on violation.
* `params.collateral` decimals must be ≤ 30.
* `params.psmAsset` must differ from the deployed `coin`.
* If `params.psmVault` is set, its underlying `asset()` must equal `params.psmAsset` and `psmVaultMinTotalSupply` must be > 0.

**Deployment Process:**

1. Calculates deterministic addresses using CREATE3
2. Deploys contracts in order: Lender → Coin → Vault
3. Initializes contracts with proper cross-references
4. Records deployment in factory registry

**Events Emitted:**

* `Deployed(lender, coin, vault)`

***

### deploymentsLength

```solidity theme={null}
function deploymentsLength() external view returns (uint256)
```

Returns the total number of deployments created by this factory.

**Returns:**

* `uint256`: Number of deployments

***

### deployments

```solidity theme={null}
function deployments(uint256 index) external view returns (address lender)
```

Array accessor returning the Lender address at a given deployment index. Use together with `deploymentsLength()` to enumerate all instances.

***

### interestModel

```solidity theme={null}
function interestModel() external view returns (address)
```

Returns the address of the shared `InterestModel` deployed by this Factory's constructor. All Lenders deployed by this Factory use this same `InterestModel` instance.

***

### minDebtFloor

```solidity theme={null}
function minDebtFloor() external view returns (uint256)
```

Returns the global immutable minimum debt floor. Every Lender's `minDebt` must be ≥ this value at deployment.

***

### pendingOperator

```solidity theme={null}
function pendingOperator() external view returns (address)
```

Returns the address currently queued to accept the operator role via `acceptOperator()`.

***

### operator

```solidity theme={null}
function operator() external view returns (address)
```

Returns the current operator address authorized to manage the factory.

**Returns:**

* `address`: Current operator address

***

### feeRecipient

```solidity theme={null}
function feeRecipient() external view returns (address)
```

Returns the address that receives protocol fees.

**Returns:**

* `address`: Fee recipient address

***

### feeBps

```solidity theme={null}
function feeBps() external view returns (uint256)
```

Returns the default fee rate in basis points (bps).

**Returns:**

* `uint256`: Default fee in bps (e.g., 100 = 1%)

***

### getFeeOf

```solidity theme={null}
function getFeeOf(address _lender) external view returns (uint256)
```

Returns the fee rate for a specific lender deployment, using custom fee if set, otherwise default fee.

**Parameters:**

* `_lender`: Address of the lender deployment

**Returns:**

* `uint256`: Fee rate in bps

***

### isDeployed

```solidity theme={null}
function isDeployed(address deployment) external view returns (bool)
```

Checks if an address was deployed by this factory.

**Parameters:**

* `deployment`: Address to check

**Returns:**

* `bool`: True if address was deployed by this factory

***

### customFeeBps

```solidity theme={null}
function customFeeBps(address lender) external view returns (uint256)
```

Returns the custom fee rate for a specific lender deployment.

**Parameters:**

* `lender`: Address of the lender deployment

**Returns:**

* `uint256`: Custom fee in bps, 0 if using default fee

## Operator Functions

### setPendingOperator

```solidity theme={null}
function setPendingOperator(address _pendingOperator) external
```

Sets a new pending operator address. Can only be called by current operator.

**Parameters:**

* `_pendingOperator`: Address to set as pending operator

**Requirements:**

* `msg.sender` must be current operator

***

### acceptOperator

```solidity theme={null}
function acceptOperator() external
```

Accepts operator role for the pending operator address.

**Requirements:**

* `msg.sender` must be pending operator

***

### setFeeRecipient

```solidity theme={null}
function setFeeRecipient(address _feeRecipient) external
```

Sets the address that receives protocol fees.

**Parameters:**

* `_feeRecipient`: New fee recipient address

**Requirements:**

* `msg.sender` must be current operator

***

### setFeeBps

```solidity theme={null}
function setFeeBps(uint256 _feeBps) external
```

Sets the default fee rate in basis points.

**Parameters:**

* `_feeBps`: New fee rate in bps (max 1000 = 10%)

**Requirements:**

* `msg.sender` must be current operator
* `_feeBps` must be ≤ 1000

***

### setCustomFeeBps

```solidity theme={null}
function setCustomFeeBps(address _address, uint256 _feeBps) external
```

Sets a custom fee rate for a specific lender deployment.

**Parameters:**

* `_address`: Lender deployment address
* `_feeBps`: Custom fee rate in bps (max 1000 = 10%)

**Requirements:**

* `msg.sender` must be current operator
* `_feeBps` must be ≤ 1000

***

### pullReserves

```solidity theme={null}
function pullReserves(address _deployment) external
```

Pulls accumulated reserves from a lender deployment to the fee recipient.

**Parameters:**

* `_deployment`: Lender deployment address

**Requirements:**

* `msg.sender` must be fee recipient
* `_deployment` must be a valid deployment

## Events

* `Deployed(address indexed lender, address indexed coin, address indexed vault)` — emitted by `deploy()` with the new instance addresses.
* `CustomFeeBpsSet(address indexed lender, uint256 feeBps)` — emitted by `setCustomFeeBps()`.
* `FeeBpsUpdated(uint256 feeBps)` — emitted by `setFeeBps()`.
* `FeeRecipientUpdated(address indexed feeRecipient)` — emitted by `setFeeRecipient()`.
* `PendingOperatorUpdated(address indexed pendingOperator)` — emitted by `setPendingOperator()`.
* `OperatorUpdated(address indexed operator)` — emitted by `acceptOperator()`.
* `ReservesPulled(address indexed lender, address indexed recipient, uint256 amount)` — emitted by `pullReserves()`.
