difftreelog
feat delegate erc call to other struct
in: master
9 files changed
crates/evm-coder-macros/src/solidity_interface.rsdiffbeforeafterboth23use syn::{23use syn::{24 Expr, FnArg, GenericArgument, Generics, Ident, ImplItem, ImplItemMethod, ItemImpl, Lit, Meta,24 Expr, FnArg, GenericArgument, Generics, Ident, ImplItem, ImplItemMethod, ItemImpl, Lit, Meta,25 MetaNameValue, NestedMeta, PatType, Path, PathArguments, ReturnType, Type, spanned::Spanned,25 MetaNameValue, NestedMeta, PatType, Path, PathArguments, ReturnType, Type, spanned::Spanned,26 parse_str,26};27};272828use crate::{29use crate::{35 name: Ident,36 name: Ident,36 pascal_call_name: Ident,37 pascal_call_name: Ident,37 snake_call_name: Ident,38 snake_call_name: Ident,39 via: Option<(Type, Ident)>,38}40}39impl Is {41impl Is {40 fn try_from(path: &Path) -> syn::Result<Self> {42 fn new_via(path: &Path, via: Option<(Type, Ident)>) -> syn::Result<Self> {41 let name = parse_ident_from_path(path, false)?.clone();43 let name = parse_ident_from_path(path, false)?.clone();42 Ok(Self {44 Ok(Self {43 pascal_call_name: pascal_ident_to_call(&name),45 pascal_call_name: pascal_ident_to_call(&name),44 snake_call_name: pascal_ident_to_snake_call(&name),46 snake_call_name: pascal_ident_to_snake_call(&name),45 name,47 name,48 via,46 })49 })47 }50 }51 fn new(path: &Path) -> syn::Result<Self> {52 Self::new_via(path, None)53 }485449 fn expand_call_def(&self, gen_ref: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {55 fn expand_call_def(&self, gen_ref: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {50 let name = &self.name;56 let name = &self.name;85 ) -> proc_macro2::TokenStream {91 ) -> proc_macro2::TokenStream {86 let name = &self.name;92 let name = &self.name;87 let pascal_call_name = &self.pascal_call_name;93 let pascal_call_name = &self.pascal_call_name;94 let via_typ = self95 .via96 .as_ref()97 .map(|(t, _)| quote! {#t})98 .unwrap_or_else(|| quote! {Self});99 let via_map = self100 .via101 .as_ref()102 .map(|(_, i)| quote! {.#i()})103 .unwrap_or_default();88 quote! {104 quote! {89 #call_name::#name(call) => return <Self as ::evm_coder::Callable<#pascal_call_name #generics>>::call(self, Msg {105 #call_name::#name(call) => return <#via_typ as ::evm_coder::Callable<#pascal_call_name #generics>>::call(self #via_map, Msg {90 call,106 call,91 caller: c.caller,107 caller: c.caller,92 value: c.value,108 value: c.value,126 let mut out = Vec::new();142 let mut out = Vec::new();127 for item in items {143 for item in items {128 match item {144 match item {129 NestedMeta::Meta(Meta::Path(path)) => out.push(Is::try_from(path)?),145 NestedMeta::Meta(Meta::Path(path)) => out.push(Is::new(path)?),146 // TODO: replace meta parsing with manual147 NestedMeta::Meta(Meta::List(list))148 if list.path.is_ident("via") && list.nested.len() == 3 =>149 {150 let mut data = list.nested.iter();151 let typ = match data.next().expect("len == 3") {152 NestedMeta::Lit(Lit::Str(s)) => {153 let v = s.value();154 let typ: Type = parse_str(&v)?;155 typ156 }130 _ => return Err(syn::Error::new(item.span(), "expected path").into()),157 _ => {158 return Err(syn::Error::new(159 item.span(),160 "via typ should be type in string",161 )162 .into())163 }164 };165 let via = match data.next().expect("len == 3") {166 NestedMeta::Meta(Meta::Path(path)) => path167 .get_ident()168 .ok_or_else(|| syn::Error::new(item.span(), "via should be ident"))?,169 _ => return Err(syn::Error::new(item.span(), "via should be ident").into()),170 };171 let path = match data.next().expect("len == 3") {172 NestedMeta::Meta(Meta::Path(path)) => path,173 _ => return Err(syn::Error::new(item.span(), "path should be path").into()),174 };175176 out.push(Is::new_via(path, Some((typ, via.clone())))?)177 }178 _ => {179 return Err(syn::Error::new(180 item.span(),181 "expected either Name or via(\"Type\", getter, Name)",182 )183 .into())184 }131 }185 }132 }186 }133 Ok(Self(out))187 Ok(Self(out))pallets/common/src/erc.rsdiffbeforeafterboth14// You should have received a copy of the GNU General Public License14// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.161617pub use pallet_evm::PrecompileOutput;17use evm_coder::{solidity_interface, types::*, execution::Result};18pub use pallet_evm::PrecompileResult;18pub use pallet_evm::{PrecompileOutput, PrecompileResult, account::CrossAccountId};19use pallet_evm_coder_substrate::dispatch_to_evm;19use sp_core::{H160, U256};20use sp_core::{H160, U256};21use sp_std::vec::Vec;22use up_data_structs::Property;2324use crate::{Pallet, CollectionHandle, Config};202521/// Does not always represent a full collection, for RFT it is either26/// Does not always represent a full collection, for RFT it is either22/// collection (Implementing ERC721), or specific collection token (Implementing ERC20)27/// collection (Implementing ERC721), or specific collection token (Implementing ERC20)26 fn call(self, source: &H160, input: &[u8], value: U256) -> Option<PrecompileResult>;31 fn call(self, source: &H160, input: &[u8], value: U256) -> Option<PrecompileResult>;27}32}3334#[solidity_interface(name = "CollectionProperties")]35impl<T: Config> CollectionHandle<T> {36 fn set_property(&mut self, caller: caller, key: string, value: string) -> Result<()> {37 <Pallet<T>>::set_collection_property(38 self,39 &T::CrossAccountId::from_eth(caller),40 Property {41 key: <Vec<u8>>::from(key)42 .try_into()43 .map_err(|_| "key too large")?,44 value: <Vec<u8>>::from(value)45 .try_into()46 .map_err(|_| "value too large")?,47 },48 )49 .map_err(dispatch_to_evm::<T>)?;50 Ok(())51 }5253 fn delete_property(&mut self, caller: caller, key: string) -> Result<()> {54 self.set_property(caller, key, string::new())55 }56}2857pallets/common/src/lib.rsdiffbeforeafterboth595 .iter()595 .iter()596 .map(|(key, value)| Property {596 .map(|(key, value)| Property {597 key: key.clone(),597 key: key.clone(),598 value: value.clone()598 value: value.clone(),599 })599 })600 .collect();600 .collect();601601pallets/fungible/src/erc.rsdiffbeforeafterboth24use pallet_evm::account::CrossAccountId;24use pallet_evm::account::CrossAccountId;25use pallet_evm_coder_substrate::{call, dispatch_to_evm};25use pallet_evm_coder_substrate::{call, dispatch_to_evm};26use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};26use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};27use pallet_common::{CollectionHandle, erc::CollectionPropertiesCall};272828use crate::{29use crate::{29 Allowance, Balance, Config, FungibleHandle, Pallet, SelfWeightOf, TotalSupply,30 Allowance, Balance, Config, FungibleHandle, Pallet, SelfWeightOf, TotalSupply,147#[solidity_interface(name = "UniqueFungible", is(ERC20))]148#[solidity_interface(149 name = "UniqueFungible",150 is(151 ERC20,152 ERC20UniqueExtensions,153 via("CollectionHandle<T>", common_mut, CollectionProperties)154 )155)]148impl<T: Config> FungibleHandle<T> {}156impl<T: Config> FungibleHandle<T> {}pallets/fungible/src/lib.rsdiffbeforeafterboth110 pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {110 pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {111 self.0111 self.0112 }112 }113 pub fn common_mut(&mut self) -> &mut pallet_common::CollectionHandle<T> {114 &mut self.0115 }113}116}114impl<T: Config> WithRecorder<T> for FungibleHandle<T> {117impl<T: Config> WithRecorder<T> for FungibleHandle<T> {115 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {118 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {pallets/nonfungible/src/common.rsdiffbeforeafterbothno syntactic changes
pallets/nonfungible/src/erc.rsdiffbeforeafterboth26use sp_core::{H160, U256};26use sp_core::{H160, U256};27use sp_std::{vec::Vec, vec};27use sp_std::{vec::Vec, vec};28use pallet_common::{28use pallet_common::{29 erc::{CommonEvmHandler, PrecompileResult},29 erc::{CommonEvmHandler, PrecompileResult, CollectionPropertiesCall},30 CollectionHandle,30};31};31use pallet_evm::account::CrossAccountId;32use pallet_evm::account::CrossAccountId;32use pallet_evm_coder_substrate::call;33use pallet_evm_coder_substrate::call;504 ERC721UniqueExtensions,505 ERC721UniqueExtensions,505 ERC721Mintable,506 ERC721Mintable,506 ERC721Burnable,507 ERC721Burnable,508 via("CollectionHandle<T>", common_mut, CollectionProperties)507 )509 )508)]510)]509impl<T: Config> NonfungibleHandle<T> {}511impl<T: Config> NonfungibleHandle<T> {}pallets/nonfungible/src/lib.rsdiffbeforeafterboth142 pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {142 pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {143 self.0143 self.0144 }144 }145 pub fn common_mut(&mut self) -> &mut pallet_common::CollectionHandle<T> {146 &mut self.0147 }145}148}146impl<T: Config> WithRecorder<T> for NonfungibleHandle<T> {149impl<T: Config> WithRecorder<T> for NonfungibleHandle<T> {147 fn recorder(&self) -> &SubstrateRecorder<T> {150 fn recorder(&self) -> &SubstrateRecorder<T> {primitives/data-structs/src/lib.rsdiffbeforeafterboth699699700 fn try_set_from_iter<I>(&mut self, iter: I) -> Result<(), PropertiesError>700 fn try_set_from_iter<I>(&mut self, iter: I) -> Result<(), PropertiesError>701 where701 where702 I: Iterator<Item=(PropertyKey, Self::Value)>702 I: Iterator<Item = (PropertyKey, Self::Value)>,703 {703 {704 for (key, value) in iter {704 for (key, value) in iter {705 self.try_set(key, value)?;705 self.try_set(key, value)?;712#[derive(Encode, Decode, TypeInfo, Derivative, Clone, PartialEq, MaxEncodedLen)]712#[derive(Encode, Decode, TypeInfo, Derivative, Clone, PartialEq, MaxEncodedLen)]713#[derivative(Default(bound = ""))]713#[derivative(Default(bound = ""))]714pub struct PropertiesMap<Value>(BoundedBTreeMap<PropertyKey, Value, ConstU32<MAX_PROPERTIES_PER_ITEM>>);714pub struct PropertiesMap<Value>(715 BoundedBTreeMap<PropertyKey, Value, ConstU32<MAX_PROPERTIES_PER_ITEM>>,716);715717716impl<Value> PropertiesMap<Value> {718impl<Value> PropertiesMap<Value> {