Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
markopoloparadox committed Nov 16, 2024
1 parent 97ea329 commit 55da578
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 119 deletions.
72 changes: 0 additions & 72 deletions avail-rust/docs/utils/README.md

This file was deleted.

9 changes: 0 additions & 9 deletions avail-rust/docs/utils/progress_transaction/Cargo.toml

This file was deleted.

33 changes: 0 additions & 33 deletions avail-rust/docs/utils/progress_transaction/src/main.rs

This file was deleted.

52 changes: 52 additions & 0 deletions avail-rust/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use crate::rpcs::{get_best_block_hash, get_finalized_head};
use crate::{
avail::data_availability::calls::types as DataAvailabilityCalls,
primitives::block::extrinsics_params::CheckAppId,
};
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,
Expand All @@ -21,6 +26,22 @@ impl Block {
})
}

pub async fn new_best_block(
online_client: &AOnlineClient,
rpc_client: &RpcClient,
) -> Result<Self, subxt::Error> {
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<Self, subxt::Error> {
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);
}
Expand Down Expand Up @@ -88,6 +109,37 @@ impl Block {
pub fn submit_data_by_app_id(&self, app_id: u32) -> Option<DataSubmission> {
submit_data_by_app_id(&self.transactions, app_id)
}

pub async fn storage_fetch<'address, T>(
&self,
address: &'address T,
) -> Result<Option<<T as subxt::storage::Address>::Target>, subxt::Error>
where
T: subxt::storage::Address<IsFetchable = Yes> + 'address,
{
self.block.storage().fetch(address).await
}

pub async fn storage_fetch_or_default<'address, T>(
&self,
address: &'address T,
) -> Result<<T as subxt::storage::Address>::Target, subxt::Error>
where
T: subxt::storage::Address<IsFetchable = Yes, IsDefaultable = Yes> + 'address,
{
self.block.storage().fetch_or_default(address).await
}

pub async fn storage_iter<T>(
&self,
address: T,
) -> Result<StreamOfResults<StorageKeyValuePair<T>>, subxt::Error>
where
T: subxt::storage::Address<IsIterable = Yes> + 'static,
T::Keys: 'static + Sized,
{
self.block.storage().iter(address).await
}
}

pub async fn transactions(
Expand Down
18 changes: 18 additions & 0 deletions avail-rust/src/examples.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions avail-rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod api_dev;
mod config;
mod examples;
mod from_substrate;
mod rpcs;
mod sdk;
Expand Down
7 changes: 4 additions & 3 deletions avail-rust/src/rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -260,7 +261,7 @@ pub async fn get_block(
Ok(value)
}

pub async fn get_latest_block(client: &RpcClient) -> Result<ABlockDetailsRPC, subxt::Error> {
pub async fn get_best_block(client: &RpcClient) -> Result<ABlockDetailsRPC, subxt::Error> {
get_block(client, None).await
}

Expand All @@ -279,7 +280,7 @@ pub async fn get_block_hash(
Ok(value)
}

pub async fn get_latest_block_hash(client: &RpcClient) -> Result<BlockHash, subxt::Error> {
pub async fn get_best_block_hash(client: &RpcClient) -> Result<BlockHash, subxt::Error> {
get_block_hash(client, None).await
}

Expand Down
4 changes: 2 additions & 2 deletions avail-rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -209,7 +209,7 @@ pub async fn get_nonce_state(
address: &str,
) -> Result<u32, ClientError> {
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)
Expand Down

0 comments on commit 55da578

Please sign in to comment.