diff --git a/examples/examples/runtime_calls.rs b/examples/examples/runtime_calls.rs index f07fc3d15d..47a31484b7 100644 --- a/examples/examples/runtime_calls.rs +++ b/examples/examples/runtime_calls.rs @@ -55,11 +55,18 @@ async fn main() -> Result<(), Box> { println!("Result: {:?}", bytes); let alice = AccountKeyring::Alice.to_account_id(); - let api_tx = dynamic::( + // let api_tx = dynamic::( + 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. diff --git a/subxt/src/runtime_api/runtime_payload.rs b/subxt/src/runtime_api/runtime_payload.rs index ee5ca72cca..1767a89bfc 100644 --- a/subxt/src/runtime_api/runtime_payload.rs +++ b/subxt/src/runtime_api/runtime_payload.rs @@ -14,6 +14,8 @@ use codec::{ use scale_value::Composite; use crate::{ + dynamic::DecodedValueThunk, + metadata::DecodeWithMetadata, Error, Metadata, }; @@ -64,7 +66,7 @@ impl RuntimeAPIPayload { /// 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 { _marker: PhantomData, } -impl RuntimeApiPayload for StaticRuntimeApiPayload -where - ArgData: Encode, - ReturnTy: Decode, -{ - type Target = ReturnTy; +// impl RuntimeApiPayload for StaticRuntimeApiPayload +// 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, - ) -> Result<(), Error> { - self.data.encode_to(out); - Ok(()) - } -} +// fn encode_args_to( +// &self, +// _metadata: &Metadata, +// out: &mut Vec, +// ) -> Result<(), Error> { +// self.data.encode_to(out); +// Ok(()) +// } +// } /// DynamicRuntimeApiPayload -pub struct DynamicRuntimeApiPayload { +pub struct DynamicRuntimeApiPayload { func_name: &'static str, fields: Composite<()>, - _marker: PhantomData, } // impl<'a, ReturnTy> DynamicRuntimeApiPayload<'a, ReturnTy> { @@ -125,22 +126,18 @@ pub struct DynamicRuntimeApiPayload { // } /// Construct a dynamic runtime API call. -pub fn dynamic( +pub fn dynamic( func_name: &'static str, fields: impl Into>, -) -> DynamicRuntimeApiPayload { +) -> DynamicRuntimeApiPayload { DynamicRuntimeApiPayload { func_name: func_name.into(), fields: fields.into(), - _marker: PhantomData, } } -impl RuntimeApiPayload for DynamicRuntimeApiPayload -where - ReturnTy: Decode, -{ - type Target = ReturnTy; +impl RuntimeApiPayload for DynamicRuntimeApiPayload { + type Target = DecodedValueThunk; fn encode_args_to( &self, diff --git a/subxt/src/runtime_api/runtime_types.rs b/subxt/src/runtime_api/runtime_types.rs index 239fa56193..c6a00885b3 100644 --- a/subxt/src/runtime_api/runtime_types.rs +++ b/subxt/src/runtime_api/runtime_types.rs @@ -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( &self, payload: DynCall, - ) -> impl Future> + ) -> impl Future::Target, Error>> where DynCall: 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 = ::decode_with_metadata( + &mut &data.0[..], + result_ty, + &client.metadata(), + )?; + // let result: DynCall::Target = Decode::decode(&mut &data.0[..])?; Ok(result) } }