Skip to content

Commit

Permalink
qol avail-rust
Browse files Browse the repository at this point in the history
  • Loading branch information
markopoloparadox committed Nov 29, 2024
1 parent ffc6619 commit acad145
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
32 changes: 32 additions & 0 deletions avail-rust/docs/book/src/test.rs
Original file line number Diff line number Diff line change
@@ -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::<DataSubmissionCall>(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
);
}
}
2 changes: 2 additions & 0 deletions avail-rust/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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(),
Expand Down
12 changes: 9 additions & 3 deletions avail-rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ pub async fn watch_transaction(
wait_for: WaitFor,
block_timeout: Option<u32>,
) -> Result<TransactionDetails, ClientError> {
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 {
Expand All @@ -101,7 +101,13 @@ pub async fn watch_transaction(
let mut current_block_number: Option<u32> = None;
let mut timeout_block_number: Option<u32> = 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();
Expand Down

0 comments on commit acad145

Please sign in to comment.