XXX: Include return type for decoding

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
Alexandru Vasile
2023-02-14 16:36:45 +02:00
parent 90bf22a1fb
commit a491858ddd
6 changed files with 20 additions and 87 deletions
+1 -45
View File
@@ -10,7 +10,6 @@ mod events;
mod runtime_api;
mod storage;
use scale_info::form::PortableForm;
use subxt_metadata::get_metadata_per_pallet_hash;
use self::runtime_api::generate_runtime_api;
@@ -32,10 +31,7 @@ use crate::{
};
use codec::Decode;
use frame_metadata::{
v15::{
RuntimeMetadataV15,
TraitMetadata,
},
v15::RuntimeMetadataV15,
RuntimeMetadata,
RuntimeMetadataPrefixed,
};
@@ -158,44 +154,6 @@ pub struct RuntimeGenerator {
metadata: RuntimeMetadataV15,
}
fn generate_runtime_call_api(
type_gen: &TypeGenerator,
runtime: &Vec<TraitMetadata<PortableForm>>,
) -> TokenStream2 {
let mut result = quote!();
for trait_ in runtime {
for method in &trait_.methods {
let trait_name = trait_.name.clone();
let method_name = method.name.clone();
let fn_name = format_ident!("{}_{}", trait_.name, method.name);
let mut params = Vec::new();
for input in &method.inputs {
let name = &input.name;
let ty = input.ty;
let ty = type_gen.resolve_type_path(ty.id());
let param = quote!( #name: #ty );
params.push(param);
}
let output = method.output;
let output = type_gen.resolve_type_path(output.id());
let fn_ = quote!(
pub fn #fn_name( #( #params, )* ) -> #output {
// call into RPC.
}
);
result.extend(fn_);
}
}
result
}
impl RuntimeGenerator {
/// Create a new runtime generator from the provided metadata.
///
@@ -233,8 +191,6 @@ impl RuntimeGenerator {
derives.clone(),
crate_path.clone(),
);
let runtime_api_fns =
generate_runtime_call_api(&type_gen, &self.metadata.runtime);
let types_mod = type_gen.generate_types_mod();
let types_mod_ident = types_mod.ident();
+1 -1
View File
@@ -53,7 +53,7 @@ fn generate_trait_api(
quote!(
#( #[doc = #docs ] )*
pub fn #method_name( #( #params, )* ) -> #crate_path::runtime_api::RuntimeAPIPayload {
pub fn #method_name( #( #params, )* ) -> #crate_path::runtime_api::RuntimeAPIPayload<#output> {
let mut #vec_acc = Vec::new();
#( #encoded; )*
-28
View File
@@ -40,26 +40,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let bytes = api.runtime_api().at(None).await?.call(api_tx).await?;
println!("Result: {:?}", bytes);
let result: polkadot::runtime_api::Core::version_target =
Decode::decode(&mut &bytes[..])?;
println!(
"Result for polkadot::runtime_api::Core::version(): {:?}\n\n",
result
);
let api_tx = polkadot::runtime_api::Metadata::metadata_versions();
println!("RuntimeApi payload: {:?}", api_tx);
let bytes = api.runtime_api().at(None).await?.call(api_tx).await?;
println!("Result: {:?}", bytes);
let result: polkadot::runtime_api::Metadata::metadata_versions_target =
Decode::decode(&mut &bytes[..])?;
println!(
"Result for polkadot::runtime_api::Metadata::metadata_versions(): {:?}\n\n",
result
);
let alice = AccountKeyring::Alice.to_account_id().into();
let api_tx = polkadot::runtime_api::AccountNonceApi::account_nonce(alice);
println!("RuntimeApi payload: {:?}", api_tx);
@@ -67,13 +53,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let bytes = api.runtime_api().at(None).await?.call(api_tx).await?;
println!("Result: {:?}", bytes);
let result: polkadot::runtime_api::AccountNonceApi::account_nonce_target =
Decode::decode(&mut &bytes[..])?;
println!(
"Result for polkadot::runtime_api::AccountNonceApi::account_nonce: {:?}\n\n",
result
);
// Send from Alice to Bob.
let signer = PairSigner::new(AccountKeyring::Alice.pair());
let dest = AccountKeyring::Bob.to_account_id().into();
@@ -95,12 +74,5 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let bytes = api.runtime_api().at(None).await?.call(api_tx).await?;
println!("Result: {:?}", bytes);
let result: polkadot::runtime_api::AccountNonceApi::account_nonce_target =
Decode::decode(&mut &bytes[..])?;
println!(
"Result for polkadot::runtime_api::AccountNonceApi::account_nonce: {:?}\n\n",
result
);
Ok(())
}
+1 -4
View File
@@ -25,10 +25,7 @@ use crate::{
Config,
Metadata,
};
use codec::{
Compact,
Decode,
};
use codec::Decode;
use derivative::Derivative;
use frame_metadata::{
OpaqueMetadata,
+9 -5
View File
@@ -2,15 +2,18 @@
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.
use std::marker::PhantomData;
/// Payload for a runtime API fn.
#[derive(Debug)]
pub struct RuntimeAPIPayload {
pub struct RuntimeAPIPayload<ReturnTy> {
func_name: &'static str,
data: Vec<u8>,
validation_hash: Option<[u8; 32]>,
_validation_hash: Option<[u8; 32]>,
_marker: PhantomData<ReturnTy>,
}
impl RuntimeAPIPayload {
impl<ReturnTy> RuntimeAPIPayload<ReturnTy> {
/// Create a new [`RuntimeAPIPayload`] from static data.
pub fn new(
func_name: &'static str,
@@ -20,14 +23,15 @@ impl RuntimeAPIPayload {
RuntimeAPIPayload {
func_name,
data,
validation_hash: Some(validation_hash),
_validation_hash: Some(validation_hash),
_marker: PhantomData,
}
}
/// Do not validate this prior to submitting it.
pub fn unvalidated(self) -> Self {
Self {
validation_hash: None,
_validation_hash: None,
..self
}
}
+8 -4
View File
@@ -7,6 +7,7 @@ use crate::{
error::Error,
Config,
};
use codec::Decode;
use derivative::Derivative;
use std::{
future::Future,
@@ -60,12 +61,13 @@ where
}
/// Execute a runtime API call for the given payload.
pub fn call(
pub fn call<ReturnTy: Decode>(
&self,
payload: RuntimeAPIPayload,
) -> impl Future<Output = Result<Vec<u8>, Error>> {
payload: RuntimeAPIPayload<ReturnTy>,
) -> impl Future<Output = Result<ReturnTy, Error>> {
let client = self.client.clone();
let block_hash = self.block_hash;
// Ensure that the returned future doesn't have a lifetime tied to api.runtime_api(),
// which is a temporary thing we'll be throwing away quickly:
async move {
@@ -77,7 +79,9 @@ where
.rpc()
.state_call(function, call_parameters, Some(block_hash))
.await?;
Ok(data.0)
let result: ReturnTy = Decode::decode(&mut &data.0[..])?;
Ok(result)
}
}
}