Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read constants from metadata at runtime #494

Merged
merged 6 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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::SignedExtra<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