# Overview

ATFi Protocol consists of two main smart contracts deployed on Base.

## Architecture

```
┌─────────────────────────────────────────────────────────────┐
│                    FactoryATFi.sol                          │
│  - Creates new VaultATFi instances                         │
│  - Tracks all vaults                                        │
│  - Manages treasury and Morpho vault addresses             │
└─────────────────────────────────────────────────────────────┘
                            │
                            │ creates
                            ▼
┌─────────────────────────────────────────────────────────────┐
│                     VaultATFi.sol                           │
│  - Manages individual event commitment                     │
│  - Handles stake/verify/settle/claim lifecycle             │
│  - Integrates with Morpho for yield generation            │
└─────────────────────────────────────────────────────────────┘
```

***

## FactoryATFi.sol

The factory contract creates and tracks all commitment vaults.

### Key Functions

| Function                                           | Description                    |
| -------------------------------------------------- | ------------------------------ |
| `createVault(token, stakeAmount, maxParticipants)` | Creates a new vault            |
| `getVault(vaultId)`                                | Returns vault address by ID    |
| `getVaultCount()`                                  | Returns total number of vaults |

### Events

| Event                                        | Description                   |
| -------------------------------------------- | ----------------------------- |
| `VaultCreated(vaultId, vaultAddress, owner)` | Emitted when vault is created |

***

## VaultATFi.sol

Individual commitment vault for each event.

### Lifecycle

```
CREATED → STAKING_OPEN → EVENT_STARTED → SETTLED
              │              │               │
              │              │               └─→ Participants can claim
              │              └─→ Staking closed, yield starts
              └─→ Participants can stake
```

### Key Functions

#### For Participants

| Function                | Description                          |
| ----------------------- | ------------------------------------ |
| `stake()`               | Register for event by staking tokens |
| `claim()`               | Claim rewards after settlement       |
| `getClaimable(address)` | View claimable amount                |
| `getStatus(address)`    | View participant status              |

#### For Organizers (Owner)

| Function                           | Description                                     |
| ---------------------------------- | ----------------------------------------------- |
| `start()`                          | Start the event (deposits to Morpho if enabled) |
| `verifyParticipant(addresses[])`   | Mark participants as verified                   |
| `settle()`                         | Finalize event and distribute rewards           |
| `openStaking()` / `closeStaking()` | Control registration                            |

### Events

| Event                               | Description          |
| ----------------------------------- | -------------------- |
| `Staked(participant, amount)`       | Participant staked   |
| `Verified(participant)`             | Participant verified |
| `Settled(totalYield, protocolFees)` | Event settled        |
| `Claimed(participant, amount)`      | Reward claimed       |

***

## Yield Integration

When yield is enabled (USDC only):

1. **On `start()`**: All staked USDC is deposited to Morpho Vault
2. **During event**: Yield accumulates
3. **On `settle()`**:
   * Morpho shares are redeemed
   * 10% protocol fee on yield
   * 10% no-show penalty
   * Remaining distributed to verified participants

***

## Security Features

* **Reentrancy Protection**: All state-changing functions are protected
* **Access Control**: Owner-only functions for organizer actions
* **Slippage Protection**: 5% slippage tolerance on Morpho withdrawals
* **Event State Machine**: Strict state transitions prevent invalid operations

***

## Deployment

See [Contract Addresses](https://github.com/ATFi-Event/docs/blob/main/contracts/addresses.md) for deployed contracts.

### Deployment Scripts

```bash
# Deploy Factory (Mainnet)
forge script script/DeployFactoryATFi.s.sol --rpc-url base --broadcast --verify

# Deploy Factory (Testnet)
forge script script/DeployFactoryATFiTestnet.s.sol --rpc-url base_sepolia --broadcast --verify
```

***

## Source Code

* [FactoryATFi.sol](https://github.com/ATFi-Event/sc/blob/main/src/FactoryATFi.sol)
* [VaultATFi.sol](https://github.com/ATFi-Event/sc/blob/main/src/VaultATFi.sol)
