Repository navigation
Expand file tree
/
Copy pathquery_runtime_api.rs
More file actions
98 lines (84 loc) · 3.43 KB
/
Copy pathquery_runtime_api.rs
File metadata and controls
98 lines (84 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
Copyright 2024 Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//! Very simple example that shows how to query Runtime Api of a Substrate node.
use codec::Encode;
use sp_core::sr25519;
use sp_keyring::Sr25519Keyring;
use substrate_api_client::{
ac_primitives::RococoRuntimeConfig,
extrinsic::BalancesExtrinsics,
rpc::JsonrpseeClient,
runtime_api::{AuthorityDiscoveryApi, CoreApi, MetadataApi, RuntimeApi, TransactionPaymentApi},
Api, GetChainInfo,
};
// To test this example with CI we run it against the Polkadot Rococo node. Remember to switch the Config to match your
// own runtime if it uses different parameter configurations. Several pre-compiled runtimes are available in the ac-primitives crate.
#[tokio::main]
async fn main() {
env_logger::init();
// Initialize the api, which retrieves the metadata from the node upon initialization.
let client = JsonrpseeClient::with_default_url().await.unwrap();
let mut api = Api::<RococoRuntimeConfig, _>::new(client).await.unwrap();
let alice_pair = Sr25519Keyring::Alice.pair();
api.set_signer(alice_pair.into());
let runtime_api = api.runtime_api();
// Query the fee of an extrinsic.
let bob = Sr25519Keyring::Bob.to_account_id();
let balance_extrinsic =
api.balance_transfer_allow_death(bob.clone().into(), 1000).await.unwrap();
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.authorities(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).await.unwrap();
println!("{:?}", version);
// Query the available metadata versions.
let metadata_versions = runtime_api.metadata_versions(None).await.unwrap();
assert_eq!(metadata_versions, [14, 15, 16]);
// List all apis and functions thereof.
let trait_names = runtime_api.list_traits(None).await.unwrap();
println!();
println!("Available traits:");
for name in trait_names {
println!("{name}");
}
println!();
let trait_name = "BabeApi";
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}");
}
println!();
// Create your own runtime api call.
let parameters = vec![1000.encode()];
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);
}