Skip to content

lighterim/lighter-evm-contract

Repository files navigation

Lighter EVM Contract

License: MIT Solidity Foundry

A decentralized Take Intent processing system built on Ethereum, enabling gasless token authorization and secure escrow transactions through Uniswap Permit2 and ERC6551 Token Bound Accounts.

πŸ“‹ Table of Contents

🎯 Overview

Lighter EVM Contract is a smart contract system that implements a decentralized transaction intent processing mechanism. The system allows buyers and sellers to express trading intentions through EIP-712 signed intents, execute transactions through relayers, and provides secure fund escrow services.

Core Concepts

  • Take Intent: A mechanism where users express trading intentions (buy/sell) through signed messages, allowing relayers to execute transactions on their behalf
  • Permit2 Integration: Uses Uniswap Permit2 for gasless token authorization and transfers
  • Token Bound Accounts: ERC6551-based account system for enhanced user experience
  • Escrow System: Secure fund custody with multiple release mechanisms (seller release, verifier release, dispute resolution)
  • TransientStorage: EIP-1153 based reentrancy protection for efficient state management
  • Gas Optimization: Inline assembly implementations for critical hashing operations

✨ Key Features

1. Three Intent Modes

Take Seller Intent

The seller publishes a sell intent and authorizes token transfer via Permit2. The buyer initiates the transaction.

Process:

  1. Seller signs a token transfer authorization using Permit2.permitWitnessTransferFrom with IntentParams as witness
  2. Relayer signs EscrowParams (escrow parameters)
  3. Buyer executes execute, transferring seller's tokens to Escrow contract

Take Buyer Intent

The buyer publishes a purchase intent. The seller authorizes tokens via Permit2 before executing.

Process:

  1. Buyer signs IntentParams
  2. Seller authorizes tokens via Permit2
  3. Relayer signs EscrowParams
  4. Seller executes execute to initiate the transaction

Take Bulk Sell Intent

The seller authorizes all tokens to AllowanceHolder at once via Permit2's permit function, supporting batch transactions.

Process:

  1. Seller authorizes tokens to AllowanceHolder via Permit2 permit
  2. Seller signs IntentParams
  3. Relayer signs EscrowParams
  4. Buyer can execute multiple times, transferring tokens through AllowanceHolder

2. Advanced Features

  • βœ… EIP-712 Signatures: Typed data signatures for better UX and security
  • βœ… ERC6551 Support: Token Bound Accounts (TBA) for account abstraction
  • βœ… EIP-1153 TransientStorage: Efficient reentrancy protection using transient storage (gas-optimized)
  • βœ… Escrow Custody: Multi-stage escrow with dispute resolution
  • βœ… Payment Method Registry: Configurable payment methods with window periods
  • βœ… User Honour System: Track user reputation and transaction history
  • βœ… Waypoint Support: Escrow lifecycle management (payment, cancellation, dispute, resolution)
  • βœ… ZK Verification: Zero-knowledge proof verification support (ZkVerifyProofVerifier)
  • βœ… Gas-Optimized Hashing: Inline assembly implementations for efficient keccak256 hashing

πŸ—οΈ Architecture

Contract Inheritance Structure

The project uses a modular multiple inheritance architecture, following the 0x-settler design pattern:

Three Main Business Lines:

  1. Take Intent Line:

    Context
      ↓
    SettlerAbstract β†’ SettlerBase
      ↓
    Permit2PaymentAbstract β†’ Permit2PaymentTakeIntent
      ↓
    Settler (core execution logic)
      ↓
    MainnetTakeIntent (mainnet implementation)
    
  2. Waypoint Line:

    Context
      ↓
    SettlerAbstract β†’ SettlerBase
      ↓
    WaypointAbstract
      ↓
    SettlerWaypoint
      ↓
    MainnetWaypoint (mainnet implementation)
    
  3. Verifier Line (in development):

    Context
      ↓
    VerifierAbstract
      ↓
    ZkVerifyProofVerifier (mainnet implementation)
    

Core Foundation:

  • Context: Base contract providing escrow, relayer, and signature verification
  • SettlerBase: Core base contract with TransientStorage-based reentrancy protection, payment method registry, and fee calculations

Directory Structure

