tomusdrw / rust-web3

Ethereum JSON-RPC multi-transport client. Rust implementation of web3 library. ENS address: rust-web3.eth
MIT License
1.45k stars 465 forks source link

How to get transactions from the specified from_address and to_address #604

Open YeautyYE opened 2 years ago

YeautyYE commented 2 years ago

I looked it up with the following code, but it was too slow

        let latest_block = web3
            .eth()
            .block(BlockId::Number(BlockNumber::Latest))
            .await
            .unwrap()
            .unwrap();

        for transaction_hash in latest_block.transactions {
            let tx = match web3
                .eth()
                .transaction(TransactionId::Hash(transaction_hash))
                .await
            {
                Ok(Some(tx)) => tx,
                _ => {
                    println!("An error occurred");
                    continue;
                }
            };
            let from_addr = tx.from.unwrap_or(H160::zero());
            let to_addr = tx.to.unwrap_or(H160::zero());
            println!(
                "[{}] from {}, to {}, value {}, gas {}, gas price {:?}",
                tx.transaction_index.unwrap_or(U64::from(0)),
                from_addr,
                to_addr,
                tx.value,
                tx.gas,
                tx.gas_price,
            );
        }