|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) 2020 Parity Technologies (UK) Ltd. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +use crate::pallet::Def; |
| 19 | +use frame_support_procedural_tools::clean_type_string; |
| 20 | +use syn::spanned::Spanned; |
| 21 | + |
| 22 | +/// * Generate enum call and implement various trait on it. |
| 23 | +/// * Implement Callable and call_function on `Pallet` |
| 24 | +pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream { |
| 25 | + let frame_support = &def.frame_support; |
| 26 | + let frame_system = &def.frame_system; |
| 27 | + let type_impl_gen = &def.type_impl_generics(); |
| 28 | + let type_decl_bounded_gen = &def.type_decl_bounded_generics(); |
| 29 | + let type_use_gen = &def.type_use_generics(); |
| 30 | + let call_ident = syn::Ident::new("Call", def.call.attr_span.clone()); |
| 31 | + let pallet_ident = &def.pallet_struct.pallet; |
| 32 | + let where_clause = &def.call.where_clause; |
| 33 | + |
| 34 | + let fn_name = def.call.methods.iter().map(|method| &method.name).collect::<Vec<_>>(); |
| 35 | + |
| 36 | + let fn_weight = def.call.methods.iter().map(|method| &method.weight); |
| 37 | + |
| 38 | + let fn_doc = def.call.methods.iter().map(|method| &method.docs).collect::<Vec<_>>(); |
| 39 | + |
| 40 | + let args_name = def.call.methods.iter() |
| 41 | + .map(|method| method.args.iter().map(|(_, name, _)| name.clone()).collect::<Vec<_>>()) |
| 42 | + .collect::<Vec<_>>(); |
| 43 | + |
| 44 | + let args_type = def.call.methods.iter() |
| 45 | + .map(|method| method.args.iter().map(|(_, _, type_)| type_.clone()).collect::<Vec<_>>()) |
| 46 | + .collect::<Vec<_>>(); |
| 47 | + |
| 48 | + let args_compact_attr = def.call.methods.iter().map(|method| { |
| 49 | + method.args.iter() |
| 50 | + .map(|(is_compact, _, type_)| { |
| 51 | + if *is_compact { |
| 52 | + quote::quote_spanned!(type_.span() => #[codec(compact)] ) |
| 53 | + } else { |
| 54 | + quote::quote!() |
| 55 | + } |
| 56 | + }) |
| 57 | + .collect::<Vec<_>>() |
| 58 | + }); |
| 59 | + |
| 60 | + let args_metadata_type = def.call.methods.iter().map(|method| { |
| 61 | + method.args.iter() |
| 62 | + .map(|(is_compact, _, type_)| { |
| 63 | + let final_type = if *is_compact { |
| 64 | + quote::quote!(Compact<#type_>) |
| 65 | + } else { |
| 66 | + quote::quote!(#type_) |
| 67 | + }; |
| 68 | + clean_type_string(&final_type.to_string()) |
| 69 | + }) |
| 70 | + .collect::<Vec<_>>() |
| 71 | + }); |
| 72 | + |
| 73 | + quote::quote_spanned!(def.call.attr_span => |
| 74 | + #[derive( |
| 75 | + #frame_support::RuntimeDebugNoBound, |
| 76 | + #frame_support::CloneNoBound, |
| 77 | + #frame_support::EqNoBound, |
| 78 | + #frame_support::PartialEqNoBound, |
| 79 | + #frame_support::codec::Encode, |
| 80 | + #frame_support::codec::Decode, |
| 81 | + )] |
| 82 | + #[allow(non_camel_case_types)] |
| 83 | + pub enum #call_ident<#type_decl_bounded_gen> #where_clause { |
| 84 | + #[doc(hidden)] |
| 85 | + #[codec(skip)] |
| 86 | + __Ignore( |
| 87 | + #frame_support::sp_std::marker::PhantomData<(#type_use_gen,)>, |
| 88 | + #frame_support::Never, |
| 89 | + ), |
| 90 | + #( #fn_name( #( #args_compact_attr #args_type ),* ), )* |
| 91 | + } |
| 92 | + |
| 93 | + impl<#type_impl_gen> #frame_support::dispatch::GetDispatchInfo |
| 94 | + for #call_ident<#type_use_gen> |
| 95 | + #where_clause |
| 96 | + { |
| 97 | + fn get_dispatch_info(&self) -> #frame_support::dispatch::DispatchInfo { |
| 98 | + match *self { |
| 99 | + #( |
| 100 | + Self::#fn_name ( #( ref #args_name, )* ) => { |
| 101 | + let base_weight = #fn_weight; |
| 102 | + |
| 103 | + let weight = < |
| 104 | + dyn #frame_support::dispatch::WeighData<( #( & #args_type, )* )> |
| 105 | + >::weigh_data(&base_weight, ( #( #args_name, )* )); |
| 106 | + |
| 107 | + let class = < |
| 108 | + dyn #frame_support::dispatch::ClassifyDispatch< |
| 109 | + ( #( & #args_type, )* ) |
| 110 | + > |
| 111 | + >::classify_dispatch(&base_weight, ( #( #args_name, )* )); |
| 112 | + |
| 113 | + let pays_fee = < |
| 114 | + dyn #frame_support::dispatch::PaysFee<( #( & #args_type, )* )> |
| 115 | + >::pays_fee(&base_weight, ( #( #args_name, )* )); |
| 116 | + |
| 117 | + #frame_support::dispatch::DispatchInfo { |
| 118 | + weight, |
| 119 | + class, |
| 120 | + pays_fee, |
| 121 | + } |
| 122 | + }, |
| 123 | + )* |
| 124 | + Self::__Ignore(_, _) => unreachable!("__Ignore cannot be used"), |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + impl<#type_impl_gen> #frame_support::dispatch::GetCallName for #call_ident<#type_use_gen> |
| 130 | + #where_clause |
| 131 | + { |
| 132 | + fn get_call_name(&self) -> &'static str { |
| 133 | + match *self { |
| 134 | + #( Self::#fn_name(..) => stringify!(#fn_name), )* |
| 135 | + Self::__Ignore(_, _) => unreachable!("__PhantomItem cannot be used."), |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + fn get_call_names() -> &'static [&'static str] { |
| 140 | + &[ #( stringify!(#fn_name), )* ] |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + impl<#type_impl_gen> #frame_support::traits::UnfilteredDispatchable |
| 145 | + for #call_ident<#type_use_gen> |
| 146 | + #where_clause |
| 147 | + { |
| 148 | + type Origin = #frame_system::pallet_prelude::OriginFor<T>; |
| 149 | + fn dispatch_bypass_filter( |
| 150 | + self, |
| 151 | + origin: Self::Origin |
| 152 | + ) -> #frame_support::dispatch::DispatchResultWithPostInfo { |
| 153 | + match self { |
| 154 | + #( |
| 155 | + Self::#fn_name( #( #args_name, )* ) => |
| 156 | + <#pallet_ident<#type_use_gen>>::#fn_name(origin, #( #args_name, )* ) |
| 157 | + .map(Into::into).map_err(Into::into), |
| 158 | + )* |
| 159 | + Self::__Ignore(_, _) => { |
| 160 | + let _ = origin; // Use origin for empty Call enum |
| 161 | + unreachable!("__PhantomItem cannot be used."); |
| 162 | + }, |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + impl<#type_impl_gen> #frame_support::dispatch::Callable<T> for #pallet_ident<#type_use_gen> |
| 168 | + #where_clause |
| 169 | + { |
| 170 | + type Call = #call_ident<#type_use_gen>; |
| 171 | + } |
| 172 | + |
| 173 | + impl<#type_impl_gen> #pallet_ident<#type_use_gen> #where_clause { |
| 174 | + #[doc(hidden)] |
| 175 | + pub fn call_functions() -> &'static [#frame_support::dispatch::FunctionMetadata] { |
| 176 | + &[ #( |
| 177 | + #frame_support::dispatch::FunctionMetadata { |
| 178 | + name: #frame_support::dispatch::DecodeDifferent::Encode( |
| 179 | + stringify!(#fn_name) |
| 180 | + ), |
| 181 | + arguments: #frame_support::dispatch::DecodeDifferent::Encode( |
| 182 | + &[ #( |
| 183 | + #frame_support::dispatch::FunctionArgumentMetadata { |
| 184 | + name: #frame_support::dispatch::DecodeDifferent::Encode( |
| 185 | + stringify!(#args_name) |
| 186 | + ), |
| 187 | + ty: #frame_support::dispatch::DecodeDifferent::Encode( |
| 188 | + #args_metadata_type |
| 189 | + ), |
| 190 | + }, |
| 191 | + )* ] |
| 192 | + ), |
| 193 | + documentation: #frame_support::dispatch::DecodeDifferent::Encode( |
| 194 | + &[ #( #fn_doc ),* ] |
| 195 | + ), |
| 196 | + }, |
| 197 | + )* ] |
| 198 | + } |
| 199 | + } |
| 200 | + ) |
| 201 | +} |
0 commit comments