src/
β”œβ”€β”€ account/              # ERC6551 Token Bound Account implementation
β”‚   β”œβ”€β”€ LighterAccount.sol      # Main account contract with honour system
β”‚   β”œβ”€β”€ AccountV3.sol          # ERC6551 account implementation
β”‚   β”œβ”€β”€ ERC6551Registry.sol     # ERC6551 registry
β”‚   └── TokenBoundConfig.sol   # TBA configuration
β”œβ”€β”€ allowanceholder/      # Permit2 allowance holder for bulk transactions
β”‚   β”œβ”€β”€ AllowanceHolder.sol
β”‚   └── IAllowanceHolder.sol
β”œβ”€β”€ chains/              # Chain-specific implementations
β”‚   └── Mainnet/
β”‚       β”œβ”€β”€ TakeIntent.sol           # Mainnet take intent implementation
β”‚       β”œβ”€β”€ Waypoint.sol            # Mainnet waypoint implementation
β”‚       β”œβ”€β”€ ZkVerifyProofVerifier.sol # ZK proof verifier
β”‚       └── Common.sol               # Common utilities
β”œβ”€β”€ core/                # Core abstract contracts and implementations
β”‚   β”œβ”€β”€ Permit2Payment.sol          # Permit2 payment implementation
β”‚   β”œβ”€β”€ Permit2PaymentAbstract.sol  # Permit2 payment abstract
β”‚   β”œβ”€β”€ SettlerErrors.sol           # Custom error definitions
β”‚   β”œβ”€β”€ WaypointAbstract.sol        # Waypoint abstract contract
β”‚   β”œβ”€β”€ VerifierAbstract.sol        # Verifier abstract contract
β”‚   └── ZkVerifier.sol              # ZK verifier base
β”œβ”€β”€ interfaces/          # Interface definitions
β”‚   β”œβ”€β”€ ISettlerBase.sol            # Core data structures
β”‚   β”œβ”€β”€ ISettlerTakeIntent.sol      # Take intent interface
β”‚   β”œβ”€β”€ ISettlerWaypoint.sol        # Waypoint interface
β”‚   β”œβ”€β”€ IEscrow.sol                 # Escrow interface
β”‚   β”œβ”€β”€ IPaymentMethodRegistry.sol  # Payment method registry interface
β”‚   └── ...
β”œβ”€β”€ utils/              # Utility libraries
β”‚   β”œβ”€β”€ ParamsHash.sol              # EIP-712 parameter hashing (gas-optimized)
β”‚   β”œβ”€β”€ TransientStorage.sol        # EIP-1153 transient storage for reentrancy
β”‚   β”œβ”€β”€ SignatureVerification.sol   # Signature verification utilities
β”‚   β”œβ”€β”€ UnsafeMath.sol              # Unsafe math operations
β”‚   β”œβ”€β”€ Permit2Helper.sol           # Permit2 helper functions
β”‚   └── ...
β”œβ”€β”€ vendor/             # Third-party vendor libraries
β”‚   β”œβ”€β”€ SafeTransferLib.sol         # Safe token transfer library
β”‚   β”œβ”€β”€ FullMath.sol                # Full precision math
β”‚   └── ...
β”œβ”€β”€ Escrow.sol          # Escrow contract (fund custody)
β”œβ”€β”€ Settler.sol         # Core settler contract (intent execution)
β”œβ”€β”€ SettlerBase.sol     # Base settler functionality
β”œβ”€β”€ SettlerAbstract.sol # Abstract settler interface
β”œβ”€β”€ SettlerWaypoint.sol # Waypoint settler implementation
β”œβ”€β”€ PaymentMethodRegistry.sol # Payment method registry
└── ISettlerActions.sol # Action selectors

πŸ”§ Technical Implementation

Gas Optimization

The project implements several gas optimization techniques:

  1. EIP-1153 TransientStorage: Uses transient storage (tload/tstore) instead of permanent storage for reentrancy protection, saving ~20,000 gas per operation
  2. Inline Assembly Hashing: All ParamsHash functions use inline assembly for keccak256 hashing, avoiding abi.encode overhead
  3. CalldataDecoder: Optimized calldata decoding without bounds checking (documented trade-off for gas efficiency)
  4. Batch Operations: Supports bulk transactions through AllowanceHolder for efficient token transfers

Reentrancy Protection

The system uses a multi-layered reentrancy protection approach:

  • TransientStorage: EIP-1153 based protection for transaction-level state
  • ReentrancyGuard: OpenZeppelin's ReentrancyGuard in Escrow contract
  • State Validation: Ensures payer, witness, and intent state are properly managed

