Transaction - 2

With everything in place, we can slowly replace and tidy up our code.

Connection

The first change that we have does was to simplify the creation of online and rpc client. Instead of manually creating them there is an convenient helper function that will set it up for us.

	use avail_rust::sdk::reconnecting_api;

	let endpoint = "ws://127.0.0.1:9944";
	let (online_client, rpc_client) = reconnecting_api(endpoint).await?;

The reconnecting_api create an rpc with the following parameters:

ReconnectingRpcClient::builder().retry_policy(ExponentialBackoff::from_millis(1000).max_delay(Duration::from_secs(3)).take(3))

Accounts

There are already premade accounts available in the SDK interface. There is one as well for Bob, Eve, and Charlie.

	use avail_rust::SDK;

	let account = SDK::alice()?;

Payload

Manually passing the pallet name, call name and call data is error prone and that's why there is an better way.
All the payloads are defined in the following path avail_rust::avail::tx().*().**(data) where * is the pallet name and ** is the call type.
For more examples go to the next page.

	use avail_rust::avail::runtime_types::bounded_collections::bounded_vec::BoundedVec;

	let data = String::from("My Data").into_bytes();
	let data = BoundedVec(data);
	let payload = avail_rust::avail::tx()
		.data_availability()
		.submit_data(data);

Transaction Parameters, Signature, Submission

Transaction parameters, signature, and submission can be combined all into one single call.
Because we are using the default transaction parameters, we are passing None as the argument. If we wish to alter the parameters, we would pass an optional Options object.

	use avail_rust::transaction::utils::sign_and_send;

	let tx_hash = sign_and_send(&online_client, &rpc_client, &account, &payload, None).await?;

Watcher

Just like the rest, the watching part can be abstracted as well.
Finding if a transaction was successful or not is now just a matter of calling is_successful(). If the transaction failed, it will return an error with the description on why it failed.
The last arguments, Some(3), tells the watcher to read the next 4 (this is not a typo, it's X + 1) blocks and if none of the contains the target transaction hash it will return an error.

	use avail_rust::{transaction::utils::watch, WaitFor};

	let tx_details = watch(&online_client, tx_hash, WaitFor::BlockInclusion, Some(3)).await?;
	println!("Transaction was found.");
	println!("Block Hash: {:?}", tx_details.block_hash); // Block Hash: 0x61415b6012005665bac0cf8575a94e509d079a762be2ba6a71a04633efd01c1b
	println!("Block Number: {:?}", tx_details.block_number); // Block Number: 200
	println!("Tx Hash: {:?}", tx_details.tx_hash); // Tx Hash: 0x01651a93d55bde0f258504498d4f2164416df5331794d9c905d4c8711d9537ef
	println!("Tx Index: {:?}", tx_details.tx_index); // Tx Index: 1

	println!("Event count: {}", tx_details.events.iter().count()); // Event count: 7
	match tx_details.is_successful(&online_client) {
		Some(x) => x?,
		None => panic!("Failed to decode events."),
	};

Source Code

use avail_rust::error::ClientError;

#[tokio::main]
async fn main() -> Result<(), ClientError> {
	// RPC Connection
	// ANCHOR: connection
	use avail_rust::sdk::reconnecting_api;

	let endpoint = "ws://127.0.0.1:9944";
	let (online_client, rpc_client) = reconnecting_api(endpoint).await?;
	// ANCHOR_END: connection

	// Accounts
	// ANCHOR: accounts
	use avail_rust::SDK;

	let account = SDK::alice()?;
	// ANCHOR_END: accounts

	// Payload
	// ANCHOR: payload
	use avail_rust::avail::runtime_types::bounded_collections::bounded_vec::BoundedVec;

	let data = String::from("My Data").into_bytes();
	let data = BoundedVec(data);
	let payload = avail_rust::avail::tx()
		.data_availability()
		.submit_data(data);
	// ANCHOR_END: payload

	// Transaction Params, Signature, Submission
	// ANCHOR: signsend
	use avail_rust::transaction::utils::sign_and_send;

	let tx_hash = sign_and_send(&online_client, &rpc_client, &account, &payload, None).await?;
	// ANCHOR_END: signsend

	// Watcher
	// ANCHOR: watcher
	use avail_rust::{transaction::utils::watch, WaitFor};

	let tx_details = watch(&online_client, tx_hash, WaitFor::BlockInclusion, Some(3)).await?;
	println!("Transaction was found.");
	println!("Block Hash: {:?}", tx_details.block_hash); // Block Hash: 0x61415b6012005665bac0cf8575a94e509d079a762be2ba6a71a04633efd01c1b
	println!("Block Number: {:?}", tx_details.block_number); // Block Number: 200
	println!("Tx Hash: {:?}", tx_details.tx_hash); // Tx Hash: 0x01651a93d55bde0f258504498d4f2164416df5331794d9c905d4c8711d9537ef
	println!("Tx Index: {:?}", tx_details.tx_index); // Tx Index: 1

	println!("Event count: {}", tx_details.events.iter().count()); // Event count: 7
	match tx_details.is_successful(&online_client) {
		Some(x) => x?,
		None => panic!("Failed to decode events."),
	};
	// ANCHOR_END: watcher

	Ok(())
}