-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add state_call to rpc api fix clippy add call_raw and automatic decoding add missing await use vec instead of option readd Option once again add some runtime api calls and first test add authority test fix build fix no-std build fix build fix result return values remove unnecessary result return add finalize block add core runtime api fix build add RutimeApiClient for clear distinguishion add transaction, staking , mmr and session_keys api fix build fix build fix clippy fix naming of session keys function add mmr tests add session key tests fix no-std error by defining types by self for now add sakintapi test and fix fix build fix tets update tests add runtime api example update README add example of self creation of call add metadata decoding add list functions add some nice printing fix build remove mmr fix async build update jsonrspee to v21 (#707) * rename authority_discovery to authorities * use Bytes instead of Vec<u8> * fix clippy
- Loading branch information
Showing
20 changed files
with
1,422 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
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::AccountKeyring; | ||
use substrate_api_client::{ | ||
ac_primitives::AssetRuntimeConfig, | ||
extrinsic::BalancesExtrinsics, | ||
rpc::JsonrpseeClient, | ||
runtime_api::{AuthorityDiscoveryApi, CoreApi, MetadataApi, RuntimeApi, TransactionPaymentApi}, | ||
Api, GetChainInfo, | ||
}; | ||
|
||
// To test this example with CI we run it against the Substrate kitchensink node, which uses the asset pallet. | ||
// Therefore, we need to use the `AssetRuntimeConfig` in this example. | ||
// ! However, most Substrate runtimes do not use the asset pallet at all. So if you run an example against your own node | ||
// you most likely should use `DefaultRuntimeConfig` instead. | ||
|
||
#[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::<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).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.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]); | ||
|
||
// 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
use super::{RuntimeApi, RuntimeApiClient}; | ||
use crate::{api::Result, rpc::Request}; | ||
use ac_primitives::config::Config; | ||
#[cfg(not(feature = "sync-api"))] | ||
use alloc::boxed::Box; | ||
use alloc::vec; | ||
use sp_core::Encode; | ||
|
||
#[maybe_async::maybe_async(?Send)] | ||
pub trait AccountNonceApi: RuntimeApi { | ||
type Index; | ||
type AccountId; | ||
|
||
/// The API to query account nonce (aka transaction index). | ||
async fn account_nonce( | ||
&self, | ||
account_id: Self::AccountId, | ||
at_block: Option<Self::Hash>, | ||
) -> Result<Self::Index>; | ||
} | ||
|
||
#[maybe_async::maybe_async(?Send)] | ||
impl<T, Client> AccountNonceApi for RuntimeApiClient<T, Client> | ||
where | ||
T: Config, | ||
Client: Request, | ||
{ | ||
type Index = T::Index; | ||
type AccountId = T::AccountId; | ||
|
||
async fn account_nonce( | ||
&self, | ||
account_id: Self::AccountId, | ||
at_block: Option<Self::Hash>, | ||
) -> Result<Self::Index> { | ||
self.runtime_call("AccountNonceApi_account_nonce", vec![account_id.encode()], at_block) | ||
.await | ||
} | ||
} |
Oops, something went wrong.