Skip to content

Commit

Permalink
fix async build
Browse files Browse the repository at this point in the history
  • Loading branch information
haerdib committed Feb 5, 2024
1 parent 0c5ef3c commit c359ac4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ The following async examples can be found in the [async examples](/examples/asyn
* [get_blocks](/examples/async/examples/get_blocks.rs): Read header, block and signed block from storage.
* [get_storage](/examples/async/examples/get_storage.rs): Read storage values.
* [print_metadata](/examples/async/examples/print_metadata.rs): Print the metadata of the node in a readable way.
* [query_runtime_api](/src/examples/examples/query_runtime_api.rs): How to query the runtime api.
* [query_runtime_api](/src/examples/async/examples/query_runtime_api.rs): How to query the runtime api.
* [runtime_update_async](/examples/async/examples/runtime_update_async.rs): How to do an runtime upgrade asynchronously.
* [staking_batch_payout](/examples/async/examples/staking_batch_payout.rs): Batch reward payout for validator.
* [subscribe_events](/examples/async/examples/subscribe_events.rs): Subscribe and react on events.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,39 @@ async fn main() {
env_logger::init();

// Initialize the api, which retrieves the metadata from the node upon initialization.
let client = JsonrpseeClient::with_default_url().unwrap();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
let client = JsonrpseeClient::with_default_url().await.unwrap();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).await.unwrap();
let alice_pair = AccountKeyring::Alice.pair();
api.set_signer(alice_pair.into());
let runtime_api = api.runtime_api();

// Query the fee of an extrinsic.
let bob = AccountKeyring::Bob.to_account_id();
let balance_extrinsic = api.balance_transfer_allow_death(bob.clone().into(), 1000);
let extrinsic_fee_details =
runtime_api.query_fee_details(balance_extrinsic.clone(), 1000, None).unwrap();
let balance_extrinsic = api.balance_transfer_allow_death(bob.clone().into(), 1000).await;
let extrinsic_fee_details = runtime_api
.query_fee_details(balance_extrinsic.clone(), 1000, None)
.await
.unwrap();
let final_fee = extrinsic_fee_details.final_fee();
println!("To exceute the balance extrinsic, the following fee is required: {:?}", final_fee);

// Get the authority Ids.
let authority_ids: Vec<sr25519::Public> = runtime_api.authority_discovery(None).unwrap();
let authority_ids: Vec<sr25519::Public> = runtime_api.authority_discovery(None).await.unwrap();
println!("The following authorities are currently active:");
for authority in authority_ids {
println!("{:?}", authority);
}

// Query the runtime api version.
let version = runtime_api.version(None).unwrap();
let version = runtime_api.version(None).await.unwrap();
println!("{:?}", version);

// Query the available metadata versions.
let metadata_versions = runtime_api.metadata_versions(None).unwrap();
let metadata_versions = runtime_api.metadata_versions(None).await.unwrap();
assert_eq!(metadata_versions, [14, 15]);

// List all apis and functions thereof.
let trait_names = runtime_api.list_traits(None).unwrap();
let trait_names = runtime_api.list_traits(None).await.unwrap();
println!();
println!("Available traits:");
for name in trait_names {
Expand All @@ -75,7 +77,7 @@ async fn main() {
println!();

let trait_name = "BabeApi";
let method_names = runtime_api.list_methods_of_trait(trait_name, None).unwrap();
let method_names = runtime_api.list_methods_of_trait(trait_name, None).await.unwrap();
println!("Available methods of {trait_name}:");
for name in method_names {
println!("{name}");
Expand All @@ -84,12 +86,14 @@ async fn main() {

// Create your own runtime api call.
let parameters = vec![1000.encode()];
let latest_block_hash = api.get_block_hash(None).unwrap().unwrap();
let result: Result<u128, substrate_api_client::Error> = runtime_api.runtime_call(
"TransactionPaymentApi_query_length_to_fee",
parameters,
Some(latest_block_hash),
);
let latest_block_hash = api.get_block_hash(None).await.unwrap().unwrap();
let result: Result<u128, substrate_api_client::Error> = runtime_api
.runtime_call(
"TransactionPaymentApi_query_length_to_fee",
parameters,
Some(latest_block_hash),
)
.await;
let output = result.unwrap();
println!("Received the following output: {:?}", output);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use sp_core::{sr25519, Decode};
use sp_keyring::AccountKeyring;
use substrate_api_client::{
ac_node_api::Metadata,
ac_primitives::AssetRuntimeConfig,
extrinsic::BalancesExtrinsics,
rpc::JsonrpseeClient,
Expand All @@ -32,9 +31,9 @@ use substrate_api_client::{
#[tokio::main]
async fn main() {
// Setup
let client = JsonrpseeClient::with_default_url().unwrap();
let client = JsonrpseeClient::with_default_url().await.unwrap();
let alice_pair = AccountKeyring::Alice.pair();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).unwrap();
let mut api = Api::<AssetRuntimeConfig, _>::new(client).await.unwrap();
api.set_signer(alice_pair.into());

let runtime_api = api.runtime_api();
Expand All @@ -43,37 +42,37 @@ async fn main() {
let bob = AccountKeyring::Bob.to_account_id();

// General Runtime Api
let bytes = runtime_api.rpc_call("Metadata_metadata_versions", None, None).unwrap();
let bytes = runtime_api.rpc_call("Metadata_metadata_versions", None, None).await.unwrap();
let metadata_versions = Vec::<u32>::decode(&mut bytes.0.as_slice()).unwrap();
assert_eq!(metadata_versions, [14, 15]);

// AccountNonce
let alice_nonce = runtime_api.account_nonce(alice, None).unwrap();
assert_eq!(alice_nonce, api.get_nonce().unwrap());
let alice_nonce = runtime_api.account_nonce(alice, None).await.unwrap();
assert_eq!(alice_nonce, api.get_nonce().await.unwrap());

// Authority Discovery
let authority_id: Vec<sr25519::Public> = runtime_api.authority_discovery(None).unwrap();
let authority_id: Vec<sr25519::Public> = runtime_api.authority_discovery(None).await.unwrap();
assert!(authority_id.len() > 0);

// BlockBuilder
let extrinsic = api.balance_transfer_allow_death(bob.clone().into(), 1000);
runtime_api.apply_extrinsic(extrinsic, None).unwrap().unwrap().unwrap();
let block = api.get_block_by_num(Some(0)).unwrap().unwrap();
let check = runtime_api.check_inherents(block, Default::default(), None).unwrap();
let extrinsic = api.balance_transfer_allow_death(bob.clone().into(), 1000).await;
runtime_api.apply_extrinsic(extrinsic, None).await.unwrap().unwrap().unwrap();
let block = api.get_block_by_num(Some(0)).await.unwrap().unwrap();
let check = runtime_api.check_inherents(block, Default::default(), None).await.unwrap();
assert!(check.ok());
// This doesn't seem to work with the current substrate node. Tried it on polkadot.js as well, but it keeps on runtime panicking.
//let _bytes = runtime_api.inherent_extrinsics(Default::default(), None).unwrap();
//let _header = runtime_api.finalize_block(None).unwrap();

// Core
let _version = runtime_api.version(None).unwrap();
let _version = runtime_api.version(None).await.unwrap();

// Metadata
let _metadata = runtime_api.metadata(None).unwrap();
let metadata = runtime_api.metadata_at_version(15, None).unwrap().unwrap();
let _method_names = runtime_api.list_methods_of_trait("BabeApi", None).unwrap();
let _trait_names = runtime_api.list_traits(None).unwrap();
let metadata_versions = runtime_api.metadata_versions(None).unwrap();
let _metadata = runtime_api.metadata(None).await.unwrap();
let _metadata = runtime_api.metadata_at_version(15, None).await.unwrap().unwrap();
let _method_names = runtime_api.list_methods_of_trait("BabeApi", None).await.unwrap();
let _trait_names = runtime_api.list_traits(None).await.unwrap();
let metadata_versions = runtime_api.metadata_versions(None).await.unwrap();
assert_eq!(metadata_versions, [14, 15]);

// MMR
Expand All @@ -97,19 +96,21 @@ async fn main() {
// runtime_api.decode_session_keys(encoded_session_keys, None).unwrap().unwrap();

// Staking
let _quota = runtime_api.nominations_quota(100000000, None).unwrap();
let _quota = runtime_api.nominations_quota(100000000, None).await.unwrap();

// Transaction Payment
let extrinsic = api.balance_transfer_allow_death(bob.clone().into(), 1000);
let _tx_fee_details = runtime_api.query_fee_details(extrinsic.clone(), 1000, None).unwrap();
let _tx_info = runtime_api.query_info(extrinsic, 1000, None).unwrap();
let _fee = runtime_api.query_length_to_fee(1000, None).unwrap();
let _fee = runtime_api.query_weight_to_fee(1000.into(), None).unwrap();
let extrinsic = api.balance_transfer_allow_death(bob.clone().into(), 1000).await;
let _tx_fee_details =
runtime_api.query_fee_details(extrinsic.clone(), 1000, None).await.unwrap();
let _tx_info = runtime_api.query_info(extrinsic, 1000, None).await.unwrap();
let _fee = runtime_api.query_length_to_fee(1000, None).await.unwrap();
let _fee = runtime_api.query_weight_to_fee(1000.into(), None).await.unwrap();

// Transaction Payment Call
let call = api.balance_transfer_allow_death(bob.clone().into(), 1000).function;
let _tx_fee_details = runtime_api.query_call_fee_details(call.clone(), 1000, None).unwrap();
let _tx_info = runtime_api.query_call_info(call, 1000, None).unwrap();
let _fee = runtime_api.query_length_to_fee_call(1000, None).unwrap();
let _fee = runtime_api.query_weight_to_fee_call(1000.into(), None).unwrap();
let call = api.balance_transfer_allow_death(bob.clone().into(), 1000).await.function;
let _tx_fee_details =
runtime_api.query_call_fee_details(call.clone(), 1000, None).await.unwrap();
let _tx_info = runtime_api.query_call_info(call, 1000, None).await.unwrap();
let _fee = runtime_api.query_length_to_fee_call(1000, None).await.unwrap();
let _fee = runtime_api.query_weight_to_fee_call(1000.into(), None).await.unwrap();
}

0 comments on commit c359ac4

Please sign in to comment.