Diamond Pattern Architecture
LiFi contracts are built using the EIP-2535 Diamond Standard, a modular smart contract architecture that enables unlimited contract functionality while staying within Ethereum’s contract size limits.What is the Diamond Pattern?
The Diamond Pattern is a design pattern for organizing smart contract code that:- Separates logic into facets: Individual contracts (facets) that contain specific functionality
- Uses delegatecall: All facets share the same storage through a central Diamond contract
- Enables upgradeability: Facets can be added, replaced, or removed without redeploying the entire system
- Bypasses size limits: Breaks functionality into smaller contracts to avoid the 24KB contract size limit
Core Components
The LiFi Diamond architecture consists of:1. Diamond Contract
The main contract that:- Stores all state variables
- Routes function calls to appropriate facets via delegatecall
- Maintains a mapping of function selectors to facet addresses
2. Facets
Modular contracts that implement specific functionality:- Bridge Facets: Integration with cross-chain bridges (Across, Hop, Stargate, etc.)
- Swap Facets: DEX aggregation and token swapping capabilities
- Utility Facets: Helper functions, access control, withdrawal mechanisms
- Diamond Standard Facets: Core Diamond pattern functionality (DiamondCut, DiamondLoupe, Ownership)
3. Shared Libraries
Common libraries used across facets:- LibSwap: Swap execution and data structures
- LibDiamond: Diamond storage and management
- LibAccess: Access control utilities
How Facets Work Together
When a transaction is executed:- User calls a function on the Diamond contract address
- Diamond lookup: The fallback function looks up which facet implements that function
- Delegatecall execution: The Diamond delegates the call to the appropriate facet
- Shared storage: The facet executes using the Diamond’s storage context
- Return data: Results are returned to the user through the Diamond
Benefits of This Architecture
Modularity: Each bridge or feature is isolated in its own facet, making code easier to understand and maintain. Upgradeability: Individual facets can be upgraded without affecting other functionality. Unlimited Size: The Diamond can have unlimited functionality by distributing it across multiple facets. Gas Efficiency: Only load the code that’s needed for each transaction. Shared State: All facets access the same storage, enabling seamless data sharing.Data Flow Example
Here’s how a typical cross-chain swap and bridge transaction flows through the system:Key Concepts
Before diving into specific facets and data structures, it’s important to understand these core concepts:- Bridge Data: The standardized data structure used across all bridge integrations
- Swap Data: The structure defining token swap parameters
- Facets: The modular contracts that implement specific functionality
Common Patterns
Two-Step Transactions
Most facets offer two patterns:- Direct Bridge:
startBridgeTokensVia{Bridge}- Bridge tokens directly - Swap + Bridge:
swapAndStartBridgeTokensVia{Bridge}- Swap source tokens first, then bridge
BridgeData structure but the swap version accepts an additional SwapData[] parameter.
Event Emission
All transactions emit standardized events:LiFiTransferStarted: Marks the beginning of a cross-chain transferAssetSwapped: Logs each swap execution (if swaps are performed)- Bridge-specific events from the respective protocols
Access Control
Facets use shared access control viaLibAccess:
- Owner-only functions for critical operations
- Whitelisted DEX/bridge addresses
- Pausable functionality for emergency situations