apex-sdk-evm
EVM blockchain adapter for the Apex SDK, providing seamless interaction with Ethereum and EVM-compatible chains.
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
- Connection Management: HTTP and WebSocket provider support with connection pooling
- Wallet Integration: Key management, transaction signing, and account creation
- Smart Contracts: Type-safe contract interaction and deployment
- Transaction Building: Comprehensive transaction builder with gas estimation
- Caching Layer: Intelligent caching for improved performance
- Metrics: Built-in monitoring and observability
Installation
Add this to your Cargo.toml:
[]
= "0.1.2"
= { = "1.0", = ["full"] }
Quick Start
Basic Connection
use ;
async
Using WebSocket
use ;
let config = websocket;
let adapter = with_config.await?;
// Subscribe to new blocks
let mut stream = adapter.subscribe_blocks.await?;
while let Some = stream.next.await
Wallet Management
Creating and Managing Wallets
use ;
// Generate a new wallet
let wallet = new;
println!;
// Import from private key
let wallet = from_bytes?;
// Import from mnemonic
let wallet = from_mnemonic?;
Signing Transactions
use ;
let adapter = new;
let wallet = from_bytes?;
// Build and sign transaction
let tx = new
.to
.value
.build;
let signed_tx = wallet.sign_transaction.await?;
let hash = adapter.send_transaction.await?;
println!;
Smart Contract Interaction
Calling Contract Methods
use ;
use Abi;
let adapter = new;
// Load contract ABI and create contract instance
let abi: Abi = from_str?;
let contract = new;
// Call a read method
let balance: U256 = contract
.?
.call
.await?;
println!;
Deploying Contracts
use ;
let adapter = new;
let wallet = from_bytes?;
let deployer = new;
let contract = deployer
.constructor_args
.signer
.deploy
.await?;
println!;
Advanced Features
Connection Pooling
use ;
let pool_config = PoolConfig ;
let adapter = with_pool.await?;
Caching
use ;
let cache_config = CacheConfig ;
let adapter = new
.with_cache;
// Subsequent calls to same data will be cached
let balance1 = adapter.get_balance.await?;
let balance2 = adapter.get_balance.await?; // Cached
Gas Estimation and Optimization
use ;
let adapter = new;
// Automatic gas estimation
let tx = new
.to
.value
.gas_estimate_auto // Automatically estimate gas
.gas_price_auto // Automatically set gas price
.build;
// Manual gas settings
let tx = new
.to
.value
.gas_limit
.gas_price
.build;
Monitoring and Metrics
Built-in Metrics
use ;
let metrics_config = MetricsConfig ;
let adapter = new
.with_metrics;
// Access metrics
let metrics = adapter.metrics;
println!;
println!;
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 examples are available in the examples directory:
- Basic Transfer - Simple ETH transfers
- Contract Interaction - Smart contract calls
- Token Operations - ERC-20 token transfers
- NFT Operations - ERC-721/1155 interactions
- DeFi Integration - DeFi protocol 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.