Code Quality

  • Foundry Linting: All code follows Foundry linting recommendations
  • Gas-Optimized Patterns: Inline assembly where appropriate for critical paths
  • Comprehensive Testing: Unit tests for core libraries (ParamsHash, etc.)
  • Code Review: Regular code reviews and security analysis

πŸ“¦ Core Contracts

Settler Contracts

  • SettlerAbstract: Abstract interface defining _dispatch and _dispatchVIP methods
  • SettlerBase: Core base contract providing:
    • EIP-1153 TransientStorage-based reentrancy protection
    • Payment method registry integration
    • Fee calculation utilities (getAmountWithFee, getFeeAmount)
    • Intent validation and state management
  • Settler: Core execution contract handling transaction intent execution logic
    • Implements _dispatch for action routing
    • Supports multiple intent types (seller intent, buyer intent, bulk sell)
  • MainnetTakeIntent: Mainnet implementation inheriting from Settler
  • SettlerWaypoint: Waypoint contract for escrow lifecycle management
    • Handles payment, cancellation, disputes, and resolution
  • MainnetWaypoint: Mainnet waypoint implementation

Account & Token Management

  • LighterAccount: ERC6551 Token Bound Account implementation
    • Mints Ticket NFTs and creates corresponding TBAs
    • Manages user honour system
    • Tracks pending transactions and quotas
  • LighterTicket: ERC721 NFT contract for user tickets
  • AllowanceHolder: Permit2 authorization holder for bulk transactions

Escrow & Payment

  • Escrow: Fund escrow contract managing transaction lifecycle
    • Creates and manages escrow transactions
    • Handles payment, release, cancellation, and dispute resolution
    • Supports multiple release mechanisms
  • PaymentMethodsRegistry: Registry for payment methods with configurable windows

Core Interfaces

  • ISettlerBase: Core data structures (IntentParams, EscrowParams, EscrowStatus, PaymentMethodConfig)
  • ISettlerTakeIntent: Interface for take intent functionality
  • ISettlerWaypoint: Interface for waypoint functionality
  • IEscrow: Escrow contract interface with full lifecycle management
  • ISettlerActions: Action selectors for transaction execution
  • IPaymentMethodRegistry: Payment method registry interface

Utility Libraries

  • ParamsHash: Gas-optimized EIP-712 parameter hashing using inline assembly
    • Supports Range, IntentParams, EscrowParams, TokenPermissions
    • All hash functions optimized with inline assembly for gas efficiency
  • TransientStorage: EIP-1153 transient storage library for reentrancy protection
    • Manages payer, witness, intentTypeHash, and tokenPermissions in transient storage
    • Provides efficient state management without permanent storage costs
  • CalldataDecoder: Efficient calldata decoding library (used in SettlerBase)

πŸš€ Quick Start

Prerequisites

  • Foundry: >= 1.0.0 (Installation Guide)
  • Solidity: 0.8.25
  • Node.js: >= 18.0.0 (optional, for Hardhat scripts)

Installation

# Clone the repository
git clone https://github.com/lighterim/lighter-evm-contract.git
cd lighter-evm-contract

# Install Foundry (if not installed)
curl -L https://foundry.paradigm.xyz | bash
foundryup

# Install dependencies
forge install

# Install Node.js dependencies (optional, for Hardhat)
npm install

Environment Setup

Create a .env file in the root directory:

# RPC endpoints
MAINNET_RPC_URL=https://eth-mainnet.g.alchemy.com/v2/YOUR_KEY
SEPOLIA_RPC_URL=https://eth-sepolia.g.alchemy.com/v2/YOUR_KEY
BASE_MAINNET_RPC_URL=https://mainnet.base.org
BNB_MAINNET_RPC_URL=https://bsc-dataseed.binance.org

# Deployment account
PRIV_KEY=your_private_key_here
DEPLOYER=0xYourDeployerAddress

# Test accounts (optional)
BUYER_PRIVATE_KEY=your_buyer_private_key
SELLER_PRIVATE_KEY=your_seller_private_key
RELAYER_PRIVATE_KEY=your_relayer_private_key

Compilation

# Compile with Foundry
forge build

# Or compile with Hardhat (optional)
npm run compile

πŸ’» Development

Code Formatting

# Format code
forge fmt

