Skip to content

Commit 76ebc86

Browse files
committed
fix: address breaking changes from client.rs updates
1 parent fb2333f commit 76ebc86

File tree

4 files changed

+26
-41
lines changed

4 files changed

+26
-41
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl Client {
164164

165165
let chain_api = ChainClient::from_backend(Arc::new(backend)).await?;
166166

167-
log::info!(target: LOG_TARGET, "Connected to {uri} with ChainHead backend");
167+
log::info!(target: LOG_TARGET, "Connected to {uri} with Legacy backend");
168168

169169
Ok::<ChainClient, Error>(chain_api)
170170
};
@@ -256,8 +256,8 @@ impl Client {
256256
self.chain_api.read().await
257257
}
258258

259-
/// Get the RPC used to connect to the chain.
260-
pub fn rpc(&self) -> &ReconnectingRpcClient {
261-
&self.rpc
259+
/// Get the currently connected endpoint
260+
pub async fn current_endpoint(&self) -> String {
261+
self.endpoints[self.current_endpoint_index.load(Ordering::Relaxed)].clone()
262262
}
263263
}

src/commands/predict.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ where
3333
T::MaxVotesPerVoter: Send,
3434
{
3535
// Update metadata constants
36-
update_metadata_constants(client.chain_api())?;
36+
update_metadata_constants(&*client.chain_api().await)?;
3737
crate::dynamic::set_balancing_iterations(config.balancing_iterations);
3838
crate::dynamic::set_algorithm(config.algorithm);
3939

@@ -45,6 +45,7 @@ where
4545
} else {
4646
client
4747
.chain_api()
48+
.await
4849
.blocks()
4950
.at_latest()
5051
.await
@@ -59,9 +60,9 @@ where
5960
// Get block hash from block number
6061
let block_hash = get_block_hash(&client, block_num).await?;
6162

62-
crate::utils::storage_at(Some(block_hash), client.chain_api()).await?
63+
crate::utils::storage_at(Some(block_hash), &*client.chain_api().await).await?
6364
} else {
64-
client.chain_api().storage().at_latest().await?
65+
client.chain_api().await.storage().at_latest().await?
6566
};
6667

6768
let current_round = storage

src/utils.rs

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ struct ChainProperties {
168168

169169
/// Get the SS58 prefix from the chain
170170
pub async fn get_ss58_prefix(client: &Client) -> Result<u16, Error> {
171-
match crate::dynamic::pallet_api::system::constants::SS58_PREFIX.fetch(client.chain_api()) {
171+
match crate::dynamic::pallet_api::system::constants::SS58_PREFIX
172+
.fetch(&*client.chain_api().await)
173+
{
172174
Ok(ss58_prefix) => Ok(ss58_prefix),
173175
Err(e) => {
174176
log::warn!(target: LOG_TARGET, "Failed to fetch SS58 prefix: {e}");
@@ -180,56 +182,38 @@ pub async fn get_ss58_prefix(client: &Client) -> Result<u16, Error> {
180182

181183
/// Get block hash from block number using RPC
182184
pub async fn get_block_hash(client: &Client, block_number: u32) -> Result<Hash, Error> {
183-
use serde_json::{json, value::to_raw_value};
185+
use subxt_rpcs::{RpcClient, client::RpcParams};
184186

185-
// Convert block number to JSON raw value
186-
let params = to_raw_value(&json!([block_number]))
187+
let mut params = RpcParams::new();
188+
params
189+
.push(block_number)
187190
.map_err(|e| Error::Other(format!("Failed to serialize block number: {e}")))?;
188191

189-
// Make RPC request - params is already Box<RawValue>
190-
let response = client
191-
.rpc()
192-
.request("chain_getBlockHash".to_string(), Some(params))
193-
.await
194-
.map_err(|e| {
192+
let endpoint = client.current_endpoint().await;
193+
let rpc_client = RpcClient::from_url(endpoint).await?;
194+
195+
let block_hash: Option<Hash> =
196+
rpc_client.request("chain_getBlockHash", params).await.map_err(|e| {
195197
Error::Other(format!("Failed to get block hash for block {block_number}: {e}"))
196198
})?;
197199

198-
// Parse response - it can be null if block doesn't exist
199-
let response_str = response.get();
200-
if response_str == "null" {
201-
return Err(Error::Other(format!(
202-
"Block {block_number} not found (may be pruned or invalid)"
203-
)));
204-
}
205-
206-
// Deserialize the hash
207-
let block_hash: Option<Hash> = serde_json::from_str(response_str)
208-
.map_err(|e| Error::Other(format!("Failed to parse block hash response: {e}")))?;
209-
210200
block_hash.ok_or_else(|| {
211201
Error::Other(format!("Block {block_number} not found (may be pruned or invalid)"))
212202
})
213203
}
214204

215205
/// Get chain properties (ss58 prefix, token decimals and symbol) from system_properties RPC
216206
pub async fn get_chain_properties(client: Client) -> Result<(u16, u8, String), Error> {
217-
use serde_json::{json, value::to_raw_value};
207+
use subxt_rpcs::{RpcClient, client::RpcParams};
218208

219-
// Call system_properties RPC method using the client's RPC
220-
let params = to_raw_value(&json!([]))
221-
.map_err(|e| Error::Other(format!("Failed to serialize RPC params: {e}")))?;
209+
let endpoint = client.current_endpoint().await;
210+
let rpc_client = RpcClient::from_url(endpoint).await?;
222211

223-
let response_raw = client
224-
.rpc()
225-
.request("system_properties".to_string(), Some(params))
212+
let response: ChainProperties = rpc_client
213+
.request("system_properties", RpcParams::new())
226214
.await
227215
.map_err(|e| Error::Other(format!("Failed to call system_properties RPC: {e}")))?;
228216

229-
// Deserialize the response
230-
let response: ChainProperties = serde_json::from_str(response_raw.get())
231-
.map_err(|e| Error::Other(format!("Failed to parse system_properties response: {e}")))?;
232-
233217
// Extract token decimals
234218
let decimals = response.token_decimals.unwrap_or(10); // Default to 10 for most Substrate chains
235219

0 commit comments

Comments
 (0)