From e642740081b3d415916930c992d6061a0390f690 Mon Sep 17 00:00:00 2001 From: James Wilson Date: Mon, 17 Apr 2023 08:13:17 +0100 Subject: [PATCH 1/4] Remove need to import parity-scale-codec to use subxt macro (#907) * remove need to import parity-scale-codec crate with Subxt for macro to work * update ui tests --- codegen/src/types/derives.rs | 5 +++++ codegen/src/types/tests.rs | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/codegen/src/types/derives.rs b/codegen/src/types/derives.rs index 1f84bcd8ab..0775bae982 100644 --- a/codegen/src/types/derives.rs +++ b/codegen/src/types/derives.rs @@ -93,12 +93,17 @@ impl Derives { derives.insert(syn::parse_quote!(#crate_path::ext::scale_encode::EncodeAsType)); let encode_crate_path = quote::quote! { #crate_path::ext::scale_encode }.to_string(); attributes.insert(syn::parse_quote!(#[encode_as_type(crate_path = #encode_crate_path)])); + derives.insert(syn::parse_quote!(#crate_path::ext::scale_decode::DecodeAsType)); let decode_crate_path = quote::quote! { #crate_path::ext::scale_decode }.to_string(); attributes.insert(syn::parse_quote!(#[decode_as_type(crate_path = #decode_crate_path)])); derives.insert(syn::parse_quote!(#crate_path::ext::codec::Encode)); + attributes.insert(syn::parse_quote!(#[codec(crate = #crate_path::ext::codec)])); + derives.insert(syn::parse_quote!(#crate_path::ext::codec::Decode)); + attributes.insert(syn::parse_quote!(#[codec(crate = #crate_path::ext::codec)])); + derives.insert(syn::parse_quote!(Debug)); Self { diff --git a/codegen/src/types/tests.rs b/codegen/src/types/tests.rs index 055b737ae1..89b7e442a8 100644 --- a/codegen/src/types/tests.rs +++ b/codegen/src/types/tests.rs @@ -53,6 +53,7 @@ fn generate_struct_with_primitives() { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct S { @@ -104,6 +105,7 @@ fn generate_struct_with_a_struct_field() { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Child { @@ -111,6 +113,7 @@ fn generate_struct_with_a_struct_field() { } #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Parent { @@ -156,11 +159,13 @@ fn generate_tuple_struct() { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Child(pub ::core::primitive::i32,); #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Parent(pub ::core::primitive::bool, pub root::subxt_codegen::types::tests::Child,); @@ -245,51 +250,61 @@ fn derive_compact_as_for_uint_wrapper_structs() { use super::root; #[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Su128 { pub a: ::core::primitive::u128, } #[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Su16 { pub a: ::core::primitive::u16, } #[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Su32 { pub a: ::core::primitive::u32, } #[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Su64 { pub a: ::core::primitive::u64, } #[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Su8 { pub a: ::core::primitive::u8, } #[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct TSu128(pub ::core::primitive::u128,); #[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct TSu16(pub ::core::primitive::u16,); #[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct TSu32(pub ::core::primitive::u32,); #[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct TSu64(pub ::core::primitive::u64,); #[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct TSu8(pub ::core::primitive::u8,); @@ -331,6 +346,7 @@ fn generate_enum() { pub mod tests { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub enum E { @@ -394,6 +410,7 @@ fn compact_fields() { pub mod tests { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub enum E { @@ -407,6 +424,7 @@ fn compact_fields() { } #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct S { @@ -414,6 +432,7 @@ fn compact_fields() { } #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct TupleStruct(#[codec(compact)] pub ::core::primitive::u32,); @@ -460,6 +479,7 @@ fn compact_generic_parameter() { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct S { @@ -505,6 +525,7 @@ fn generate_array_field() { pub mod tests { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct S { @@ -547,6 +568,7 @@ fn option_fields() { pub mod tests { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct S { @@ -592,6 +614,7 @@ fn box_fields_struct() { pub mod tests { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct S { @@ -637,6 +660,7 @@ fn box_fields_enum() { pub mod tests { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub enum E { @@ -682,6 +706,7 @@ fn range_fields() { pub mod tests { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct S { @@ -731,6 +756,7 @@ fn generics() { pub mod tests { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Bar { @@ -738,6 +764,7 @@ fn generics() { pub c: root::subxt_codegen::types::tests::Foo<::core::primitive::u8>, } #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Foo<_0> { @@ -786,6 +813,7 @@ fn generics_nested() { pub mod tests { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Bar<_0> { @@ -793,6 +821,7 @@ fn generics_nested() { } #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Foo<_0, _1> { @@ -841,6 +870,7 @@ fn generate_bitvec() { pub mod tests { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct S { @@ -899,6 +929,7 @@ fn generics_with_alias_adds_phantom_data_marker() { pub mod tests { use super::root; #[derive(::subxt_path::ext::codec::CompactAs, ::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct NamedFields<_0> { @@ -907,6 +938,7 @@ fn generics_with_alias_adds_phantom_data_marker() { pub __subxt_unused_type_params: ::core::marker::PhantomData<_0> } #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct UnnamedFields<_0, _1> ( @@ -976,6 +1008,7 @@ fn modules() { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Bar { @@ -984,6 +1017,7 @@ fn modules() { } #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Foo; @@ -993,6 +1027,7 @@ fn modules() { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct Foo { @@ -1035,6 +1070,7 @@ fn dont_force_struct_names_camel_case() { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct AB; @@ -1081,11 +1117,13 @@ fn apply_user_defined_derives_for_all_types() { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Clone, Debug, Eq)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct A(pub root :: subxt_codegen :: types :: tests :: B,); #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Clone, Debug, Eq)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct B; @@ -1153,16 +1191,19 @@ fn apply_user_defined_derives_for_specific_types() { use super::root; #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug, Eq)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct A(pub root :: subxt_codegen :: types :: tests :: B,); #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug, Eq, Hash)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct B(pub root :: subxt_codegen :: types :: tests :: C,); #[derive(::subxt_path::ext::codec::Decode, ::subxt_path::ext::codec::Encode, ::subxt_path::ext::scale_decode::DecodeAsType, ::subxt_path::ext::scale_encode::EncodeAsType, Debug, Eq, Ord, PartialOrd)] + #[codec(crate = ::subxt_path::ext::codec)] #[decode_as_type(crate_path = ":: subxt_path :: ext :: scale_decode")] #[encode_as_type(crate_path = ":: subxt_path :: ext :: scale_encode")] pub struct C; From 2997b1bdc380e6f504c3f68ac8c3ca46598aaeea Mon Sep 17 00:00:00 2001 From: Tadeo Hepperle <62739623+tadeohepperle@users.noreply.github.com> Date: Mon, 17 Apr 2023 10:40:48 +0200 Subject: [PATCH 2/4] Partial fee estimates for SubmittableExtrinsic (#910) * add partial_fee estimation to submittable extrinsic * add integration test * make functions immune to doctest * add doc test * inline encoded_with_len, fix tests * fix test fmt * remove unused imoort * Bump h2 from 0.3.16 to 0.3.17 (#911) Bumps [h2](https://github.com/hyperium/h2) from 0.3.16 to 0.3.17. - [Release notes](https://github.com/hyperium/h2/releases) - [Changelog](https://github.com/hyperium/h2/blob/master/CHANGELOG.md) - [Commits](https://github.com/hyperium/h2/compare/v0.3.16...v0.3.17) --- updated-dependencies: - dependency-name: h2 dependency-type: indirect ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * call_raw returns Res: Decode * remove import * remove struct --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- subxt/src/client/online_client.rs | 9 ++- subxt/src/rpc/rpc.rs | 41 ++++------- subxt/src/runtime_api/runtime_types.rs | 9 +-- subxt/src/tx/tx_client.rs | 30 ++++++-- subxt/src/tx/tx_progress.rs | 29 ++++---- testing/integration-tests/src/blocks/mod.rs | 9 ++- testing/integration-tests/src/client/mod.rs | 80 +++++++++++++++++++-- 7 files changed, 138 insertions(+), 69 deletions(-) diff --git a/subxt/src/client/online_client.rs b/subxt/src/client/online_client.rs index 0d264681c9..703777f26a 100644 --- a/subxt/src/client/online_client.rs +++ b/subxt/src/client/online_client.rs @@ -17,7 +17,7 @@ use crate::{ tx::TxClient, Config, Metadata, }; -use codec::{Compact, Decode}; +use codec::Compact; use derivative::Derivative; use frame_metadata::RuntimeMetadataPrefixed; use futures::future; @@ -136,10 +136,9 @@ impl OnlineClient { /// Fetch the metadata from substrate using the runtime API. async fn fetch_metadata(rpc: &Rpc) -> Result { - let bytes = rpc.state_call("Metadata_metadata", None, None).await?; - let cursor = &mut &*bytes; - let _ = >::decode(cursor)?; - let meta: RuntimeMetadataPrefixed = Decode::decode(cursor)?; + let (_, meta) = rpc + .state_call::<(Compact, RuntimeMetadataPrefixed)>("Metadata_metadata", None, None) + .await?; Ok(meta.try_into()?) } diff --git a/subxt/src/rpc/rpc.rs b/subxt/src/rpc/rpc.rs index 64f146de5d..b21b9d4311 100644 --- a/subxt/src/rpc/rpc.rs +++ b/subxt/src/rpc/rpc.rs @@ -31,16 +31,19 @@ //! # } //! ``` +use std::sync::Arc; + +use codec::{Decode, Encode}; +use frame_metadata::RuntimeMetadataPrefixed; +use serde::Serialize; + +use crate::{error::Error, utils::PhantomDataSendSync, Config, Metadata}; + use super::{ rpc_params, types::{self, ChainHeadEvent, FollowEvent}, RpcClient, RpcClientT, Subscription, }; -use crate::{error::Error, utils::PhantomDataSendSync, Config, Metadata}; -use codec::{Decode, Encode}; -use frame_metadata::RuntimeMetadataPrefixed; -use serde::Serialize; -use std::sync::Arc; /// Client for substrate rpc interfaces pub struct Rpc { @@ -151,25 +154,6 @@ impl Rpc { Ok(metadata) } - /// Execute a runtime API call. - pub async fn call( - &self, - function: String, - call_parameters: Option<&[u8]>, - at: Option, - ) -> Result { - let call_parameters = call_parameters.unwrap_or_default(); - - let bytes: types::Bytes = self - .client - .request( - "state_call", - rpc_params![function, to_hex(call_parameters), at], - ) - .await?; - Ok(bytes) - } - /// Fetch system properties pub async fn system_properties(&self) -> Result { self.client @@ -364,14 +348,13 @@ impl Rpc { } /// Execute a runtime API call. - pub async fn state_call( + pub async fn state_call( &self, function: &str, call_parameters: Option<&[u8]>, at: Option, - ) -> Result { + ) -> Result { let call_parameters = call_parameters.unwrap_or_default(); - let bytes: types::Bytes = self .client .request( @@ -379,7 +362,9 @@ impl Rpc { rpc_params![function, to_hex(call_parameters), at], ) .await?; - Ok(bytes) + let cursor = &mut &bytes[..]; + let res: Res = Decode::decode(cursor)?; + Ok(res) } /// Create and submit an extrinsic and return a subscription to the events triggered. diff --git a/subxt/src/runtime_api/runtime_types.rs b/subxt/src/runtime_api/runtime_types.rs index 70162e4f08..7bcd3436b9 100644 --- a/subxt/src/runtime_api/runtime_types.rs +++ b/subxt/src/runtime_api/runtime_types.rs @@ -3,6 +3,7 @@ // see LICENSE for license details. use crate::{client::OnlineClientT, error::Error, Config}; +use codec::Decode; use derivative::Derivative; use std::{future::Future, marker::PhantomData}; @@ -32,21 +33,21 @@ where Client: OnlineClientT, { /// Execute a raw runtime API call. - pub fn call_raw<'a>( + pub fn call_raw<'a, Res: Decode>( &self, function: &'a str, call_parameters: Option<&'a [u8]>, - ) -> impl Future, Error>> + 'a { + ) -> impl Future> + 'a { 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 { - let data = client + let data: Res = client .rpc() .state_call(function, call_parameters, Some(block_hash)) .await?; - Ok(data.0) + Ok(data) } } } diff --git a/subxt/src/tx/tx_client.rs b/subxt/src/tx/tx_client.rs index 9eb9fa279a..0b73a964e1 100644 --- a/subxt/src/tx/tx_client.rs +++ b/subxt/src/tx/tx_client.rs @@ -2,17 +2,18 @@ // This file is dual-licensed as Apache-2.0 or GPL-3.0. // see LICENSE for license details. -use super::TxPayload; +use std::borrow::Cow; + +use codec::{Compact, Encode}; +use derivative::Derivative; + use crate::{ client::{OfflineClientT, OnlineClientT}, config::{Config, ExtrinsicParams, Hasher}, error::Error, - tx::{Signer as SignerT, TxProgress}, + tx::{Signer as SignerT, TxPayload, TxProgress}, utils::{Encoded, PhantomDataSendSync}, }; -use codec::{Compact, Encode}; -use derivative::Derivative; -use std::borrow::Cow; // This is returned from an API below, so expose it here. pub use crate::rpc::types::DryRunResult; @@ -465,4 +466,23 @@ where let dry_run_bytes = self.client.rpc().dry_run(self.encoded(), at).await?; dry_run_bytes.into_dry_run_result(&self.client.metadata()) } + + /// This returns an estimate for what the extrinsic is expected to cost to execute, less any tips. + /// The actual amount paid can vary from block to block based on node traffic and other factors. + pub async fn partial_fee_estimate(&self) -> Result { + let mut params = self.encoded().to_vec(); + (self.encoded().len() as u32).encode_to(&mut params); + // destructuring RuntimeDispatchInfo, see type information + // data layout: {weight_ref_time: Compact, weight_proof_size: Compact, class: u8, partial_fee: u128} + let (_, _, _, partial_fee) = self + .client + .rpc() + .state_call::<(Compact, Compact, u8, u128)>( + "TransactionPaymentApi_query_info", + Some(¶ms), + None, + ) + .await?; + Ok(partial_fee) + } } diff --git a/subxt/src/tx/tx_progress.rs b/subxt/src/tx/tx_progress.rs index 09a5a40119..97aa77e8c3 100644 --- a/subxt/src/tx/tx_progress.rs +++ b/subxt/src/tx/tx_progress.rs @@ -416,11 +416,7 @@ mod test { use crate::{ client::{OfflineClientT, OnlineClientT}, - config::{ - extrinsic_params::BaseExtrinsicParams, - polkadot::{PlainTip, PolkadotConfig}, - WithExtrinsicParams, - }, + config::{extrinsic_params::BaseExtrinsicParams, polkadot::PlainTip, WithExtrinsicParams}, error::RpcError, rpc::{types::SubstrateTxStatus, RpcSubscription, Subscription}, tx::TxProgress, @@ -429,15 +425,23 @@ mod test { use serde_json::value::RawValue; + type MockTxProgress = TxProgress; + type MockHash = , + > as Config>::Hash; + type MockSubstrateTxStatus = SubstrateTxStatus; + + /// a mock client to satisfy trait bounds in tests #[derive(Clone, Debug)] struct MockClient; - impl OfflineClientT for MockClient { + impl OfflineClientT for MockClient { fn metadata(&self) -> crate::Metadata { panic!("just a mock impl to satisfy trait bounds") } - fn genesis_hash(&self) -> ::Hash { + fn genesis_hash(&self) -> ::Hash { panic!("just a mock impl to satisfy trait bounds") } @@ -446,15 +450,8 @@ mod test { } } - type MockTxProgress = TxProgress; - type MockHash = , - > as Config>::Hash; - type MockSubstrateTxStatus = SubstrateTxStatus; - - impl OnlineClientT for MockClient { - fn rpc(&self) -> &crate::rpc::Rpc { + impl OnlineClientT for MockClient { + fn rpc(&self) -> &crate::rpc::Rpc { panic!("just a mock impl to satisfy trait bounds") } } diff --git a/testing/integration-tests/src/blocks/mod.rs b/testing/integration-tests/src/blocks/mod.rs index ed3fb4b5e5..d9c37c2e73 100644 --- a/testing/integration-tests/src/blocks/mod.rs +++ b/testing/integration-tests/src/blocks/mod.rs @@ -3,7 +3,7 @@ // see LICENSE for license details. use crate::test_context; -use codec::{Compact, Decode}; +use codec::Compact; use frame_metadata::RuntimeMetadataPrefixed; use futures::StreamExt; @@ -101,10 +101,9 @@ async fn runtime_api_call() -> Result<(), subxt::Error> { let block = sub.next().await.unwrap()?; let rt = block.runtime_api().await?; - let bytes = rt.call_raw("Metadata_metadata", None).await?; - let cursor = &mut &*bytes; - let _ = >::decode(cursor)?; - let meta: RuntimeMetadataPrefixed = Decode::decode(cursor)?; + let (_, meta) = rt + .call_raw::<(Compact, RuntimeMetadataPrefixed)>("Metadata_metadata", None) + .await?; let metadata_call = match meta.1 { frame_metadata::RuntimeMetadata::V14(metadata) => metadata, _ => panic!("Metadata V14 unavailable"), diff --git a/testing/integration-tests/src/client/mod.rs b/testing/integration-tests/src/client/mod.rs index 7b4fdb0d45..2dee4a2c66 100644 --- a/testing/integration-tests/src/client/mod.rs +++ b/testing/integration-tests/src/client/mod.rs @@ -388,15 +388,11 @@ async fn rpc_state_call() { let api = ctx.client(); // Call into the runtime of the chain to get the Metadata. - let metadata_bytes = api + let (_, meta) = api .rpc() - .state_call("Metadata_metadata", None, None) + .state_call::<(Compact, RuntimeMetadataPrefixed)>("Metadata_metadata", None, None) .await .unwrap(); - - let cursor = &mut &*metadata_bytes; - let _ = >::decode(cursor).unwrap(); - let meta: RuntimeMetadataPrefixed = Decode::decode(cursor).unwrap(); let metadata_call = match meta.1 { frame_metadata::RuntimeMetadata::V14(metadata) => metadata, _ => panic!("Metadata V14 unavailable"), @@ -582,3 +578,75 @@ async fn chainhead_unstable_unpin() { .await .is_err()); } + +/// taken from original type +#[derive(Encode, Decode, Debug, Clone, Eq, PartialEq)] +pub struct FeeDetails { + /// The minimum fee for a transaction to be included in a block. + pub inclusion_fee: Option, + /// tip + pub tip: u128, +} + +/// taken from original type +/// The base fee and adjusted weight and length fees constitute the _inclusion fee_. +#[derive(Encode, Decode, Debug, Clone, Eq, PartialEq)] +pub struct InclusionFee { + /// minimum amount a user pays for a transaction. + pub base_fee: u128, + /// amount paid for the encoded length (in bytes) of the transaction. + pub len_fee: u128, + /// + /// - `targeted_fee_adjustment`: This is a multiplier that can tune the final fee based on the + /// congestion of the network. + /// - `weight_fee`: This amount is computed based on the weight of the transaction. Weight + /// accounts for the execution time of a transaction. + /// + /// adjusted_weight_fee = targeted_fee_adjustment * weight_fee + pub adjusted_weight_fee: u128, +} + +#[tokio::test] +async fn partial_fee_estimate_correct() { + let ctx = test_context().await; + let api = ctx.client(); + + let alice = pair_signer(AccountKeyring::Alice.pair()); + let hans = pair_signer(Sr25519Pair::generate().0); + + let tx = node_runtime::tx() + .balances() + .transfer(hans.account_id().clone().into(), 1_000_000_000_000); + + let signed_extrinsic = api + .tx() + .create_signed(&tx, &alice, Default::default()) + .await + .unwrap(); + + // Method I: TransactionPaymentApi_query_info + let partial_fee_1 = signed_extrinsic.partial_fee_estimate().await.unwrap(); + + // Method II: TransactionPaymentApi_query_fee_details + calculations + let len_bytes: [u8; 4] = (signed_extrinsic.encoded().len() as u32).to_le_bytes(); + let encoded_with_len = [signed_extrinsic.encoded(), &len_bytes[..]].concat(); + let InclusionFee { + base_fee, + len_fee, + adjusted_weight_fee, + } = api + .rpc() + .state_call::( + "TransactionPaymentApi_query_fee_details", + Some(&encoded_with_len), + None, + ) + .await + .unwrap() + .inclusion_fee + .unwrap(); + let partial_fee_2 = base_fee + len_fee + adjusted_weight_fee; + + // Both methods should yield the same fee + assert_eq!(partial_fee_1, partial_fee_2); +} From 62dce16efa2c6764228ea7ae7b5b27d6ac119eb9 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 17 Apr 2023 12:38:46 +0300 Subject: [PATCH 3/4] Bump serde_json from 1.0.95 to 1.0.96 (#914) Bumps [serde_json](https://github.com/serde-rs/json) from 1.0.95 to 1.0.96. - [Release notes](https://github.com/serde-rs/json/releases) - [Commits](https://github.com/serde-rs/json/compare/v1.0.95...v1.0.96) --- updated-dependencies: - dependency-name: serde_json dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- Cargo.lock | 4 ++-- cli/Cargo.toml | 2 +- subxt/Cargo.toml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8b3f7e4987..32438f9f57 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2900,9 +2900,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.95" +version = "1.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744" +checksum = "057d394a50403bcac12672b2b18fb387ab6d289d957dab67dd201875391e52f1" dependencies = [ "itoa", "ryu", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 8198dcf4e9..6eff088be2 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -28,7 +28,7 @@ color-eyre = "0.6.1" # serialize the metadata serde = { version = "1.0.159", features = ["derive"] } # serialize as json -serde_json = "1.0.95" +serde_json = "1.0.96" # hex encoded metadata to bytes hex = "0.4.3" # actual metadata types diff --git a/subxt/Cargo.toml b/subxt/Cargo.toml index 561c2d711f..18566508e3 100644 --- a/subxt/Cargo.toml +++ b/subxt/Cargo.toml @@ -45,7 +45,7 @@ futures = { version = "0.3.27", default-features = false, features = ["std"] } hex = "0.4.3" jsonrpsee = { version = "0.16", optional = true, features = ["jsonrpsee-types"] } serde = { version = "1.0.159", features = ["derive"] } -serde_json = { version = "1.0.95", features = ["raw_value"] } +serde_json = { version = "1.0.96", features = ["raw_value"] } thiserror = "1.0.40" tracing = "0.1.34" parking_lot = "0.12.0" From aec1cc52c1c0533c367540719a52127fea6253ff Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 17 Apr 2023 12:38:53 +0300 Subject: [PATCH 4/4] Bump clap from 4.2.1 to 4.2.2 (#915) Bumps [clap](https://github.com/clap-rs/clap) from 4.2.1 to 4.2.2. - [Release notes](https://github.com/clap-rs/clap/releases) - [Changelog](https://github.com/clap-rs/clap/blob/master/CHANGELOG.md) - [Commits](https://github.com/clap-rs/clap/compare/v4.2.1...v4.2.2) --- updated-dependencies: - dependency-name: clap dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- Cargo.lock | 56 +++++++++++++++++++++++++------------------------- cli/Cargo.toml | 2 +- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 32438f9f57..df4a47879f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -94,42 +94,51 @@ dependencies = [ [[package]] name = "anstream" -version = "0.2.6" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "342258dd14006105c2b75ab1bd7543a03bdf0cfc94383303ac212a04939dff6f" +checksum = "9e579a7752471abc2a8268df8b20005e3eadd975f585398f17efcfd8d4927371" dependencies = [ "anstyle", "anstyle-parse", + "anstyle-query", "anstyle-wincon", - "concolor-override", - "concolor-query", + "colorchoice", "is-terminal", "utf8parse", ] [[package]] name = "anstyle" -version = "0.3.5" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23ea9e81bd02e310c216d080f6223c179012256e5151c41db88d12c88a1684d2" +checksum = "41ed9a86bf92ae6580e0a31281f65a1b1d867c0cc68d5346e2ae128dddfa6a7d" [[package]] name = "anstyle-parse" -version = "0.1.1" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7d1bb534e9efed14f3e5f44e7dd1a4f709384023a4165199a4241e18dff0116" +checksum = "e765fd216e48e067936442276d1d57399e37bce53c264d6fefbe298080cb57ee" dependencies = [ "utf8parse", ] [[package]] -name = "anstyle-wincon" -version = "0.2.0" +name = "anstyle-query" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3127af6145b149f3287bb9a0d10ad9c5692dba8c53ad48285e5bec4063834fa" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys 0.48.0", +] + +[[package]] +name = "anstyle-wincon" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bcd8291a340dd8ac70e18878bc4501dd7b4ff970cfa21c207d36ece51ea88fd" dependencies = [ "anstyle", - "windows-sys 0.45.0", + "windows-sys 0.48.0", ] [[package]] @@ -461,9 +470,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.2.1" +version = "4.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "046ae530c528f252094e4a77886ee1374437744b2bff1497aa898bbddbbb29b3" +checksum = "9b802d85aaf3a1cdb02b224ba472ebdea62014fccfcb269b95a4d76443b5ee5a" dependencies = [ "clap_builder", "clap_derive", @@ -472,9 +481,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.2.1" +version = "4.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "223163f58c9a40c3b0a43e1c4b50a9ce09f007ea2cb1ec258a687945b4b7929f" +checksum = "14a1a858f532119338887a4b8e1af9c60de8249cd7bafd68036a489e261e37b6" dependencies = [ "anstream", "anstyle", @@ -558,19 +567,10 @@ dependencies = [ ] [[package]] -name = "concolor-override" +name = "colorchoice" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a855d4a1978dc52fb0536a04d384c2c0c1aa273597f08b77c8c4d3b2eec6037f" - -[[package]] -name = "concolor-query" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88d11d52c3d7ca2e6d0040212be9e4dbbcd78b6447f535b6b561f449427944cf" -dependencies = [ - "windows-sys 0.45.0", -] +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" [[package]] name = "constant_time_eq" @@ -3527,7 +3527,7 @@ dependencies = [ name = "subxt-cli" version = "0.28.0" dependencies = [ - "clap 4.2.1", + "clap 4.2.2", "color-eyre", "frame-metadata", "hex", diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 6eff088be2..2b3d77ad57 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -22,7 +22,7 @@ subxt-codegen = { version = "0.28.0", path = "../codegen" } # perform node compatibility subxt-metadata = { version = "0.28.0", path = "../metadata" } # parse command line args -clap = { version = "4.1.11", features = ["derive", "cargo"] } +clap = { version = "4.2.2", features = ["derive", "cargo"] } # colourful error reports color-eyre = "0.6.1" # serialize the metadata