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

Error: Transport("Cannot send request. Internal task finished.") #647

Open palinko91 opened 2 years ago

palinko91 commented 2 years ago

Hello I'm getting this error while listening for new pending transactions, or even if I'm using subscribe_logs with a filter, but right now I'm showing code example for the new pending transaction filter.

use std::env;
use tokio::*;
use web3::*;

use futures::stream::StreamExt;
use web3::contract::tokens::Tokenize;
use web3::transports::ws::WebSocket;
use web3::types::{TransactionId, Transaction, H256};

async fn list(pendingtx: H256, web3s: Web3<WebSocket>) -> Option<Transaction> {
    let tx = match web3s
        .eth()
        .transaction(TransactionId::Hash(pendingtx))
        .await
    {
        Ok(Some(tx)) => return Some(tx),
        _ => {
            return None;
        }
    };
}

#[tokio::main]
async fn main() -> web3::Result {
    // Loading the .env file
    dotenv().expect("Failed to read .env file");

    // Make the node connection
    let api_link = &env::var("NODE_LINK").expect("NODE_LINK must be set");
    let transport = web3::transports::WebSocket::new(&api_link).await?;
    let web3s = web3::Web3::new(transport.clone());

    // Setup the subscription stream for new pending transactions
    let mut subscription = web3s
        .eth_subscribe()
        .subscribe_new_pending_transactions()
        .await?;

    loop {
        while let Some(pendingtx) = subscription.next().await {
            let listed = list(pendingtx.unwrap(), web3s.clone());

            tokio::spawn(async move {
                match listed.await {
                    Some(result) => {
                        println!("Transaction sent from: {:?}", result.from.unwrap());
                        println!("------------------------------------------------------------");
                    }
                    None => (),
                }
            });
        }

        // If the next value in the subscription is None, then make a new subscription stream
        subscription = web3s
            .eth_subscribe()
            .subscribe_new_pending_transactions()
            .await?;
        println!("New subscription made!");
    }

    Ok(())
}

And I tried out multiple free tier node service and basically all doing the same error. Not sure because of the heavy stream not allowed in free tier or because of the web3 crate I'm experiencing this problem. But because in issues #642 had similar problem I think it's might web3 crate connected error.

alexmikhalevich commented 1 year ago

Any updates on the issue? I have the same problem