apex-sdk-evm
EVM blockchain adapter for the Apex SDK, providing seamless interaction with Ethereum and EVM-compatible chains.
Note: This package has been fully migrated from ethers-rs to Alloy. Examples in this README are being updated. For the latest API usage, see the source code and tests.
Overview
apex-sdk-evm enables developers to interact with Ethereum and other EVM-compatible blockchains through a unified, type-safe Rust API. It supports HTTP and WebSocket connections, transaction building, smart contract interaction, and wallet management.
Features
- Multi-Chain Support: Ethereum, Polygon, BSC, Arbitrum, Optimism, and other EVM chains
- Modern Alloy Integration: Built on Alloy, the modern Ethereum library
- Connection Management: HTTP and WebSocket provider support with connection pooling
- Wallet Integration: BIP-39 mnemonic support, private key management, transaction signing
- Transaction Building: EIP-1559 and legacy transactions with automatic gas estimation
- Message Signing: EIP-191 (personal sign) and EIP-712 (typed data) support
- Type Safety: Compile-time guarantees with Alloy's strong typing
- Caching Layer: Intelligent caching for improved performance
- Metrics: Built-in monitoring and observability
Installation
Add this to your Cargo.toml:
[]
= "0.1.3"
= { = "1.0", = ["full"] }
Quick Start
Basic Connection
use EvmAdapter;
async
Using WebSocket
use EvmAdapter;
async
Wallet Management
Creating and Managing Wallets
use Wallet;
// Generate a new wallet
let wallet = generate;
println!;
println!;
// Import from private key (hex string with or without 0x prefix)
let wallet = from_private_key?;
// Import from mnemonic
let wallet = from_mnemonic?;
// Set chain ID for transaction signing
let wallet = wallet.with_chain_id; // Ethereum mainnet
Executing Transactions with Apex SDK
The recommended way to execute EVM transactions is through the unified Apex SDK interface:
use *;
use Wallet;
async
See examples/evm-transfer/ for a complete working example.
Smart Contract Interaction
Type-Safe Contract Calls with Alloy's sol! Macro
Apex SDK uses Alloy's sol! macro for compile-time safe contract interactions:
use sol;
use ;
use *;
// Define ERC20 interface using Alloy's sol! macro
sol!
async
For a complete example including write transactions, see examples/evm-contract-call/.
Key Advantages of sol! Macro
- Compile-time safety: Wrong types won't compile
- Automatic ABI encoding/decoding: No manual serialization
- Type inference: Rust knows the exact types of all parameters
- No runtime errors: All type checking happens at compile time
Advanced Features
Query Blockchain Data
use EvmAdapter;
use ChainAdapter;
async
Message Signing
use Wallet;
let wallet = from_private_key?;
// Sign a message (EIP-191 personal sign)
let message = "Hello, Ethereum!";
let signature = wallet.sign_message.await?;
println!;
// Verify signature
let recovered_address = signature.recover_address_from_msg?;
assert_eq!;
Supported Chains
The adapter works with any EVM-compatible blockchain:
// Ethereum Mainnet
let eth = new;
// Polygon
let polygon = new;
// BSC
let bsc = new;
// Arbitrum
let arbitrum = new;
// Optimism
let optimism = new;
// Local development (Ganache, Hardhat)
let local = new;
Error Handling
Comprehensive error types for robust applications:
use ;
match some_evm_operation.await
Testing
Unit Tests
Integration Tests
The integration tests require a running Ethereum node or testnet access.
Examples
Complete working examples are available in the examples directory:
- EVM Transfer - Execute ETH transfers on Sepolia testnet with wallet signing
- Contract Interaction - Type-safe ERC-20 contract calls using Alloy's
sol!macro - Account Manager - Multi-chain account management
- Contract Orchestration - Smart contract deployment across chains
- Price Oracle - Multi-chain price aggregation
All examples use the modern Alloy library and demonstrate blockchain interactions.
Configuration
Environment Variables
# Provider URLs
ETHEREUM_RPC_URL="https://eth.llamarpc.com"
POLYGON_RPC_URL="https://polygon-rpc.com"
# Private keys (use with caution)
PRIVATE_KEY="0x..." # For testing only
# API keys
INFURA_API_KEY="your-infura-key"
ALCHEMY_API_KEY="your-alchemy-key"
Configuration File
# config.toml
[]
= "ethereum"
= "30s"
[]
= "https://eth.llamarpc.com"
= 1
[]
= "https://polygon-rpc.com"
= 137
[]
= true
= 10000
= "5m"
[]
= true
= "0.0.0.0:9090"
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.
Contributing
Contributions are welcome! Please read CONTRIBUTING.md for guidelines.