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

# Architecture

> Modular design, upgradeable, gas-optimized

Technical architecture of TED Protocol smart contracts and systems.

***

### Overview

TED Protocol is built on a modular, upgradeable architecture using the **Diamond Pattern (EIP-2535)**.

User transactions flow through a single **DiamondProxy** entry point, which routes calls to specialized facets:

* **Swap Facets** — FXSwapFacet, CurveAdapter, UniswapV3
* **Bridge Facets** — CCTPFacet, LayerZeroFacet, WormholeFacet
* **Admin Facets** — AdminFacet, DiamondCut, DiamondLoupe

***

### Diamond Pattern (EIP-2535)

#### Why Diamond?

* **Modularity** — Each feature is a separate facet
* **Upgradeability** — Update single facets without redeploying entire contracts
* **No size limits** — Bypasses the 24KB contract limit
* **Single address** — One entry point for all functions
* **Gas efficiency** — delegatecall-based routing

#### Structure

The **DiamondProxy** contract maps function selectors to facet addresses.

1. When a function is called, the proxy looks up which facet handles that selector
2. Delegates the call to that facet
3. Functionality can be added, replaced, or removed without changing the main contract address

#### Storage Pattern

All facets share **diamond storage** using a unique storage position (keccak256 hash), preventing storage collisions that can occur with traditional proxy patterns.

Storage struct contains:

* Facet mappings
* Owner address
* Protocol state such as pause status

***

### Core Facets

#### FXSwapFacet

Handles stablecoin FX swaps with multi-DEX routing.

| Function     | Description                                                            |
| ------------ | ---------------------------------------------------------------------- |
| `swap()`     | Execute token exchanges                                                |
| `getQuote()` | Return expected output, price impact, route details, and gas estimates |

#### DEX Adapters

| Adapter               | Target         | Features                                      |
| --------------------- | -------------- | --------------------------------------------- |
| **CurveAdapter**      | Curve Finance  | Optimized for stablecoin pools and meta pools |
| **UniswapAdapter**    | Uniswap V3     | Concentrated liquidity                        |
| **PancakeAdapter**    | PancakeSwap V3 | BSC pools                                     |
| **DragonSwapAdapter** | DragonSwap     | Kaia pools                                    |

#### Bridge Facets

| Facet              | Bridge       | Supported Tokens |
| ------------------ | ------------ | ---------------- |
| **CCTPFacet**      | Circle CCTP  | USDC             |
| **LayerZeroFacet** | LayerZero V2 | USDT, TEDP (OFT) |
| **WormholeFacet**  | Wormhole     | General tokens   |

***

### Data Flow

#### Same-Chain Swap

1. User calls `swap()` with token pair and amount
2. **DiamondProxy** routes to **FXSwapFacet**
3. Facet queries all DEX adapters in parallel
4. Selects optimal route and executes swap
5. Sends output tokens to user

#### Cross-Chain Swap

**Source chain:**

1. User calls `crossChainSwap()`
2. Swap to bridge-compatible token if needed
3. Initiate cross-chain transfer via the appropriate bridge facet

**Bridge:**

* Bridge protocol delivers the message

**Destination chain:**

1. Receive tokens
2. Swap to target token if needed
3. Deliver final tokens to user

***

### Security Architecture

#### Access Control

All changes flow through:

1. **Multi-sig** (3/5 requirement)
2. **Timelock** (48 hours)
3. **DiamondProxy**

This ensures no single party can make changes, and users have time to react to proposed modifications.

#### Emergency Controls

| Action    | Required Signatures | Purpose                            |
| --------- | ------------------- | ---------------------------------- |
| Pause     | 2-of-5              | Stop all operations                |
| Unpause   | 3-of-5              | Resume operations                  |
| Upgrade   | 3-of-5 + timelock   | Deploy new facets                  |
| Emergency | 4-of-5              | Bypass timelock for critical fixes |

***

### TEDP Token Architecture

#### OFT Standard

**TEDP** implements LayerZero **OFT (Omnichain Fungible Token)**.

* Combined with ERC-20 and ERC-20Permit
* Fixed supply of 1 billion tokens minted at deployment
* Seamless cross-chain transfers

#### Cross-Chain Flow

1. **Source chain** — TEDP tokens are burned
2. **Verification** — LayerZero DVNs (Decentralized Verifier Networks) verify the burn transaction
3. **Destination chain** — Equivalent TEDP minted to the recipient

> Total supply remains constant across all chains.

***

### External Integrations

#### DEX Contracts

| DEX         | Contracts                        | Notes              |
| ----------- | -------------------------------- | ------------------ |
| Curve       | Pool-specific routers + registry | For pool discovery |
| Uniswap V3  | SwapRouter02 + UniswapV3Factory  | -                  |
| PancakeSwap | SmartRouter + V3Factory          | BSC                |

#### Bridge Contracts

| Bridge      | Contracts                          |
| ----------- | ---------------------------------- |
| Circle CCTP | TokenMessenger, MessageTransmitter |
| LayerZero   | Endpoint, DVN, Executor            |
| Wormhole    | Core Bridge, Token Bridge          |

***

### Gas Optimization

#### Optimization Techniques

| Technique              | Savings            |
| ---------------------- | ------------------ |
| Batch operations       | 30-50%             |
| Storage packing        | 20-40%             |
| Assembly optimization  | 10-20%             |
| Minimal proxy patterns | 50%+ on deployment |

#### Typical Gas Costs

| Operation              | Gas (Ethereum/L2)     |
| ---------------------- | --------------------- |
| Simple swap            | \~150k                |
| Multi-hop swap         | \~300k                |
| Cross-chain initiation | \~200k (source chain) |
