<h1 align="center">
    🤵 EVM 客户端
</h1>
<h4 align="center">
一个轻量级的 EVM 客户端 Rust 库，可与多个 EVM 兼容的区块链无缝交互。支持连接 15 条以上的链，具备自动 RPC 故障转移功能支持。
</h4>
<p align="center">
  <a href="https://github.com/0xhappyboy/evm-client/LICENSE"><img src="https://img.shields.io/badge/License-GPL3.0-d1d1f6.svg?style=flat&labelColor=1C2C2E&color=BEC5C9&logo=googledocs&label=license&logoColor=BEC5C9" alt="License"></a>
</p>
<p align="center">
<a href="./README_zh-CN.md">简体中文</a> | <a href="./README.md">English</a>
</p>
 
 
# 依赖

```shell
cargo add evm-client
```

# 例子

## 基本连接示例

创建一个支持以太坊主网自动 RPC 选择的客户端

```rust
use evm_client::{EvmClient, EvmType};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = EvmClient::from_type(EvmType::ETHEREUM_MAINNET).await?;
    println!("✅ Connected to Ethereum Mainnet");
    client.health().await?;
    println!("✅ Health check passed");
    Ok(())
}
```

## 自定义 RPC 连接

使用特定的 RPC 端点进行连接

```rust
use evm_client::EvmClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = EvmClient::from_rpc("https://mainnet.infura.io/v3/your-api-key").await?;
    println!("✅ Connected via custom RPC");

    Ok(())
}
```

## 钱包整合

创建带有钱包的客户端以进行交易签名

```rust
use evm_client::{EvmClient, EvmType};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let private_key = "0xYourPrivateKeyHere";
    let client = EvmClient::from_wallet(EvmType::POLYGON_MAINNET, private_key).await?;
    println!("✅ Wallet client created");
    let chain_id = client.provider.get_chainid().await?;
    println!("🔗 Chain ID: {}", chain_id);
    Ok(())
}
```

## 多链测试

测试跨多条链的连接性

```rust
use evm_client::{EvmClient, EvmType};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let chains = [
        EvmType::ETHEREUM_MAINNET,
        EvmType::BSC_MAINNET,
        EvmType::POLYGON_MAINNET,
        EvmType::ARB_MAINNET,
        EvmType::BASE_MAINNET,
    ];
    for chain in chains {
        match EvmClient::from_type(chain).await {
            Ok(client) => {
                let chain_id = client.provider.get_chainid().await?;
                println!("✅ {} - Chain ID: {} - Connected", chain.name(), chain_id);
            }
            Err(e) => {
                println!("❌ {} - Connection failed: {}", chain.name(), e);
            }
        }
    }

    Ok(())
}
```

## 错误处理

处理连接失败错误

```rust
use evm_client::{EvmClient, EvmType, EvmClientError};

#[tokio::main]
async fn main() {
    match EvmClient::from_type(EvmType::ETHEREUM_MAINNET).await {
        Ok(client) => {
            println!("✅ Connection successful");

            match client.health().await {
                Ok(()) => println!("✅ Health check passed"),
                Err(EvmClientError::RpcError(msg)) => {
                    eprintln!("❌ Health check failed: {}", msg);
                }
            }
        }
        Err(EvmClientError::RpcError(msg)) => {
            eprintln!("❌ Connection failed: {}", msg);
        }
    }
}
```

## 带钱包的自定义 RPC

将自定义 RPC 与钱包功能相结合

```rust
use evm_client::EvmClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let rpc_url = "https://polygon-rpc.com";
    let private_key = "0xYourPrivateKeyHere";

    let client = EvmClient::from_rpc_and_wallet(rpc_url, private_key).await?;
    println!("✅ Custom RPC wallet client created");

    Ok(())
}
```

## 链信息

获取有关支持链的详细信息

```rust
use evm_client::EvmType;

fn main() {
    let chains = [
        EvmType::ETHEREUM_MAINNET,
        EvmType::ARB_MAINNET,
        EvmType::OPTIMISM_MAINNET,
    ];

    for chain in chains {
        println!("📋 Chain: {}", chain.name());
        println!("   ID: {}", chain.chain_id());
        println!("   RPC Endpoints: {}", chain.rpc().len());
        println!("---");
    }
}
```
