From acad1458c3df1438f889ff3fd5b8f9b98e49fb3b Mon Sep 17 00:00:00 2001 From: Marko Petrlic Date: Fri, 29 Nov 2024 09:33:03 +0100 Subject: [PATCH] qol avail-rust --- avail-rust/docs/book/src/test.rs | 32 ++++++++++++++++++++++++++++++++ avail-rust/src/error.rs | 2 ++ avail-rust/src/utils.rs | 12 +++++++++--- 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 avail-rust/docs/book/src/test.rs diff --git a/avail-rust/docs/book/src/test.rs b/avail-rust/docs/book/src/test.rs new file mode 100644 index 000000000..8aabd04b6 --- /dev/null +++ b/avail-rust/docs/book/src/test.rs @@ -0,0 +1,32 @@ +use avail_rust::{avail, error::ClientError, Nonce::BestBlockAndTxPool, Options, SDK}; + +type DataSubmissionCall = avail::data_availability::calls::types::SubmitData; + +pub async fn run() -> Result<(), ClientError> { + let sdk = SDK::new("wss://rpc-hex-devnet.avail.tools/ws").await?; + let online_client = &sdk.online_client; + + let account = SDK::charlie()?; + let data = String::from("My Data").into_bytes(); + let options = Some(Options::new().nonce(BestBlockAndTxPool).app_id(1)); + let tx = sdk.tx.data_availability.submit_data(data); + let mut si = 0; + let mut ei = 0; + + // Data Submission + loop { + println!("Preparing new Tx execution."); + let res = tx.execute_wait_for_inclusion(&account, options).await?; + if let Some(call_data) = res.get_data::(online_client).await { + si += 1 + } else { + println!("Something went wrong :O"); + ei += 1 + } + + println!( + "Tx Success. Block Number: {}. Number of success: {}, Number of Errors: {}", + res.block_number, si, ei + ); + } +} diff --git a/avail-rust/src/error.rs b/avail-rust/src/error.rs index 728b475b4..9ace4340d 100644 --- a/avail-rust/src/error.rs +++ b/avail-rust/src/error.rs @@ -7,6 +7,7 @@ use crate::transactions::TransactionFailed; #[derive(Debug)] pub enum ClientError { Custom(String), + BlockStream(String), Subxt(subxt::Error), SubxtSigner(SecretUriError), Sr25519(sr25519::Error), @@ -16,6 +17,7 @@ impl ClientError { pub fn to_string(&self) -> String { match self { ClientError::Custom(e) => e.clone(), + ClientError::BlockStream(e) => e.clone(), ClientError::Subxt(e) => e.to_string(), ClientError::SubxtSigner(e) => e.to_string(), ClientError::Sr25519(e) => e.to_string(), diff --git a/avail-rust/src/utils.rs b/avail-rust/src/utils.rs index 509c1ddbe..3f0b7fa14 100644 --- a/avail-rust/src/utils.rs +++ b/avail-rust/src/utils.rs @@ -88,8 +88,8 @@ pub async fn watch_transaction( wait_for: WaitFor, block_timeout: Option, ) -> Result { - let mut block_hash = tx_hash; - let mut block_number = 0u32; + let mut block_hash; + let mut block_number; let mut tx_details = None; let mut stream = if wait_for == WaitFor::BlockInclusion { @@ -101,7 +101,13 @@ pub async fn watch_transaction( let mut current_block_number: Option = None; let mut timeout_block_number: Option = None; - while let Some(block) = stream.next().await { + loop { + let Some(block) = stream.next().await else { + let error = + String::from("Failed to get next block from the stream. Client might be offline"); + return Err(ClientError::BlockStream(error)); + }; + let block = block?; block_hash = block.hash(); block_number = block.number();