Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Marko/sdk refactor #690

Merged
merged 31 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
88e9884
Added multisig, account abstraction, block abstraction
markopoloparadox Nov 4, 2024
5bf00b8
Added mdBook, expanded Block interfaces.
markopoloparadox Nov 4, 2024
f5a244b
Added AppId, Nonce, Blocks and Hex String examples to cookbook
markopoloparadox Nov 4, 2024
5967e3d
Added setup and data_submit section to cookbook
markopoloparadox Nov 4, 2024
88df4ab
Cleanup
markopoloparadox Nov 4, 2024
9f31a0e
Added RPCs
markopoloparadox Nov 7, 2024
814cb6d
Refactoring avail-rust sdk
markopoloparadox Nov 8, 2024
dae4b60
expanded block interface
markopoloparadox Nov 9, 2024
d2c76f2
test
markopoloparadox Nov 12, 2024
97ea329
Made it compile
markopoloparadox Nov 13, 2024
55da578
changes
markopoloparadox Nov 16, 2024
84e31a7
Moved extrinsic examples around for availrust
markopoloparadox Nov 25, 2024
b0a20b4
another refactor of avail-rust
markopoloparadox Nov 25, 2024
46ffaef
refactor
markopoloparadox Nov 25, 2024
e82424a
Updated some of avail-rust docs
markopoloparadox Nov 26, 2024
2f23cef
Added more examples to avail-rust
markopoloparadox Nov 26, 2024
81a0e51
Refacotred avail-js
markopoloparadox Nov 26, 2024
2bc629d
Updated avail-js extrinsic docs
markopoloparadox Nov 26, 2024
7926ab7
Changes in avail-js examples
markopoloparadox Nov 27, 2024
07592ad
Added more avail-js doc examples
markopoloparadox Nov 28, 2024
dbe1a07
Updated avail-js book docs
markopoloparadox Nov 28, 2024
b8f5ca3
Updated Account abstraction for avail-rust
markopoloparadox Nov 28, 2024
caefe21
added qol changes to avail-rust
markopoloparadox Nov 28, 2024
b49cbf5
qol changes and lint for avail-js
markopoloparadox Nov 28, 2024
ffc6619
qol avail-rust
markopoloparadox Nov 29, 2024
acad145
qol avail-rust
markopoloparadox Nov 29, 2024
ed91b17
changes
markopoloparadox Nov 29, 2024
bbc6374
Added tx submission retry mechanism and logging to avail-rust
markopoloparadox Nov 30, 2024
6574a27
qol
markopoloparadox Dec 2, 2024
5c6ab41
Enabled RPC recconection
markopoloparadox Dec 2, 2024
fca7d07
fixed nonce issue
markopoloparadox Dec 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
qol avail-rust
  • Loading branch information
markopoloparadox committed Nov 29, 2024
commit ffc66196823a699b1c41c748f7e6dcbc12df09fc
2 changes: 1 addition & 1 deletion avail-rust/docs/book/src/examples/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use avail_rust::{avail, block::Block, error::ClientError, utils, Options, SDK};
use avail_rust::{avail, error::ClientError, utils, Block, Options, SDK};