# Check formatting
forge fmt --check

Gas Optimization

# Generate gas report
forge test --gas-report

Code Coverage

# Generate coverage report
forge coverage

πŸ§ͺ Testing

Run All Tests

forge test

Run Specific Test Suites

# Test Settler functionality
forge test --match-path test/Settler.t.sol -vvvvv

# Test Take Intent
forge test --match-path test/TakeIntent.t.sol -vvvvv

# Test Waypoint
forge test --match-path test/Waypoint.t.sol -vvvvv

# Test unit tests (ParamsHash, etc.)
forge test --match-path test/unit/ -vvvvv

# Test Permit2 transfers
forge test --match-path test/Permit2TransferTest.t.sol -vvvvv

Fork Testing

Tests support testnet fork testing:

# Set RPC URL
export SEPOLIA_RPC_URL=your_rpc_url

# Run fork tests
forge test --match-path test/TakeIntent.t.sol --ffi -vvvvv

Test Coverage

# Generate coverage report
forge coverage

# Run unit tests
forge test --match-path test/unit/ -vv

# Run integration tests
forge test --match-path test/TakeIntent.t.sol -vv
forge test --match-path test/Waypoint.t.sol -vv

πŸ“¦ Deployment

Prerequisites

  1. Set up environment variables (see Environment Setup)
  2. Ensure sufficient balance for deployment
  3. Configure network in foundry.toml or hardhat.config.ts

Foundry Deployment

# Deploy to Sepolia testnet
forge script script/Deploy.s.sol \
  --rpc-url $SEPOLIA_RPC_URL \
  --broadcast \
  --private-key $PRIV_KEY \
  --verify

Hardhat Deployment

# Deploy using Hardhat
npx hardhat run scripts/deploy-sepolia.ts --network sepolia

Contract Verification

# Verify contract on Etherscan
forge verify-contract \
  --rpc-url https://sepolia.drpc.org \
  --verifier etherscan \
  --verifier-url https://api-sepolia.etherscan.io/api \
  $CONTRACT_ADDRESS \
  src/account/LighterAccount.sol:LighterAccount \
  --constructor-args $(cast abi-encode "constructor(address,address,address,uint256)" $TICKET_CONTRACT $REGISTRY $ACCOUNT_IMPL 0)

For detailed deployment instructions, see DEPLOYMENT_README.md.

πŸ”’ Security

Security Considerations

  • ⚠️ Before Production: Conduct a comprehensive security audit
  • ⚠️ Private Key Management: Never commit private keys to the repository
  • ⚠️ Access Control: Review all authorization mechanisms carefully
  • ⚠️ Test Coverage: Ensure all critical paths have test coverage
  • ⚠️ Reentrancy: Escrow contract uses ReentrancyGuard for protection

Known Limitations

  1. Verifier Branch: VerifierAbstract and ZK verification are in active development
  2. Signature Replay: Consider adding nonce mechanism for additional protection
  3. CalldataDecoder: Uses optimized decoding without bounds checking (documented trade-off for gas efficiency)

Security Best Practices

  • Use multi-sig wallets for contract ownership
  • Implement time-locked upgrades for critical contracts
  • Monitor contract events for suspicious activity
  • Regular security audits and code reviews

πŸ“š Documentation

Core Documentation

Contract Documentation

  • ISettlerActions: Action selectors for all supported operations
  • ISettlerBase: Core data structures and enums
  • IEscrow: Escrow contract interface and events

External Resources

🀝 Contributing

We welcome contributions! Please see our contributing guidelines:

Ways to Contribute

  • πŸ› Report Bugs: Open an issue with detailed information
  • πŸ’‘ Suggest Features: Propose new features or improvements
  • πŸ“ Improve Documentation: Help improve our documentation
  • πŸ”§ Submit Code: Submit pull requests with fixes or features
  • βœ… Add Tests: Increase test coverage

Development Workflow

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add tests for new functionality
  5. Ensure all tests pass (forge test)
  6. Format code (forge fmt)
  7. Commit your changes (git commit -m 'Add amazing feature')
  8. Push to the branch (git push origin feature/amazing-feature)
  9. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License.

πŸ™ Acknowledgments

πŸ“ž Contact & Support

πŸ”— Related Links


⚠️ Disclaimer: This project is in active development. Please conduct thorough security audits and testing before production deployment.

About

Solidity contracts for Lighter.IM

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors