What are Facets?
Facets are modular smart contracts that implement specific functionality within the LiFi Diamond architecture. Each facet is a separate contract that contains a focused set of functions, and all facets share the same storage through the central Diamond contract. Think of facets as “plug-in modules” that can be added, removed, or upgraded independently without affecting the rest of the system.How Facets Are Organized
Naming Convention
All facets follow a consistent naming pattern:- Bridge Facets:
{BridgeName}Facet(e.g.,AcrossFacet,StargateFacet) - Utility Facets:
{Function}Facet(e.g.,GenericSwapFacet,WithdrawFacet) - Diamond Facets: Standard Diamond pattern facets (
DiamondCutFacet,DiamondLoupeFacet)
Function Organization
Each facet typically provides:- Primary functions: Core functionality (e.g.,
startBridgeTokensVia{Bridge}) - Alternative functions: Variants with different capabilities (e.g.,
swapAndStartBridgeTokensVia{Bridge}) - Helper functions: Internal utilities (if public)
Storage Pattern
Facets use the Diamond Storage pattern to avoid storage collisions:Facet Categories
1. Bridge Facets
Bridge facets integrate with cross-chain bridge protocols to enable token transfers between blockchains.Common Bridge Facets
Layer 2 & Optimistic Rollup BridgesArbitrumBridgeFacet: Native Arbitrum bridge integrationOptimismBridgeFacet: Native Optimism bridge integrationPolygonBridgeFacet: Polygon PoS bridge integration
AcrossFacet: Across Protocol integration (v1)AcrossFacetV3: Across Protocol v3AcrossFacetV4: Across Protocol v4StargateFacetV2: Stargate v2 bridgeHopFacet: Hop Protocol bridgeCBridgeFacet: Celer cBridge integrationAllBridgeFacet: AllBridge integrationOmniBridgeFacet: xDai OmniBridge
ChainflipFacet: Chainflip cross-chain swapsMayanFacet: Mayan Finance bridgeSquidFacet: Squid Router integrationSymbiosisFacet: Symbiosis ProtocolDeBridgeDlnFacet: deBridge DLN integrationRelayFacet: Relay Protocol integration
Bridge Facet Pattern
All bridge facets follow a similar structure:2. Swap Facets
Swap facets provide DEX aggregation and token swapping capabilities.Available Swap Facets
GenericSwapFacet: Generic DEX integration supporting any whitelisted DEXGenericSwapFacetV3: Enhanced version with additional features
Swap Facet Functions
3. Utility Facets
Utility facets provide auxiliary functions for contract management and operations.Core Utility Facets
Access Control & Management-
OwnershipFacet: Contract ownership managementtransferOwnership(address newOwner)confirmOwnershipTransfer()cancelOwnershipTransfer()owner()- Get current owner
-
AccessManagerFacet: Fine-grained access control -
WhitelistManagerFacet: Manage whitelisted contracts and addresses
WithdrawFacet: Emergency token withdrawalwithdraw(address token, address to, uint256 amount)withdrawNative(address to, uint256 amount)
CalldataVerificationFacet: Verify transaction calldataEmergencyPauseFacet: Emergency pause functionality
PeripheryRegistryFacet: Manage peripheral contract registrations
Example: WithdrawFacet
4. Diamond Standard Facets
These facets implement the core EIP-2535 Diamond Standard functionality.DiamondCutFacet
Enables upgrading the Diamond by adding, replacing, or removing facets.0(Add): Add new functions from a facet1(Replace): Replace existing functions with new implementation2(Remove): Remove functions from the Diamond
DiamondLoupeFacet
Provides introspection functions to query the Diamond’s facet configuration.Working with Facets
Connecting to Facets
All facets are accessed through the Diamond contract address:Discovering Available Facets
Checking if a Facet Exists
Facet Versioning
Some facets have multiple versions to support protocol upgrades:AcrossFacet(v1) →AcrossFacetV3→AcrossFacetV4GenericSwapFacet→GenericSwapFacetV3StargateFacet→StargateFacetV2
Best Practices
1. Use Typed Facet Interfaces
Always use TypeScript types from the generated contract types:2. Handle Facet Upgrades
Build your integration to gracefully handle facet upgrades:3. Verify Facet Availability
For critical integrations, verify facets exist before calling:4. Monitor Facet Changes
Subscribe toDiamondCut events to detect facet changes: