Skip to content

Commit

Permalink
Read constants from metadata at runtime (#494)
Browse files Browse the repository at this point in the history
* Read constants from metadata at runtime

* Update polkadot codegen

* Update polkadot.rs

* Update polkadot.rs
  • Loading branch information
ascjones authored Mar 31, 2022
1 parent 9318f62 commit cc0b1ec
Show file tree
Hide file tree
Showing 3 changed files with 1,044 additions and 1,174 deletions.
25 changes: 19 additions & 6 deletions codegen/src/api/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
// along with subxt. If not, see <http://www.gnu.org/licenses/>.

use crate::types::TypeGenerator;
use frame_metadata::PalletConstantMetadata;
use frame_metadata::{
PalletConstantMetadata,
PalletMetadata,
};
use heck::ToSnakeCase as _;
use proc_macro2::TokenStream as TokenStream2;
use quote::{
Expand All @@ -26,18 +29,22 @@ use scale_info::form::PortableForm;

pub fn generate_constants(
type_gen: &TypeGenerator,
pallet: &PalletMetadata<PortableForm>,
constants: &[PalletConstantMetadata<PortableForm>],
types_mod_ident: &syn::Ident,
) -> TokenStream2 {
let constant_fns = constants.iter().map(|constant| {
let fn_name = format_ident!("{}", constant.name.to_snake_case());
let pallet_name = &pallet.name;
let constant_name = &constant.name;
let return_ty = type_gen.resolve_type_path(constant.ty.id(), &[]);

let ref_slice = constant.value.as_slice();

quote! {
pub fn #fn_name(&self) -> ::core::result::Result<#return_ty, ::subxt::BasicError> {
Ok(::subxt::codec::Decode::decode(&mut &[#(#ref_slice,)*][..])?)
let pallet = self.client.metadata().pallet(#pallet_name)?;
let constant = pallet.constant(#constant_name)?;
let value = ::subxt::codec::Decode::decode(&mut &constant.value[..])?;
Ok(value)
}
}
});
Expand All @@ -46,9 +53,15 @@ pub fn generate_constants(
pub mod constants {
use super::#types_mod_ident;

pub struct ConstantsApi;
pub struct ConstantsApi<'a, T: ::subxt::Config> {
client: &'a ::subxt::Client<T>,
}

impl<'a, T: ::subxt::Config> ConstantsApi<'a, T> {
pub fn new(client: &'a ::subxt::Client<T>) -> Self {
Self { client }
}

impl ConstantsApi {
#(#constant_fns)*
}
}
Expand Down
15 changes: 9 additions & 6 deletions codegen/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ impl RuntimeGenerator {
let constants_mod = if !pallet.constants.is_empty() {
constants::generate_constants(
&type_gen,
pallet,
&pallet.constants,
types_mod_ident,
)
Expand Down Expand Up @@ -300,8 +301,8 @@ impl RuntimeGenerator {
T: ::subxt::Config,
X: ::subxt::extrinsic::ExtrinsicParams<T>,
{
pub fn constants(&'a self) -> ConstantsApi {
ConstantsApi
pub fn constants(&'a self) -> ConstantsApi<'a, T> {
ConstantsApi { client: &self.client }
}

pub fn storage(&'a self) -> StorageApi<'a, T> {
Expand Down Expand Up @@ -335,12 +336,14 @@ impl RuntimeGenerator {
}
}

pub struct ConstantsApi;
pub struct ConstantsApi<'a, T: ::subxt::Config> {
client: &'a ::subxt::Client<T>,
}

impl ConstantsApi {
impl<'a, T: ::subxt::Config> ConstantsApi<'a, T> {
#(
pub fn #pallets_with_constants(&self) -> #pallets_with_constants::constants::ConstantsApi {
#pallets_with_constants::constants::ConstantsApi
pub fn #pallets_with_constants(&self) -> #pallets_with_constants::constants::ConstantsApi<'a, T> {
#pallets_with_constants::constants::ConstantsApi::new(self.client)
}
)*
}
Expand Down
Loading

0 comments on commit cc0b1ec

Please sign in to comment.