MatrixBridge_L1_UltraLowGas

Description:

Smart contract deployed on Ethereum.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

/**
 * ============================================================================
 * MATRIX BRIDGE L1 - ULTRA LOW GAS VERSION
 * Target: Under $5 per bridge transaction
 * ============================================================================
 * 
 * GAS REDUCTION STRATEGY:
 * - L2_GAS: 100K (vs your current 1M = 10x cheaper!)
 * - Simple receive on L2 (no complex swapping in L2 bridge call)
 * - User swaps after receiving tokens on L2
 * 
 * COST BREAKDOWN:
 * - L1 execution: ~$2-3
 * - L2 gas prepayment (100K): ~$2-3
 * - Total: $4-6 ✅
 * 
 * ============================================================================
 */

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IERC20 {
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
}

interface IUniswapV2Router {
    function swapExactETHForTokens(
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external payable returns (uint[] memory amounts);
    
    function swapExactTokensForTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
    
    function WETH() external pure returns (address);
}

interface IL1StandardBridge {
    function depositERC20To(
        address _l1Token,
        address _l2Token,
        address _to,
        uint256 _amount,
        uint32 _l2Gas,
        bytes calldata _data
    ) external;
}

contract MatrixBridge_L1_UltraLowGas {
    
    // ========================================================================
    // CONSTANTS
    // ========================================================================
    
    address public constant PEPU_L1 = 0x93aA0ccD1e5628d3A841C4DbdF602D9eb04085d6;
    address public constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    address public constant UNISWAP_ROUTER = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
    address public constant L1_BRIDGE = 0xE92Df19F4e0Fd067FE3b788Cf03ffD06Cd9Be4A7;
    address public constant WPEPU_L2 = 0xF9Cf4A16d26979b929Be7176bAc4e7084975FCB8;
    
    uint256 private constant DEADLINE_BUFFER = 300;
    
    // ========================================================================
    // STATE VARIABLES
    // ========================================================================
    
    address public l2ReceiverContract;
    address public feeCollector;
    address public owner;
    
    // ULTRA LOW GAS: 100K instead of 1M (10x reduction!)
    uint32 public l2Gas = 100000;
    
    uint256 public feeBps = 30;
    uint256 private constant MAX_FEE = 500;
    uint256 private constant BPS_DENOMINATOR = 10000;
    
    // ========================================================================
    // EVENTS
    // ========================================================================
    
    event BridgeInitiated(
        address indexed user,
        address indexed inputToken,
        uint256 inputAmount,
        uint256 pepuAmount,
        uint256 feeAmount
    );
    
    event FeeCollected(uint256 amount);
    event FeeUpdated(uint256 newFeeBps);
    event L2GasUpdated(uint32 newL2Gas);
    
    // ========================================================================
    // ERRORS
    // ========================================================================
    
    error ZeroAmount();
    error ZeroAddress();
    error TransferFailed();
    error ExcessiveFee();
    error Unauthorized();
    error SwapFailed();
    error InvalidGas();
    
    // ========================================================================
    // CONSTRUCTOR
    // ========================================================================
    
    constructor(address _l2Receiver, address _feeCollector) {
        if (_l2Receiver == address(0) || _feeCollector == address(0)) revert ZeroAddress();
        l2ReceiverContract = _l2Receiver;
        feeCollector = _feeCollector;
        owner = msg.sender;
    }
    
    modifier onlyOwner() {
        if (msg.sender != owner) revert Unauthorized();
        _;
    }
    
    // ========================================================================
    // SIMPLIFIED BRIDGE FUNCTIONS
    // ========================================================================
    
    /**
     * @notice Bridge ETH → PEPU → L2 (user swaps on L2 themselves)
     * @param minPepuOut Minimum PEPU from swap
     */
    function bridgeETH(uint256 minPepuOut) external payable {
        if (msg.value == 0) revert ZeroAmount();
        
        address[] memory path = new address[](2);
        path[0] = WETH;
        path[1] = PEPU_L1;
        
        uint256 balanceBefore = IERC20(PEPU_L1).balanceOf(address(this));
        
        IUniswapV2Router(UNISWAP_ROUTER).swapExactETHForTokens{value: msg.value}(
            minPepuOut,
            path,
            address(this),
            block.timestamp + DEADLINE_BUFFER
        );
        
        uint256 pepuReceived = IERC20(PEPU_L1).balanceOf(address(this)) - balanceBefore;
        uint256 feeCollected = _collectFeeAndBridge(msg.sender, pepuReceived);
        
        emit BridgeInitiated(msg.sender, address(0), msg.value, pepuReceived, feeCollected);
    }
    
    /**
     * @notice Bridge PEPU directly
     * @param amount Amount of PEPU
     */
    function bridgePEPU(uint256 amount) external {
        if (amount == 0) revert ZeroAmount();
        
        if (!IERC20(PEPU_L1).transferFrom(msg.sender, address(this), amount)) {
            revert TransferFailed();
        }
        
        uint256 bridgeFee = _collectFeeAndBridge(msg.sender, amount);
        
        emit BridgeInitiated(msg.sender, PEPU_L1, amount, amount, bridgeFee);
    }
    
    /**
     * @notice Bridge any ERC20 → PEPU → L2
     * @param inputToken Token to bridge
     * @param amount Amount to bridge
     * @param minPepuOut Minimum PEPU to receive
     */
    function bridgeAnyToken(
        address inputToken,
        uint256 amount,
        uint256 minPepuOut
    ) external {
        if (amount == 0) revert ZeroAmount();
        if (inputToken == address(0)) revert ZeroAddress();
        
        if (!IERC20(inputToken).transferFrom(msg.sender, address(this), amount)) {
            revert TransferFailed();
        }
        
        if (inputToken == PEPU_L1) {
            uint256 pepuFee = _collectFeeAndBridge(msg.sender, amount);
            emit BridgeInitiated(msg.sender, inputToken, amount, amount, pepuFee);
            return;
        }
        
        IERC20(inputToken).approve(UNISWAP_ROUTER, amount);
        
        address[] memory path;
        
        if (inputToken == WETH) {
            path = new address[](2);
            path[0] = WETH;
            path[1] = PEPU_L1;
        } else {
            path = new address[](3);
            path[0] = inputToken;
            path[1] = WETH;
            path[2] = PEPU_L1;
        }
        
        uint256 balanceBefore = IERC20(PEPU_L1).balanceOf(address(this));
        
        IUniswapV2Router(UNISWAP_ROUTER).swapExactTokensForTokens(
            amount,
            minPepuOut,
            path,
            address(this),
            block.timestamp + DEADLINE_BUFFER
        );
        
        uint256 pepuReceived = IERC20(PEPU_L1).balanceOf(address(this)) - balanceBefore;
        
        if (pepuReceived == 0) revert SwapFailed();
        
        uint256 swapFee = _collectFeeAndBridge(msg.sender, pepuReceived);
        
        emit BridgeInitiated(msg.sender, inputToken, amount, pepuReceived, swapFee);
    }
    
    // ========================================================================
    // INTERNAL FUNCTIONS
    // ========================================================================
    
    /**
     * @dev Bridge PEPU to L2 with minimal gas
     * @notice User receives WPEPU on L2 and swaps themselves
     */
    function _collectFeeAndBridge(
        address user,
        uint256 pepuAmount
    ) private returns (uint256 feeAmount) {
        
        feeAmount = (pepuAmount * feeBps) / BPS_DENOMINATOR;
        uint256 amountAfterFee = pepuAmount - feeAmount;
        
        if (feeAmount > 0) {
            IERC20(PEPU_L1).transfer(feeCollector, feeAmount);
            emit FeeCollected(feeAmount);
        }
        
        IERC20(PEPU_L1).approve(L1_BRIDGE, amountAfterFee);
        
        // SIMPLIFIED: No output token encoding (user swaps on L2)
        bytes memory userData = abi.encode(user);
        
        // ULTRA LOW GAS: Only 100K (vs 1M = 10x cheaper!)
        IL1StandardBridge(L1_BRIDGE).depositERC20To(
            PEPU_L1,
            WPEPU_L2,
            user, // Send directly to user (not L2 contract)
            amountAfterFee,
            l2Gas,
            userData
        );
        
        return feeAmount;
    }
    
    // ========================================================================
    // ADMIN FUNCTIONS
    // ========================================================================
    
    /**
     * @notice Adjust L2 gas (start at 100K, increase if needed)
     */
    function setL2Gas(uint32 newL2Gas) external onlyOwner {
        if (newL2Gas < 50000 || newL2Gas > 500000) revert InvalidGas();
        l2Gas = newL2Gas;
        emit L2GasUpdated(newL2Gas);
    }
    
    function setFee(uint256 newFeeBps) external onlyOwner {
        if (newFeeBps > MAX_FEE) revert ExcessiveFee();
        feeBps = newFeeBps;
        emit FeeUpdated(newFeeBps);
    }
    
    function setFeeCollector(address newCollector) external onlyOwner {
        if (newCollector == address(0)) revert ZeroAddress();
        feeCollector = newCollector;
    }
    
    function setL2Receiver(address newReceiver) external onlyOwner {
        if (newReceiver == address(0)) revert ZeroAddress();
        l2ReceiverContract = newReceiver;
    }
    
    function transferOwnership(address newOwner) external onlyOwner {
        if (newOwner == address(0)) revert ZeroAddress();
        owner = newOwner;
    }
    
    function rescueTokens(address token, uint256 amount) external onlyOwner {
        IERC20(token).transfer(owner, amount);
    }
    
    function rescueETH() external onlyOwner {
        payable(owner).transfer(address(this).balance);
    }
    
    receive() external payable {}
}

/**
 * ============================================================================
 * HOW TO ACHIEVE <$5 GAS
 * ============================================================================
 * 
 * STRATEGY 1: ULTRA LOW L2 GAS (This Contract)
 * ----------------------------------------------
 * L2_GAS = 100,000 (vs your 1M)
 * - L1 execution: ~$2-3
 * - L2 gas: ~$2-3
 * - Total: $4-6 ✅
 * 
 * TRADEOFF:
 * - User receives WPEPU on L2
 * - User must swap WPEPU to desired token on L2 (separate tx, <$0.01)
 * - Still cheaper overall!
 * 
 * USER FLOW:
 * 1. Bridge on L1 ($5)
 * 2. Wait 5-10 minutes
 * 3. Swap on L2 DEX (<$0.01)
 * 4. Done! Total: ~$5
 * 
 * ============================================================================
 * 
 * STRATEGY 2: REMOVE L2 SWAP ENTIRELY
 * ------------------------------------
 * Instead of swapping on L2, just give them WPEPU:
 * 
 * Benefits:
 * - Even lower gas (50K L2 gas possible)
 * - Simpler contract
 * - Under $3-4 total!
 * 
 * Marketing:
 * "Bridge for $3, swap on L2 for pennies!"
 * 
 * ============================================================================
 * 
 * STRATEGY 3: BATCH BRIDGING (Future)
 * ------------------------------------
 * - Collect multiple user requests
 * - Bridge in batches
 * - Split L1 gas among users
 * - Could get to <$1 per user!
 * 
 * ============================================================================
 * 
 * COMPARISON:
 * 
 * Your Current: $100+ (1M L2 gas)
 * Optimized:    $15  (200K L2 gas)
 * Ultra-Low:    $5   (100K L2 gas) ← This contract
 * Minimal:      $3   (50K L2 gas, no swap)
 * 
 * ============================================================================
 */

Tags:
addr:0xa70d187b5537031a6a30569085bc5d54267d5c08|verified:true|block:23635924|tx:0x77a8c2771cbaff4e4ec0be7c4e9b94bb22183536ecff35255ee734986e927e05|first_check:1761295271

Submitted on: 2025-10-24 10:41:14

Comments

Log in to comment.

No comments yet.