1

To query Polkadot node with subxt this is the code I used and that works fine.

use subxt::{OnlineClient, SubstrateConfig};

#[subxt::subxt(runtime_metadata_path = "metadata.scale")]
pub mod runtime {}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {

    let api = OnlineClient::<SubstrateConfig>::from_url("wss://rpc.polkadot.io:443").await?;

    let address = runtime::constants().balances().existential_deposit();
    let existential_deposit = api.constants().at(&address)?;
    println!("Existential Deposit: {}", existential_deposit);
    Ok(())
}

The metadata should be the same for common pallets and I used local Astar dev node to fetch metadata with:

subxt metadata -f bytes > metadata.scale

However, when I change rpc url to connect to Kusama, Shiden or Astar I get rpc timeout.

OnlineClient::<SubstrateConfig>::from_url("wss://kusama-rpc.polkadot.io:433").await?;

Error printout:

Error: Rpc(RpcError("Networking or low-level protocol error: Connection timeout exceeded: 10s"))

What is it that I'm doing wrong?

2 Answers 2

3

Ah, all right I see you are using the wrong port.

Change the URI to "wss://kusama-rpc.polkadot.io:443" should make it work.

1
  • OMG, I can' believe it... so much time wasted. Thanks a million!!
    – Maario
    Sep 29, 2022 at 14:49
1

I don't why the nodes you are connecting to are so slow but you could try to increase the connection timeout as follows (you need include jsonrpsee in your Cargo.toml as well):

use subxt::{OnlineClient, SubstrateConfig};
use jsonrpsee::ws_client::WsClientBuilder;
use std::time::Duration;

#[subxt::subxt(runtime_metadata_path = "metadata.scale")]
pub mod runtime {}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let rpc_client = WsClientBuilder::default()
        .connection_timeout(Duration::from_secs(60))
        .build("wss://kusama-rpc.polkadot.io:433")
        .await?;

    let api = SubxtClient::from_rpc_client(rpc_client).await?;

    let address = runtime::constants().balances().existential_deposit();
    let existential_deposit = api.constants().at(&address)?;
    println!("Existential Deposit: {}", existential_deposit);
    Ok(())
}

In addition make sure you compile the program in --release mode

1
  • thank for the idea to increase timer but it still does not work with Kusama wss. Your code works fine for me with Polkadot wss.
    – Maario
    Sep 29, 2022 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.