-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters