API संदर्भ
इथेरियम API
Core API
गाइड

Rust टूल्स

Rust, Ethereum विकास के लिए कई लाइब्रेरीज प्रदान करता है, जिसमें 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)
    }
}

# एसिंक इवेंट हैंडलिंग

Ethereum इवेंट्स की सदस्यता लें:

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(())
    }
}

# टेस्टिंग

Ethereum इंटरैक्शन के परीक्षण का उदाहरण:

#[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());
    }
}

यह भी देखें

हमें बेहतर बनाने में मदद करें!
इस पृष्ठ को साझा करें और हमें आपके लिए और भी बेहतर उत्पाद बनाने में मदद करें।