# Deployer

## Introduction

MDCreate2 is a factory contract that enables deterministic deployment of distributor instances using `CREATE2`. This ensures predictable contract addresses and provides a unified deployment interface for both Merkle and ECDSA distributors.

## 🚀 Quick Start

### What is MDCreate2?

MDCreate2 is a factory contract that:

* Deploys distributor contracts deterministically using `CREATE2`
* Manages implementation addresses for different distributor types
* Configures fee collectors and fee tokens
* Provides address prediction before deployment

### Key Benefits

* ✅ **Deterministic Addresses** - Predict contract addresses before deployment
* ✅ **Multiple Implementations** - Support various distributor types
* ✅ **Fee Management** - Centralized fee configuration
* ✅ **Gas Efficient** - Minimal proxy pattern for deployments

## 📋 Factory Overview

### Architecture

```
┌────────────────────────────────────────────────────────┐
│                  MDCreate2 Factory                     │
│                                                        │
│  ┌─────────────────┐    ┌──────────────────────────┐   │
│  │ Implementation  │    │    Deployment Mapping    │   │
│  │    Registry     │    │   projectId => address   │   │
│  └─────────────────┘    └──────────────────────────┘   │
│                                                        │
│  ┌─────────────────┐    ┌──────────────────────────┐   │
│  │  Fee Collector  │    │        Fee Tokens        │   │
│  │    Settings     │    │      Configuration       │   │
│  └─────────────────┘    └──────────────────────────┘   │
└────────────────────────────────────────────────────────┘
                         │
                         │ Deploys
                         ▼
      ┌──────────────────────────────────────┐
      │         Distributor Instances        │
      │     (Clones with Minimal Proxies)    │
      └──────────────────────────────────────┘
```

### Key Components

* **Implementation Registry** - Maps distributor types to implementation addresses
* **Deployment Tracking** - Maps project IDs to deployed addresses
* **Fee Configuration** - Manages fee collectors and tokens
* **Clone Factory** - Uses OpenZeppelin's Clones library

## 🔧 Contract Details

### Storage

```solidity
address public defaultFeeCollector;
mapping(string projectId => address deployment) public deployments;
mapping(uint8 mdType => address implementation) public implementations;
mapping(address deployment => address feeCollectors) internal _feeCollectors;
```

### Key Functions

#### Deployment Functions

| Function           | Description                                   |
| ------------------ | --------------------------------------------- |
| `deploy()`         | Deploys a new distributor instance            |
| `simulateDeploy()` | Predicts deployment address without deploying |

```solidity
function deploy(
    uint8 mdType,
    string calldata projectId
) external returns (address instance)
```

#### Configuration Functions

| Function                   | Description                                   |
| -------------------------- | --------------------------------------------- |
| `setImplementation()`      | Registers implementation for distributor type |
| `setDefaultFeeParams()`    | Sets default fee collector                    |
| `setDeploymentFeeParams()` | Sets fee collector for specific deployment    |

#### View Functions

| Function          | Description                          |
| ----------------- | ------------------------------------ |
| `feeCollectors()` | Returns fee collector for deployment |
| `feeTokens()`     | Returns fee token for deployment     |
| `version()`       | Returns factory version              |

## 🔐 Security Considerations

### Access Control

| Role   | Permissions                                  |
| ------ | -------------------------------------------- |
| Owner  | Register implementations, set fee parameters |
| Anyone | Deploy distributors, simulate deployments    |

### Deployment Safety

* **Unique Project IDs** - Each project ID can only be used once
* **Implementation Validation** - Only registered implementations can be deployed
* **Initialization** - Deployed contracts must be initialized by deployer

## 🛠️ CREATE2 Mechanics

### Address Calculation

The deployment address is deterministically calculated using:

```solidity
address = CREATE2(
    0,                              // value
    deployerAddress,                // sender (factory)
    keccak256(projectId),           // salt
    keccak256(implementationCode)   // creation code
)
```

### Benefits

1. **Predictable Addresses** - Know contract address before deployment
2. **Cross-chain Consistency** - Same address on different chains
3. **Counterfactual Instantiation** - Reference contracts before deployment

## 📊 Fee System Integration

### Fee Flow

```
┌─────────────────┐
│   Distributor   │
└────────┬────────┘
         │ Queries fees
         ▼
┌─────────────────┐
│    MDCreate2    │
└────────┬────────┘
         │ Returns collector
         ▼
┌─────────────────┐
│  Fee Collector  │
└─────────────────┘
```

### Fee Configuration

1. **Default Collector** - Applied to all new deployments
2. **Custom Collector** - Override for specific deployments
3. **Fee Tokens** - Retrieved from fee collector contract

## 🔄 Versioning

The factory maintains its own version independent of distributor implementations.

## 📡 Events

| Event       | Description                          |
| ----------- | ------------------------------------ |
| `DidDeploy` | Emitted when distributor is deployed |

```solidity
event DidDeploy(
    uint8 mdType,
    string projectId,
    address instance
);
```

## 🔍 Error Handling

| Error                  | Condition               | Seelctor     |
| ---------------------- | ----------------------- | ------------ |
| `UnsupportedOperation` | Project ID already used | `0x9ba6061b` |

## 📚 Implementation Types

| Type | Description                           |
| ---- | ------------------------------------- |
| 0    | TokenTableMerkleDistributor           |
| 1    | TokenTableNativeMerkleDistributor     |
| 2    | SimpleERC721MerkleDistributor         |
| 3    | NftGatedMerkleDistributor             |
| 4    | CustomFeesNativeMerkleDistributor     |
| 5    | SimpleNoMintERC721MerkleDistributor   |
| 6    | FungibleTokenECDSADistributor         |
| 7    | FungibleTokenWithFeesECDSADistributor |
