mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 23:51:05 +00:00
XXX: Dynamic return type under ValueThunk
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
This commit is contained in:
@@ -55,11 +55,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("Result: {:?}", bytes);
|
||||
|
||||
let alice = AccountKeyring::Alice.to_account_id();
|
||||
let api_tx = dynamic::<polkadot::runtime_api::AccountNonceApi::account_nonce_target>(
|
||||
// let api_tx = dynamic::<polkadot::runtime_api::AccountNonceApi::account_nonce_target>(
|
||||
let api_tx = dynamic(
|
||||
"AccountNonceApi_account_nonce",
|
||||
vec![Value::from_bytes(&alice)],
|
||||
);
|
||||
let bytes = api.runtime_api().at(None).await?.dyn_call(api_tx).await?;
|
||||
let bytes = api
|
||||
.runtime_api()
|
||||
.at(None)
|
||||
.await?
|
||||
.dyn_call(api_tx)
|
||||
.await?
|
||||
.to_value()?;
|
||||
println!("Result: {:?}", bytes);
|
||||
|
||||
// Send from Alice to Bob.
|
||||
|
||||
@@ -14,6 +14,8 @@ use codec::{
|
||||
use scale_value::Composite;
|
||||
|
||||
use crate::{
|
||||
dynamic::DecodedValueThunk,
|
||||
metadata::DecodeWithMetadata,
|
||||
Error,
|
||||
Metadata,
|
||||
};
|
||||
@@ -64,7 +66,7 @@ impl<ReturnTy> RuntimeAPIPayload<ReturnTy> {
|
||||
/// RuntimeApiPayload
|
||||
pub trait RuntimeApiPayload {
|
||||
/// The return type into which the result of the call is interpreted.
|
||||
type Target;
|
||||
type Target: DecodeWithMetadata;
|
||||
|
||||
// TODO: Could do with some lifetimes.
|
||||
/// The function name of the runtime API.
|
||||
@@ -90,32 +92,31 @@ pub struct StaticRuntimeApiPayload<ArgData, ReturnTy> {
|
||||
_marker: PhantomData<ReturnTy>,
|
||||
}
|
||||
|
||||
impl<ArgData, ReturnTy> RuntimeApiPayload for StaticRuntimeApiPayload<ArgData, ReturnTy>
|
||||
where
|
||||
ArgData: Encode,
|
||||
ReturnTy: Decode,
|
||||
{
|
||||
type Target = ReturnTy;
|
||||
// impl<ArgData, ReturnTy> RuntimeApiPayload for StaticRuntimeApiPayload<ArgData, ReturnTy>
|
||||
// where
|
||||
// ArgData: Encode,
|
||||
// ReturnTy: Decode,
|
||||
// {
|
||||
// type Target = ReturnTy;
|
||||
|
||||
fn func_name(&self) -> String {
|
||||
self.func_name.into()
|
||||
}
|
||||
// fn func_name(&self) -> String {
|
||||
// self.func_name.into()
|
||||
// }
|
||||
|
||||
fn encode_args_to(
|
||||
&self,
|
||||
_metadata: &Metadata,
|
||||
out: &mut Vec<u8>,
|
||||
) -> Result<(), Error> {
|
||||
self.data.encode_to(out);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
// fn encode_args_to(
|
||||
// &self,
|
||||
// _metadata: &Metadata,
|
||||
// out: &mut Vec<u8>,
|
||||
// ) -> Result<(), Error> {
|
||||
// self.data.encode_to(out);
|
||||
// Ok(())
|
||||
// }
|
||||
// }
|
||||
|
||||
/// DynamicRuntimeApiPayload
|
||||
pub struct DynamicRuntimeApiPayload<ReturnTy> {
|
||||
pub struct DynamicRuntimeApiPayload {
|
||||
func_name: &'static str,
|
||||
fields: Composite<()>,
|
||||
_marker: PhantomData<ReturnTy>,
|
||||
}
|
||||
|
||||
// impl<'a, ReturnTy> DynamicRuntimeApiPayload<'a, ReturnTy> {
|
||||
@@ -125,22 +126,18 @@ pub struct DynamicRuntimeApiPayload<ReturnTy> {
|
||||
// }
|
||||
|
||||
/// Construct a dynamic runtime API call.
|
||||
pub fn dynamic<ReturnTy>(
|
||||
pub fn dynamic(
|
||||
func_name: &'static str,
|
||||
fields: impl Into<Composite<()>>,
|
||||
) -> DynamicRuntimeApiPayload<ReturnTy> {
|
||||
) -> DynamicRuntimeApiPayload {
|
||||
DynamicRuntimeApiPayload {
|
||||
func_name: func_name.into(),
|
||||
fields: fields.into(),
|
||||
_marker: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
impl<ReturnTy> RuntimeApiPayload for DynamicRuntimeApiPayload<ReturnTy>
|
||||
where
|
||||
ReturnTy: Decode,
|
||||
{
|
||||
type Target = ReturnTy;
|
||||
impl RuntimeApiPayload for DynamicRuntimeApiPayload {
|
||||
type Target = DecodedValueThunk;
|
||||
|
||||
fn encode_args_to(
|
||||
&self,
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
use crate::{
|
||||
client::OnlineClientT,
|
||||
error::Error,
|
||||
metadata::DecodeWithMetadata,
|
||||
Config,
|
||||
};
|
||||
use codec::Decode;
|
||||
@@ -96,10 +97,9 @@ where
|
||||
pub fn dyn_call<DynCall>(
|
||||
&self,
|
||||
payload: DynCall,
|
||||
) -> impl Future<Output = Result<DynCall::Target, Error>>
|
||||
) -> impl Future<Output = Result<<DynCall::Target as DecodeWithMetadata>::Target, Error>>
|
||||
where
|
||||
DynCall: RuntimeApiPayload,
|
||||
<DynCall as RuntimeApiPayload>::Target: Decode,
|
||||
{
|
||||
let client = self.client.clone();
|
||||
let block_hash = self.block_hash;
|
||||
@@ -119,7 +119,13 @@ where
|
||||
.state_call(&function, call_parameters, Some(block_hash))
|
||||
.await?;
|
||||
|
||||
let result: DynCall::Target = Decode::decode(&mut &data.0[..])?;
|
||||
let result_ty = client.metadata().runtime_fn(&function)?.return_ty_id();
|
||||
let result = <DynCall::Target as DecodeWithMetadata>::decode_with_metadata(
|
||||
&mut &data.0[..],
|
||||
result_ty,
|
||||
&client.metadata(),
|
||||
)?;
|
||||
// let result: DynCall::Target = Decode::decode(&mut &data.0[..])?;
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user