Rust工具

Rust为以太坊开发提供了多个库,其中web3-rs是区块链交互的主要选择。

请注意!
嘿!友情提示:此页面旨在为您提供使用 Rust 请求的全面概述。有关实际代码示例,请查看我们的 API Methods documentation ,那里有所有支持语言的即用型示例!

# Web3-rs

Web3库的Rust实现。

use web3::Web3;
use web3::types::{Address, H256, TransactionParameters, U256};
use std::str::FromStr;

struct EthereumClient {
    web3: Web3<web3::transports::Http>,
}

impl EthereumClient {
    pub async fn new(url: &str) -> Result<Self, web3::Error> {
        let transport = web3::transports::Http::new(url)?;
        let web3 = Web3::new(transport);
        Ok(Self { web3 })
    }

    pub async fn get_balance(&self, address: &str) -> Result<U256, web3::Error> {
        let address = Address::from_str(address)
            .map_err(|_| web3::Error::InvalidAddress)?;

        self.web3.eth().balance(address, None).await
    }

    pub async fn send_transaction(
        &self,
        from: Address,
        to: Address,
        value: U256,
    ) -> Result<H256, web3::Error> {
        let tx = TransactionParameters {
            to: Some(to),
            value,
            from,
            ..Default::default()
        };

        self.web3.eth().send_transaction(tx).await
    }
}
  • GitHub: rust-web3
  • 文档: docs.rs/web3
  • 特性:
    • 异步支持
    • 类型安全接口
    • 合约交互
    • 交易管理
    • ENS支持
    • WebSocket支持

# 合约集成

在Rust中使用智能合约:

use web3::contract::{Contract, Options};
use web3::types::{Address, U256};

struct SmartContract {
    contract: Contract<web3::transports::Http>,
}

impl SmartContract {
    pub async fn new(
        web3: Web3<web3::transports::Http>,
        address: Address,
        abi: &[u8],
    ) -> Result<Self, web3::Error> {
        let contract = Contract::from_json(
            web3.eth(),
            address,
            abi,
        )?;

        Ok(Self { contract })
    }

    pub async fn call_method<T: serde::de::DeserializeOwned>(
        &self,
        method: &str,
        params: &[web3::contract::tokens::Tokenize],
    ) -> Result<T, web3::Error> {
        let result = self.contract.query(
            method,
            params,
            None,
            Options::default(),
            None,
        ).await?;

        Ok(result)
    }
}

# 错误处理

Rust中的强大错误处理:

use thiserror::Error;

#[derive(Error, Debug)]
pub enum EthereumError {
    #[error("Web3 error: {0}")]
    Web3Error(#[from] web3::Error),

    #[error("Invalid address: {0}")]
    InvalidAddress(String),

    #[error("Contract error: {0}")]
    ContractError(String),

    #[error("Transaction failed: {0}")]
    TransactionError(String),
}

impl EthereumClient {
    pub async fn safe_get_balance(&self, address: &str) -> Result<f64, EthereumError> {
        let wei = self.get_balance(address).await?;
        let eth = wei.as_u128() as f64 / 1e18;
        Ok(eth)
    }
}

# 异步事件处理

订阅以太坊事件:

use futures::StreamExt;

impl EthereumClient {
    pub async fn monitor_blocks(&self) -> Result<(), web3::Error> {
        let mut stream = self.web3.eth_subscribe().subscribe_new_heads().await?;

        while let Some(block) = stream.next().await {
            match block {
                Ok(header) => {
                    println!("New block: {}", header.number.unwrap_or_default());
                }
                Err(e) => {
                    eprintln!("Error: {}", e);
                }
            }
        }

        Ok(())
    }
}

# 测试

以太坊交互测试示例:

#[cfg(test)]
mod tests {
    use super::*;
    use tokio;

    #[tokio::test]
    async fn test_balance_query() {
        let client = EthereumClient::new("http://localhost:8545")
            .await
            .expect("Failed to create client");

        let balance = client
            .get_balance("0x742d35Cc6634C0532925a3b844Bc454e4438f44e")
            .await
            .expect("Failed to get balance");

        assert!(balance > U256::zero());
    }
}

另请参阅

帮助我们变得更好!
分享此页面并帮助我们为您创建更好的产品。