Skip to content

feat: add multi-network single signer example#244

Open
dicethedev wants to merge 1 commit intoalloy-rs:mainfrom
dicethedev:add-multichain-signer-example
Open

feat: add multi-network single signer example#244
dicethedev wants to merge 1 commit intoalloy-rs:mainfrom
dicethedev:add-multichain-signer-example

Conversation

@dicethedev
Copy link
Copy Markdown

Motivation

Closes #54

This PR adds a comprehensive example demonstrating how to use a single signer across multiple EVM-compatible networks. The example was specifically requested to show:

  • Creating a single PrivateKeySigner that can be reused across different networks
  • Connecting to multiple EVM-compatible networks (Ethereum, Optimism, Arbitrum)
  • Sending transactions to each network using the same wallet
  • Handling network-specific transaction receipt fields

This addresses a common use case for developers building multi-chain applications who need to understand how to efficiently manage signers across different EVM networks without creating separate signers for each chain.

Solution

Overview

Added multi_network_signer.rs to examples/transactions/examples/ which demonstrates:

  1. Single Signer Reuse: Creates one PrivateKeySigner from an environment variable and clones it for use with multiple providers
  2. Multi-Network Support: Connects to three testnet networks (Ethereum Sepolia, Optimism Sepolia, Arbitrum Sepolia) using AnyNetwork
  3. Transaction Flow: Sends a small amount of ETH (0.0001) to a recipient address on each network
  4. Network-Specific Fields: Extracts and displays L2-specific receipt fields:
    • Arbitrum: gasUsedForL1 and l1BlockNumber
    • Optimism: l1Fee, l1GasUsed, and l1GasPrice
  5. Gas Cost Comparison: Shows gas costs across different networks to demonstrate the efficiency gains of L2s

Key Features

  • Safety: Uses environment variable for private key (never hardcoded)
  • User-friendly: Comprehensive documentation with faucet links and setup instructions
  • Robust Error Handling: Gracefully handles insufficient balance, RPC errors, and missing network-specific fields
  • Educational: Clear output showing before/after balances, gas costs, and L2-specific data
  • Rate Limiting: Includes delays between network calls to avoid RPC rate limits

Code Structure

// Network configuration for easy extension
struct NetworkConfig {
    name: &'static str,
    rpc_url: &'static str,
    chain_id: u64,
    is_l2: bool,
}

// Iterates through networks, demonstrating:
// 1. Provider creation with shared signer
// 2. Balance checking
// 3. Transaction building and sending
// 4. Receipt extraction with network-specific fields
// 5. Cost calculation and comparison

How to Test

# 1. Export your MetaMask test wallet private key
export PRIVATE_KEY="0xYOUR_METAMASK_PRIVATE_KEY"

# 2. Fund the wallet at testnet faucets:
# - Ethereum Sepolia: https://sepoliafaucet.com/
# - Optimism Sepolia: https://app.optimism.io/faucet
# - Arbitrum Sepolia: https://faucet.quicknode.com/arbitrum/sepolia

# 3. Run the example
cargo run --example multi_network_signer

Expected behavior: The example will connect to each network, check balance, send a transaction if funded, and display network-specific receipt fields.

Testing

Tested successfully on all three networks:

Ethereum Sepolia:

  • Balance check working
  • Transaction sent and confirmed
  • Gas cost displayed correctly

Optimism Sepolia:

  • Balance check working
  • Transaction sent and confirmed
  • L1 fee fields extracted and displayed

Arbitrum Sepolia:

  • Balance check working
  • Transaction sent and confirmed
  • L1 gas usage fields extracted and displayed

All transactions confirmed within 30-60 seconds. The same signer worked seamlessly across all networks, demonstrating the power of EVM compatibility.

Related Examples

This example builds upon the existing any_network.rs example (as suggested by maintainers) and expands it to show practical multi-network usage with transaction sending, which was the primary request in issue #54.

PR Checklist

  • Added Documentation
    • Comprehensive doc comments explaining the example
    • Inline comments for complex sections
    • Instructions for running the example
    • Links to testnet faucets
    • Safety warnings about private keys
  • Breaking changes
    • No breaking changes - this is a new example addition

Additional Notes

Future Enhancements (can be separate PRs if desired):

  • Add contract interaction example (similar to the remove_liquidity request)
  • Show batch transactions across multiple networks
  • Demonstrate EIP-1559 transaction differences across networks
  • Add more L2 networks (Base, Polygon zkEVM, etc.)

Dependencies Added:

  • serde with derive feature for deserializing network-specific receipt fields
  • env_logger for optional debug logging
  • tokio time feature for rate limiting

I'm happy to make any adjustments based on feedback!

- Demonstrates using one signer across multiple EVM networks
- Shows Ethereum, Optimism, and Arbitrum Sepolia testnets
- Includes balance checking on each network
- References issue alloy-rs#54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Add example of using multiple EVM compatible networks, including single signer on multiple providers

1 participant