Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add missing files
Browse files Browse the repository at this point in the history
niklasad1 committed Nov 23, 2023

Unverified

The email in this signature doesn’t match the committer email.
1 parent 7d70fb0 commit 26ff320
Showing 2 changed files with 68 additions and 3 deletions.
67 changes: 67 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use crate::prelude::*;
use jsonrpsee::ws_client::WsClientBuilder;
use subxt::backend::rpc::RpcClient as RawRpcClient;

/// Wraps the subxt interfaces to make it easy to use for the staking-miner.
#[derive(Clone, Debug)]
pub struct Client {
/// Access to typed rpc calls from subxt.
rpc: RpcClient,
/// Access to chain APIs such as storage, events etc.
chain_api: ChainClient,
/// Raw RPC client.
raw_rpc: RawRpcClient,
}

impl Client {
pub async fn new(uri: &str) -> Result<Self, subxt::Error> {
log::debug!(target: LOG_TARGET, "attempting to connect to {:?}", uri);

let rpc = loop {
match WsClientBuilder::default()
.max_request_size(u32::MAX)
.max_response_size(u32::MAX)
.request_timeout(std::time::Duration::from_secs(600))
.build(&uri)
.await
{
Ok(rpc) => break RawRpcClient::new(rpc),
Err(e) => {
log::warn!(
target: LOG_TARGET,
"failed to connect to client due to {:?}, retrying soon..",
e,
);
},
};
tokio::time::sleep(std::time::Duration::from_millis(2_500)).await;
};

let chain_api = ChainClient::from_rpc_client(rpc.clone()).await?;

Ok(Self { rpc: RpcClient::new(rpc.clone()), raw_rpc: rpc, chain_api })
}

/// Get a reference to the RPC interface exposed by subxt.
pub fn rpc(&self) -> &RpcClient {
&self.rpc
}

/// Get a reference to the chain API.
pub fn chain_api(&self) -> &ChainClient {
&self.chain_api
}

// This is exposed until a new version of subxt is released.
pub async fn rpc_system_account_next_index<T>(
&self,
account_id: &T,
) -> Result<u64, subxt::Error>
where
T: serde::Serialize,
{
self.raw_rpc
.request("system_accountNextIndex", subxt::rpc_params![&account_id])
.await
}
}
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -123,9 +123,7 @@ async fn main() -> Result<(), Error> {
log::info!(target: LOG_TARGET, "Connected to chain: {}", chain);
epm::update_metadata_constants(client.chain_api()).await?;

SHARED_CLIENT
.set(client.clone())
.expect("shared client only set once; qed");
SHARED_CLIENT.set(client.clone()).expect("shared client only set once; qed");

// Start a new tokio task to perform the runtime updates in the background.
// if this fails then the miner will be stopped and has to be re-started.

0 comments on commit 26ff320

Please sign in to comment.