diff --git a/avail-rust/docs/utils/README.md b/avail-rust/docs/utils/README.md deleted file mode 100644 index 48aedf8dc..000000000 --- a/avail-rust/docs/utils/README.md +++ /dev/null @@ -1,72 +0,0 @@ -# Utils - -## Progress Transaction - -### Interface - -```rust -async fn progress_transaction(&self, maybe_tx_progress: Result, subxt::Error>, wait_for: WaitFor) -> Result; -``` - -#### Parameters - -| parameter | type | optional | description | -| ----------------- | -------------------------------------------------- | -------- | ---------------------------------------- | -| maybe_tx_progress | Result, subxt::Error> | false | transaction in progress | -| waitFor | WaitFor | false | wait for block inclusion or finalization | - -#### Return value - -On failure, a reason of failure is returned. On Success, it progresses and returns the transaction included in the block details. - -### Minimal Example - -#### Cargo.toml - -```rust -[package] -name = "progress-transaction" -edition = "2021" - -[dependencies] -avail-rust = { git = "https://github.com/availproject/avail" } -tokio = { version = "1.38.0", features = ["rt-multi-thread"] } -``` - -#### main.rs - -```rust -use avail_rust::{avail, subxt::utils::AccountId32, AccountId, Keypair, SecretUri, WaitFor, SDK}; -use core::str::FromStr; - -#[tokio::main] -async fn main() -> Result<(), String> { - let sdk = SDK::new("ws://127.0.0.1:9944").await.unwrap(); - - // Input - let secret_uri = SecretUri::from_str("//Alice").unwrap(); - let account = Keypair::from_uri(&secret_uri).unwrap(); - let dest: AccountId32 = AccountId::from_str("5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw") - .map_err(|e| e.to_string())?; - let amount = 1_000_000_000_000_000_000u128; // 1 Avail - - let tx_api = sdk.api.tx(); - let call = avail::tx() - .balances() - .transfer_keep_alive(dest.into(), amount); - - let maybe_tx_progress = tx_api - .sign_and_submit_then_watch_default(&call, &account) - .await; - - let tx_in_block = sdk - .util - .progress_transaction(maybe_tx_progress, WaitFor::BlockInclusion) - .await?; - - println!("BlockHash={:?}", tx_in_block.block_hash()); - println!("ExtrinsicHash={:?}", tx_in_block.extrinsic_hash()); - - Ok(()) -} -``` diff --git a/avail-rust/docs/utils/progress_transaction/Cargo.toml b/avail-rust/docs/utils/progress_transaction/Cargo.toml deleted file mode 100644 index 8c234070b..000000000 --- a/avail-rust/docs/utils/progress_transaction/Cargo.toml +++ /dev/null @@ -1,9 +0,0 @@ -[workspace] - -[package] -name = "progress-transaction" -edition = "2021" - -[dependencies] -avail-rust = { git = "https://github.com/availproject/avail" } -tokio = { version = "1.38.0", features = ["rt-multi-thread"] } diff --git a/avail-rust/docs/utils/progress_transaction/src/main.rs b/avail-rust/docs/utils/progress_transaction/src/main.rs deleted file mode 100644 index cda780dc1..000000000 --- a/avail-rust/docs/utils/progress_transaction/src/main.rs +++ /dev/null @@ -1,33 +0,0 @@ -use avail_rust::{avail, subxt::utils::AccountId32, AccountId, Keypair, SecretUri, WaitFor, SDK}; -use core::str::FromStr; - -#[tokio::main] -async fn main() -> Result<(), String> { - let sdk = SDK::new("ws://127.0.0.1:9944").await.unwrap(); - - // Input - let secret_uri = SecretUri::from_str("//Alice").unwrap(); - let account = Keypair::from_uri(&secret_uri).unwrap(); - let dest: AccountId32 = AccountId::from_str("5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw") - .map_err(|e| e.to_string())?; - let amount = 1_000_000_000_000_000_000u128; // 1 Avail - - let tx_api = sdk.api.tx(); - let call = avail::tx() - .balances() - .transfer_keep_alive(dest.into(), amount); - - let maybe_tx_progress = tx_api - .sign_and_submit_then_watch_default(&call, &account) - .await; - - let tx_in_block = sdk - .util - .progress_transaction(maybe_tx_progress, WaitFor::BlockInclusion) - .await?; - - println!("BlockHash={:?}", tx_in_block.block_hash()); - println!("ExtrinsicHash={:?}", tx_in_block.extrinsic_hash()); - - Ok(()) -} diff --git a/avail-rust/src/block.rs b/avail-rust/src/block.rs index fccc36e77..a7f45139a 100644 --- a/avail-rust/src/block.rs +++ b/avail-rust/src/block.rs @@ -1,3 +1,4 @@ +use crate::rpcs::{get_best_block_hash, get_finalized_head}; use crate::{ avail::data_availability::calls::types as DataAvailabilityCalls, primitives::block::extrinsics_params::CheckAppId, @@ -5,7 +6,11 @@ use crate::{ use crate::{ABlock, AExtrinsicDetails, AExtrinsics, AFoundExtrinsic, AOnlineClient}; use primitive_types::H256; +use subxt::backend::rpc::RpcClient; +use subxt::backend::StreamOfResults; use subxt::blocks::StaticExtrinsic; +use subxt::storage::StorageKeyValuePair; +use subxt::utils::Yes; pub struct Block { pub block: ABlock, @@ -21,6 +26,22 @@ impl Block { }) } + pub async fn new_best_block( + online_client: &AOnlineClient, + rpc_client: &RpcClient, + ) -> Result { + let best_hash = get_best_block_hash(rpc_client).await?; + Self::new(online_client, best_hash).await + } + + pub async fn new_finalized_block( + online_client: &AOnlineClient, + rpc_client: &RpcClient, + ) -> Result { + let best_hash = get_finalized_head(rpc_client).await?; + Self::new(online_client, best_hash).await + } + pub fn transaction_count(&self) -> usize { return transaction_count(&self.transactions); } @@ -88,6 +109,37 @@ impl Block { pub fn submit_data_by_app_id(&self, app_id: u32) -> Option { submit_data_by_app_id(&self.transactions, app_id) } + + pub async fn storage_fetch<'address, T>( + &self, + address: &'address T, + ) -> Result::Target>, subxt::Error> + where + T: subxt::storage::Address + 'address, + { + self.block.storage().fetch(address).await + } + + pub async fn storage_fetch_or_default<'address, T>( + &self, + address: &'address T, + ) -> Result<::Target, subxt::Error> + where + T: subxt::storage::Address + 'address, + { + self.block.storage().fetch_or_default(address).await + } + + pub async fn storage_iter( + &self, + address: T, + ) -> Result>, subxt::Error> + where + T: subxt::storage::Address + 'static, + T::Keys: 'static + Sized, + { + self.block.storage().iter(address).await + } } pub async fn transactions( diff --git a/avail-rust/src/examples.rs b/avail-rust/src/examples.rs new file mode 100644 index 000000000..4a3722496 --- /dev/null +++ b/avail-rust/src/examples.rs @@ -0,0 +1,18 @@ +#[cfg(test)] +mod tests { + use super::*; + use crate::{account::Account, avail, block::Block, rpcs::get_best_block}; + + #[tokio::test] + async fn testing_function() { + let sdk = crate::sdk::SDK::new("ws://127.0.0.1:9944").await.unwrap(); + + let block = Block::new_best_block(&sdk.online_client, &sdk.rpc_client) + .await + .unwrap(); + let address = avail::storage().data_availability().next_app_id(); + let storage_value = block.storage_fetch_or_default(&address).await.unwrap(); + + dbg!(storage_value); + } +} diff --git a/avail-rust/src/lib.rs b/avail-rust/src/lib.rs index 0391e4e26..db6b0c545 100644 --- a/avail-rust/src/lib.rs +++ b/avail-rust/src/lib.rs @@ -1,5 +1,6 @@ mod api_dev; mod config; +mod examples; mod from_substrate; mod rpcs; mod sdk; diff --git a/avail-rust/src/rpcs.rs b/avail-rust/src/rpcs.rs index 47cbda498..43fa7a078 100644 --- a/avail-rust/src/rpcs.rs +++ b/avail-rust/src/rpcs.rs @@ -83,6 +83,7 @@ impl Payment { #[cfg(test)] mod tests { use super::*; + use crate::utils; use crate::Keypair; use crate::SecretUri; use std::str::FromStr; @@ -95,7 +96,7 @@ mod tests { let sdk = crate::sdk::SDK::new("ws://127.0.0.1:9944").await.unwrap(); let keys = sdk.rpc.author.rotate_keys().await.unwrap(); - let keys = sdk.util.deconstruct_session_keys(keys).unwrap(); + let keys = utils::deconstruct_session_keys(keys).unwrap(); let b = sdk .tx .session @@ -260,7 +261,7 @@ pub async fn get_block( Ok(value) } -pub async fn get_latest_block(client: &RpcClient) -> Result { +pub async fn get_best_block(client: &RpcClient) -> Result { get_block(client, None).await } @@ -279,7 +280,7 @@ pub async fn get_block_hash( Ok(value) } -pub async fn get_latest_block_hash(client: &RpcClient) -> Result { +pub async fn get_best_block_hash(client: &RpcClient) -> Result { get_block_hash(client, None).await } diff --git a/avail-rust/src/utils.rs b/avail-rust/src/utils.rs index 6271a3602..95fe66301 100644 --- a/avail-rust/src/utils.rs +++ b/avail-rust/src/utils.rs @@ -11,7 +11,7 @@ use subxt_signer::sr25519::Keypair; use crate::{ avail::{self, runtime_types::da_runtime::primitives::SessionKeys}, error::ClientError, - rpcs::{account_next_index, get_block_hash, get_latest_block_hash}, + rpcs::{account_next_index, get_best_block_hash, get_block_hash}, transactions::{options::parse_options, TransactionDetails, TransactionFailed}, AExtrinsicEvents, AOnlineClient, ATxInBlock, ATxProgress, AccountId, AppUncheckedExtrinsic, Options, WaitFor, @@ -209,7 +209,7 @@ pub async fn get_nonce_state( address: &str, ) -> Result { let account = account_id_from_str(address)?; - let hash = get_latest_block_hash(rpc_client).await?; + let hash = get_best_block_hash(rpc_client).await?; let block = online_client.blocks().at(hash).await?; Ok(block.account_nonce(&account).await? as u32)