pub async fn run() -> Result<(), ClientError> {
let sdk = SDK::new(SDK::local_endpoint()).await?;
Expand Down
16 changes: 8 additions & 8 deletions avail-rust/docs/book/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use avail_rust::{
error::ClientError,
rpcs,
utils::account_id_from_str,
AccountId, SDK,
AccountId, Block, SDK,
};

pub async fn da_app_keys() -> Result<(), ClientError> {
Expand All @@ -14,7 +14,7 @@ pub async fn da_app_keys() -> Result<(), ClientError> {
let key = BoundedVec(key.as_bytes().to_vec());
let storage_query = avail::storage().data_availability().app_keys(key);

let best_block_hash = rpcs::get_best_block_hash(rpc_client).await?;
let best_block_hash = Block::fetch_best_block_hash(rpc_client).await?;
let storage = online_client.storage().at(best_block_hash);
let result = storage.fetch(&storage_query).await?;

Expand All @@ -29,7 +29,7 @@ pub async fn da_app_keys_iter() -> Result<(), ClientError> {

let storage_query = avail::storage().data_availability().app_keys_iter();

let best_block_hash = rpcs::get_best_block_hash(rpc_client).await?;
let best_block_hash = Block::fetch_best_block_hash(rpc_client).await?;
let storage = online_client.storage().at(best_block_hash);
let mut results = storage.iter(storage_query).await?;

Expand All @@ -50,7 +50,7 @@ pub async fn da_next_app_id() -> Result<(), ClientError> {

let storage_query = avail::storage().data_availability().next_app_id();

let best_block_hash = rpcs::get_best_block_hash(rpc_client).await?;
let best_block_hash = Block::fetch_best_block_hash(rpc_client).await?;
let storage = online_client.storage().at(best_block_hash);
let result = storage.fetch_or_default(&storage_query).await?;

Expand All @@ -64,7 +64,7 @@ pub async fn staking_active_era() -> Result<(), ClientError> {
let (online_client, rpc_client) = (&sdk.online_client, &sdk.rpc_client);

let storage_query = avail::storage().staking().active_era();
let best_block_hash = rpcs::get_best_block_hash(rpc_client).await?;
let best_block_hash = Block::fetch_best_block_hash(rpc_client).await?;
let storage = online_client.storage().at(best_block_hash);
let result = storage.fetch(&storage_query).await?;

Expand All @@ -80,7 +80,7 @@ pub async fn staking_bonded() -> Result<(), ClientError> {
let account_id = account_id_from_str("5GNJqTPyNqANBkUVMN1LPPrxXnFouWXoe2wNSmmEoLctxiZY")?; // Alice_Stash

let storage_query = avail::storage().staking().bonded(account_id);
let best_block_hash = rpcs::get_best_block_hash(rpc_client).await?;
let best_block_hash = Block::fetch_best_block_hash(rpc_client).await?;
let storage = online_client.storage().at(best_block_hash);
let result = storage.fetch(&storage_query).await?;

Expand All @@ -94,7 +94,7 @@ pub async fn staking_bonded_iter() -> Result<(), ClientError> {
let (online_client, rpc_client) = (&sdk.online_client, &sdk.rpc_client);

let storage_query = avail::storage().staking().bonded_iter();
let best_block_hash = rpcs::get_best_block_hash(rpc_client).await?;
let best_block_hash = Block::fetch_best_block_hash(rpc_client).await?;
let storage = online_client.storage().at(best_block_hash);
let mut results = storage.iter(storage_query).await?;

Expand All @@ -114,7 +114,7 @@ pub async fn system_account_iter() -> Result<(), ClientError> {
let (online_client, rpc_client) = (&sdk.online_client, &sdk.rpc_client);

let storage_query = avail::storage().system().account_iter();
let best_block_hash = rpcs::get_best_block_hash(rpc_client).await?;
let best_block_hash = Block::fetch_best_block_hash(rpc_client).await?;
let storage = online_client.storage().at(best_block_hash);
let mut results = storage.iter(storage_query).await?;

Expand Down
7 changes: 3 additions & 4 deletions avail-rust/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::str::FromStr;

use subxt::{backend::rpc::RpcClient, blocks::StaticExtrinsic, ext::scale_encode::EncodeAsFields};

use crate::{
avail,
error::ClientError,
rpcs::get_block_hash,
transactions::{Transaction, TransactionDetails, TransactionFailed},
transactions::{Transaction, TransactionDetails},
utils, AOnlineClient, AccountId, Keypair, Options, SecretUri, WaitFor, SDK,
};

Expand Down Expand Up @@ -80,11 +79,11 @@ impl Account {
tx: Transaction<T>,
wait_for: WaitFor,
options: Option<Options>,
) -> Result<TransactionDetails, TransactionFailed>
) -> Result<TransactionDetails, ClientError>
where
T: StaticExtrinsic + EncodeAsFields,
{
tx.execute(wait_for, &self.keyring, options).await
tx.execute(wait_for, &self.keyring, options, Some(6)).await
}

pub async fn get_nonce_state(
Expand Down
20 changes: 14 additions & 6 deletions avail-rust/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::ClientError;
use crate::rpcs::{get_best_block_hash, get_block_hash, get_finalized_head};
use crate::rpcs;
use crate::{
avail::data_availability::calls::types as DataAvailabilityCalls,
primitives::block::extrinsics_params::CheckAppId,
Expand Down Expand Up @@ -33,16 +33,16 @@ impl 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
let block_hash = Self::fetch_best_block_hash(rpc_client).await?;
Self::new(online_client, block_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
let block_hash = Self::fetch_finalized_block_hash(rpc_client).await?;
Self::new(online_client, block_hash).await
}

pub async fn from_block(block: ABlock) -> Result<Self, subxt::Error> {
Expand All @@ -58,7 +58,7 @@ impl Block {
rpc_client: &RpcClient,
block_number: u32,
) -> Result<Self, subxt::Error> {
let block_hash = get_block_hash(rpc_client, Some(block_number)).await?;
let block_hash = rpcs::get_block_hash(rpc_client, Some(block_number)).await?;
Self::new(online_client, block_hash).await
}

Expand Down Expand Up @@ -175,6 +175,14 @@ impl Block {
{
self.block.storage().iter(address).await
}

pub async fn fetch_best_block_hash(client: &RpcClient) -> Result<H256, subxt::Error> {
rpcs::get_block_hash(client, None).await
}

pub async fn fetch_finalized_block_hash(client: &RpcClient) -> Result<H256, subxt::Error> {
rpcs::get_finalized_head(client).await
}
}

pub async fn fetch_transactions(
Expand Down
1 change: 1 addition & 0 deletions avail-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub use sdk::{WaitFor, SDK};

pub use crate::avail::runtime_types::sp_arithmetic::per_things::Perbill;
pub use avail_core;
pub use block::Block;
pub use hex;
pub use kate_recovery;
pub use primitives::block::{
Expand Down
4 changes: 0 additions & 4 deletions avail-rust/src/rpcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,6 @@ pub async fn get_block_hash(
Ok(value)
}

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

pub async fn get_finalized_head(client: &RpcClient) -> Result<BlockHash, subxt::Error> {
let value: BlockHash = client
.request("chain_getFinalizedHead", rpc_params![])
Expand Down
28 changes: 18 additions & 10 deletions avail-rust/src/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
from_substrate::FeeDetails,
rpcs::query_fee_details,
utils::{self, *},
AExtrinsicEvents, AOnlineClient, ATxInBlock, AvailConfig, WaitFor, H256,
AExtrinsicEvents, AOnlineClient, AvailConfig, WaitFor, H256,
};

use options::parse_options;
Expand Down Expand Up @@ -68,7 +68,6 @@ impl Transactions {

#[derive(Debug, Clone)]
pub struct TransactionDetails {
pub tx_in_block: Arc<ATxInBlock>,
pub events: Arc<AExtrinsicEvents>,
pub tx_hash: H256,
pub tx_index: u32,
Expand All @@ -78,15 +77,13 @@ pub struct TransactionDetails {

impl TransactionDetails {
pub fn new(
tx_in_block: ATxInBlock,
events: AExtrinsicEvents,
tx_hash: H256,
tx_index: u32,
block_hash: H256,
block_number: u32,
) -> Self {
Self {
tx_in_block: tx_in_block.into(),
events: events.into(),
tx_hash,
tx_index,
Expand Down Expand Up @@ -269,17 +266,17 @@ where
&self,
account: &Keypair,
options: Option<Options>,
) -> Result<TransactionDetails, TransactionFailed> {
self.execute(WaitFor::BlockInclusion, account, options)
) -> Result<TransactionDetails, ClientError> {
self.execute(WaitFor::BlockInclusion, account, options, Some(3))
.await
}

pub async fn execute_wait_for_finalization(
&self,
account: &Keypair,
options: Option<Options>,
) -> Result<TransactionDetails, TransactionFailed> {
self.execute(WaitFor::BlockFinalization, account, options)
) -> Result<TransactionDetails, ClientError> {
self.execute(WaitFor::BlockFinalization, account, options, Some(6))
.await
}

Expand All @@ -288,14 +285,16 @@ where
wait_for: WaitFor,
account: &Keypair,
options: Option<Options>,
) -> Result<TransactionDetails, TransactionFailed> {
progress_and_parse_transaction(
block_timeout: Option<u32>,
) -> Result<TransactionDetails, ClientError> {
execute_and_watch_transaction(
&self.online_client,
&self.rpc_client,
account,
&self.payload,
wait_for,
options,
block_timeout,
)
.await
}
Expand All @@ -315,6 +314,15 @@ where
.await
}

pub async fn watch_transaction(
online_client: &AOnlineClient,
tx_hash: H256,
wait_for: WaitFor,
block_timeout: Option<u32>,
) -> Result<TransactionDetails, ClientError> {
watch_transaction(online_client, tx_hash, wait_for, block_timeout).await
}

pub async fn payment_query_info(
&self,
account: &Keypair,
Expand Down
5 changes: 3 additions & 2 deletions avail-rust/src/transactions/options.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use subxt::backend::rpc::RpcClient;

use crate::error::ClientError;
use crate::rpcs::{account_next_index, get_block_hash, get_finalized_head, get_header};
use crate::rpcs::{account_next_index, get_block_hash, get_header};
use crate::Block;
use crate::{AOnlineClient, AccountId, AvailExtrinsicParamsBuilder, BlockHash};

use super::Params;
Expand Down Expand Up @@ -110,7 +111,7 @@ pub async fn parse_options(
builder.nonce(nonce)
},
Nonce::FinalizedBlock => {
let hash = get_finalized_head(rpc_client).await?;
let hash = Block::fetch_finalized_block_hash(rpc_client).await?;
let block = online_client.blocks().at(hash).await?;
let nonce = block.account_nonce(&account).await?;
builder.nonce(nonce)
Expand Down
Loading