difftreelog
refactor CanBePlacedInVec
in: master
2 files changed
crates/evm-coder/procedural/src/solidity_interface.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![allow(dead_code)]1819// NOTE: In order to understand this Rust macro better, first read this chapter20// about Procedural Macros in Rust book:21// https://doc.rust-lang.org/reference/procedural-macros.html2223use proc_macro2::TokenStream;24use quote::{quote, ToTokens, format_ident};25use inflector::cases;26use std::fmt::Write;27use syn::{28 Expr, FnArg, GenericArgument, Generics, Ident, ImplItem, ImplItemMethod, ItemImpl, Lit, Meta,29 MetaNameValue, PatType, PathArguments, ReturnType, Type,30 spanned::Spanned,31 parse::{Parse, ParseStream},32 parenthesized, Token, LitInt, LitStr,33};3435use crate::{36 fn_selector_str, parse_ident_from_pat, parse_ident_from_path, parse_path, parse_path_segment,37 parse_result_ok, pascal_ident_to_call, pascal_ident_to_snake_call, snake_ident_to_pascal,38 snake_ident_to_screaming,39};4041struct Is {42 name: Ident,43 pascal_call_name: Ident,44 snake_call_name: Ident,45 via: Option<(Type, Ident)>,46 condition: Option<Expr>,47}48impl Is {49 fn expand_call_def(&self, gen_ref: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {50 let name = &self.name;51 let pascal_call_name = &self.pascal_call_name;52 quote! {53 #name(#pascal_call_name #gen_ref)54 }55 }5657 fn expand_interface_id(&self) -> proc_macro2::TokenStream {58 let pascal_call_name = &self.pascal_call_name;59 quote! {60 interface_id ^= u32::from_be_bytes(#pascal_call_name::interface_id());61 }62 }6364 fn expand_supports_interface(65 &self,66 generics: &proc_macro2::TokenStream,67 ) -> proc_macro2::TokenStream {68 let pascal_call_name = &self.pascal_call_name;69 let condition = self.condition.as_ref().map(|condition| {70 quote! {71 (#condition) &&72 }73 });74 quote! {75 #condition <#pascal_call_name #generics>::supports_interface(this, interface_id)76 }77 }7879 fn expand_variant_weight(&self) -> proc_macro2::TokenStream {80 let name = &self.name;81 quote! {82 Self::#name(call) => call.weight()83 }84 }8586 fn expand_variant_call(87 &self,88 call_name: &proc_macro2::Ident,89 generics: &proc_macro2::TokenStream,90 ) -> proc_macro2::TokenStream {91 let name = &self.name;92 let pascal_call_name = &self.pascal_call_name;93 let via_typ = self94 .via95 .as_ref()96 .map(|(t, _)| quote! {#t})97 .unwrap_or_else(|| quote! {Self});98 let via_map = self99 .via100 .as_ref()101 .map(|(_, i)| quote! {.#i()})102 .unwrap_or_default();103 let condition = self.condition.as_ref().map(|condition| {104 quote! {105 if ({let this = &self; (#condition)})106 }107 });108 quote! {109 #call_name::#name(call) #condition => return <#via_typ as ::evm_coder::Callable<#pascal_call_name #generics>>::call(self #via_map, Msg {110 call,111 caller: c.caller,112 value: c.value,113 })114 }115 }116117 fn expand_parse(&self, generics: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {118 let name = &self.name;119 let pascal_call_name = &self.pascal_call_name;120 quote! {121 if let Some(parsed_call) = <#pascal_call_name #generics>::parse(method_id, reader)? {122 return Ok(Some(Self::#name(parsed_call)))123 }124 }125 }126127 fn expand_generator(&self, generics: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {128 let pascal_call_name = &self.pascal_call_name;129 quote! {130 <#pascal_call_name #generics>::generate_solidity_interface(tc, is_impl);131 }132 }133134 fn expand_event_generator(&self) -> proc_macro2::TokenStream {135 let name = &self.name;136 quote! {137 #name::generate_solidity_interface(tc, is_impl);138 }139 }140}141142#[derive(Default)]143struct IsList(Vec<Is>);144impl Parse for IsList {145 fn parse(input: ParseStream) -> syn::Result<Self> {146 let mut out = vec![];147 loop {148 if input.is_empty() {149 break;150 }151 let name = input.parse::<Ident>()?;152 let lookahead = input.lookahead1();153154 let mut condition: Option<Expr> = None;155 let mut via: Option<(Type, Ident)> = None;156157 if lookahead.peek(syn::token::Paren) {158 let contents;159 parenthesized!(contents in input);160 let input = contents;161162 while !input.is_empty() {163 let lookahead = input.lookahead1();164 if lookahead.peek(Token![if]) {165 input.parse::<Token![if]>()?;166 let contents;167 parenthesized!(contents in input);168 let contents = contents.parse::<Expr>()?;169170 if condition.replace(contents).is_some() {171 return Err(syn::Error::new(input.span(), "condition is already set"));172 }173 } else if lookahead.peek(kw::via) {174 input.parse::<kw::via>()?;175 let contents;176 parenthesized!(contents in input);177178 let method = contents.parse::<Ident>()?;179 contents.parse::<kw::returns>()?;180 let ty = contents.parse::<Type>()?;181182 if via.replace((ty, method)).is_some() {183 return Err(syn::Error::new(input.span(), "via is already set"));184 }185 } else {186 return Err(lookahead.error());187 }188189 if input.peek(Token![,]) {190 input.parse::<Token![,]>()?;191 } else if !input.is_empty() {192 return Err(syn::Error::new(input.span(), "expected end"));193 }194 }195 } else if lookahead.peek(Token![,]) || input.is_empty() {196 // Pass197 } else {198 return Err(lookahead.error());199 };200 out.push(Is {201 pascal_call_name: pascal_ident_to_call(&name),202 snake_call_name: pascal_ident_to_snake_call(&name),203 name,204 via,205 condition,206 });207 if input.peek(Token![,]) {208 input.parse::<Token![,]>()?;209 continue;210 } else {211 break;212 }213 }214 Ok(Self(out))215 }216}217218pub struct InterfaceInfo {219 name: Ident,220 is: IsList,221 inline_is: IsList,222 events: IsList,223 expect_selector: Option<u32>,224}225impl Parse for InterfaceInfo {226 fn parse(input: ParseStream) -> syn::Result<Self> {227 let mut name = None;228 let mut is = None;229 let mut inline_is = None;230 let mut events = None;231 let mut expect_selector = None;232 // TODO: create proc-macro to optimize proc-macro boilerplate? :D233 loop {234 let lookahead = input.lookahead1();235 if lookahead.peek(kw::name) {236 let k = input.parse::<kw::name>()?;237 input.parse::<Token![=]>()?;238 if name.replace(input.parse::<Ident>()?).is_some() {239 return Err(syn::Error::new(k.span(), "name is already set"));240 }241 } else if lookahead.peek(kw::is) {242 let k = input.parse::<kw::is>()?;243 let contents;244 parenthesized!(contents in input);245 if is.replace(contents.parse::<IsList>()?).is_some() {246 return Err(syn::Error::new(k.span(), "is is already set"));247 }248 } else if lookahead.peek(kw::inline_is) {249 let k = input.parse::<kw::inline_is>()?;250 let contents;251 parenthesized!(contents in input);252 if inline_is.replace(contents.parse::<IsList>()?).is_some() {253 return Err(syn::Error::new(k.span(), "inline_is is already set"));254 }255 } else if lookahead.peek(kw::events) {256 let k = input.parse::<kw::events>()?;257 let contents;258 parenthesized!(contents in input);259 if events.replace(contents.parse::<IsList>()?).is_some() {260 return Err(syn::Error::new(k.span(), "events is already set"));261 }262 } else if lookahead.peek(kw::expect_selector) {263 let k = input.parse::<kw::expect_selector>()?;264 input.parse::<Token![=]>()?;265 let value = input.parse::<LitInt>()?;266 if expect_selector267 .replace(value.base10_parse::<u32>()?)268 .is_some()269 {270 return Err(syn::Error::new(k.span(), "expect_selector is already set"));271 }272 } else if input.is_empty() {273 break;274 } else {275 return Err(lookahead.error());276 }277 if input.peek(Token![,]) {278 input.parse::<Token![,]>()?;279 } else {280 break;281 }282 }283 Ok(Self {284 name: name.ok_or_else(|| syn::Error::new(input.span(), "missing name"))?,285 is: is.unwrap_or_default(),286 inline_is: inline_is.unwrap_or_default(),287 events: events.unwrap_or_default(),288 expect_selector,289 })290 }291}292293struct MethodInfo {294 rename_selector: Option<String>,295 hide: bool,296}297impl Parse for MethodInfo {298 fn parse(input: ParseStream) -> syn::Result<Self> {299 let mut rename_selector = None;300 let mut hide = false;301 while !input.is_empty() {302 let lookahead = input.lookahead1();303 if lookahead.peek(kw::rename_selector) {304 let k = input.parse::<kw::rename_selector>()?;305 input.parse::<Token![=]>()?;306 if rename_selector307 .replace(input.parse::<LitStr>()?.value())308 .is_some()309 {310 return Err(syn::Error::new(k.span(), "rename_selector is already set"));311 }312 } else if lookahead.peek(kw::hide) {313 input.parse::<kw::hide>()?;314 hide = true;315 } else {316 return Err(lookahead.error());317 }318319 if input.peek(Token![,]) {320 input.parse::<Token![,]>()?;321 } else if !input.is_empty() {322 return Err(syn::Error::new(input.span(), "expected end"));323 }324 }325 Ok(Self {326 rename_selector,327 hide,328 })329 }330}331332trait AbiType {333 fn plain(&self) -> syn::Result<&Ident>;334 fn is_value(&self) -> bool;335 fn is_caller(&self) -> bool;336 fn is_special(&self) -> bool;337}338339impl AbiType for Type {340 fn plain(&self) -> syn::Result<&Ident> {341 let path = parse_path(self)?;342 let segment = parse_path_segment(path)?;343 if !segment.arguments.is_empty() {344 return Err(syn::Error::new(self.span(), "Not plain type"));345 }346 Ok(&segment.ident)347 }348349 fn is_value(&self) -> bool {350 if let Ok(ident) = self.plain() {351 return ident == "value";352 }353 false354 }355356 fn is_caller(&self) -> bool {357 if let Ok(ident) = self.plain() {358 return ident == "caller";359 }360 false361 }362363 fn is_special(&self) -> bool {364 self.is_caller() || self.is_value()365 }366}367368#[derive(Debug)]369struct MethodArg {370 name: Ident,371 camel_name: String,372 ty: Type,373}374impl MethodArg {375 fn try_from(value: &PatType) -> syn::Result<Self> {376 let name = parse_ident_from_pat(&value.pat)?.clone();377 Ok(Self {378 camel_name: cases::camelcase::to_camel_case(&name.to_string()),379 name,380 ty: value.ty.as_ref().clone(),381 })382 }383 fn is_value(&self) -> bool {384 self.ty.is_value()385 }386 fn is_caller(&self) -> bool {387 self.ty.is_caller()388 }389 fn is_special(&self) -> bool {390 self.ty.is_special()391 }392393 fn expand_call_def(&self) -> proc_macro2::TokenStream {394 assert!(!self.is_special());395 let name = &self.name;396 let ty = &self.ty;397398 quote! {399 #name: #ty400 }401 }402403 fn expand_parse(&self) -> proc_macro2::TokenStream {404 assert!(!self.is_special());405 let name = &self.name;406 let ty = &self.ty;407 quote! {408 #name: <#ty>::abi_read(reader)?409 }410 }411412 fn expand_call_arg(&self) -> proc_macro2::TokenStream {413 if self.is_value() {414 quote! {415 c.value.clone()416 }417 } else if self.is_caller() {418 quote! {419 c.caller.clone()420 }421 } else {422 let name = &self.name;423 quote! {424 #name425 }426 }427 }428429 fn expand_solidity_argument(&self) -> proc_macro2::TokenStream {430 let camel_name = &self.camel_name.to_string();431 let ty = &self.ty;432 quote! {433 <NamedArgument<#ty>>::new(#camel_name)434 }435 }436}437438#[derive(PartialEq)]439enum Mutability {440 Mutable,441 View,442 Pure,443}444445/// Group all keywords for this macro. Usage example:446/// #[solidity_interface(name = "B", inline_is(A))]447mod kw {448 syn::custom_keyword!(weight);449450 syn::custom_keyword!(via);451 syn::custom_keyword!(returns);452 syn::custom_keyword!(name);453 syn::custom_keyword!(is);454 syn::custom_keyword!(inline_is);455 syn::custom_keyword!(events);456 syn::custom_keyword!(expect_selector);457458 syn::custom_keyword!(rename_selector);459 syn::custom_keyword!(hide);460}461462/// Rust methods are parsed into this structure when Solidity code is generated463struct Method {464 name: Ident,465 camel_name: String,466 pascal_name: Ident,467 screaming_name: Ident,468 hide: bool,469 args: Vec<MethodArg>,470 has_normal_args: bool,471 has_value_args: bool,472 mutability: Mutability,473 result: Type,474 weight: Option<Expr>,475 docs: Vec<String>,476}477impl Method {478 fn try_from(value: &ImplItemMethod) -> syn::Result<Self> {479 let mut info = MethodInfo {480 rename_selector: None,481 hide: false,482 };483 let mut docs = Vec::new();484 let mut weight = None;485 for attr in &value.attrs {486 let ident = parse_ident_from_path(&attr.path, false)?;487 if ident == "solidity" {488 info = attr.parse_args::<MethodInfo>()?;489 } else if ident == "doc" {490 let args = attr.parse_meta().unwrap();491 let value = match args {492 Meta::NameValue(MetaNameValue {493 lit: Lit::Str(str), ..494 }) => str.value(),495 _ => unreachable!(),496 };497 docs.push(value);498 } else if ident == "weight" {499 weight = Some(attr.parse_args::<Expr>()?);500 }501 }502 let ident = &value.sig.ident;503 let ident_str = ident.to_string();504 if !cases::snakecase::is_snake_case(&ident_str) {505 return Err(syn::Error::new(ident.span(), "method name should be snake_cased\nif alternative solidity name needs to be set - use #[solidity] attribute"));506 }507508 let mut mutability = Mutability::Pure;509510 if let Some(FnArg::Receiver(receiver)) = value511 .sig512 .inputs513 .iter()514 .find(|arg| matches!(arg, FnArg::Receiver(_)))515 {516 if receiver.reference.is_none() {517 return Err(syn::Error::new(518 receiver.span(),519 "receiver should be by ref",520 ));521 }522 if receiver.mutability.is_some() {523 mutability = Mutability::Mutable;524 } else {525 mutability = Mutability::View;526 }527 }528 let mut args = Vec::new();529 for typ in value530 .sig531 .inputs532 .iter()533 .filter(|arg| matches!(arg, FnArg::Typed(_)))534 {535 let typ = match typ {536 FnArg::Typed(typ) => typ,537 _ => unreachable!(),538 };539 args.push(MethodArg::try_from(typ)?);540 }541542 if mutability != Mutability::Mutable && args.iter().any(|arg| arg.is_value()) {543 return Err(syn::Error::new(544 args.iter().find(|arg| arg.is_value()).unwrap().ty.span(),545 "payable function should be mutable",546 ));547 }548549 let result = match &value.sig.output {550 ReturnType::Type(_, ty) => ty,551 _ => return Err(syn::Error::new(value.sig.output.span(), "interface method should return Result<value>\nif there is no value to return - specify void (which is alias to unit)")),552 };553 let result = parse_result_ok(result)?;554555 let camel_name = info556 .rename_selector557 .unwrap_or_else(|| cases::camelcase::to_camel_case(&ident.to_string()));558 let has_normal_args = args.iter().filter(|arg| !arg.is_special()).count() != 0;559 let has_value_args = args.iter().any(|a| a.is_value());560561 Ok(Self {562 name: ident.clone(),563 camel_name,564 pascal_name: snake_ident_to_pascal(ident),565 screaming_name: snake_ident_to_screaming(ident),566 hide: info.hide,567 args,568 has_normal_args,569 has_value_args,570 mutability,571 result: result.clone(),572 weight,573 docs,574 })575 }576 fn expand_call_def(&self) -> proc_macro2::TokenStream {577 let defs = self578 .args579 .iter()580 .filter(|a| !a.is_special())581 .map(|a| a.expand_call_def());582 let pascal_name = &self.pascal_name;583 let docs = &self.docs;584585 if self.has_normal_args {586 quote! {587 #(#[doc = #docs])*588 #[allow(missing_docs)]589 #pascal_name {590 #(591 #defs,592 )*593 }594 }595 } else {596 quote! {#pascal_name}597 }598 }599600 fn expand_const(&self) -> proc_macro2::TokenStream {601 let screaming_name = &self.screaming_name;602 let screaming_name_signature = format_ident!("{}_SIGNATURE", &self.screaming_name);603 let custom_signature = self.expand_custom_signature();604 quote! {605 const #screaming_name_signature: ::evm_coder::custom_signature::FunctionSignature = #custom_signature;606 const #screaming_name: ::evm_coder::types::bytes4 = {607 let mut sum = ::evm_coder::sha3_const::Keccak256::new();608 let mut pos = 0;609 while pos < Self::#screaming_name_signature.unit.len {610 sum = sum.update(&[Self::#screaming_name_signature.unit.data[pos]; 1]);611 pos += 1;612 }613 let a = sum.finalize();614 [a[0], a[1], a[2], a[3]]615 };616 }617 }618619 fn expand_interface_id(&self) -> proc_macro2::TokenStream {620 let screaming_name = &self.screaming_name;621 quote! {622 interface_id ^= u32::from_be_bytes(Self::#screaming_name);623 }624 }625626 fn expand_parse(&self) -> proc_macro2::TokenStream {627 let pascal_name = &self.pascal_name;628 let screaming_name = &self.screaming_name;629 if self.has_normal_args {630 let parsers = self631 .args632 .iter()633 .filter(|a| !a.is_special())634 .map(|a| a.expand_parse());635 quote! {636 Self::#screaming_name => return Ok(Some(Self::#pascal_name {637 #(638 #parsers,639 )*640 }))641 }642 } else {643 quote! { Self::#screaming_name => return Ok(Some(Self::#pascal_name)) }644 }645 }646647 fn expand_variant_call(&self, call_name: &proc_macro2::Ident) -> proc_macro2::TokenStream {648 let pascal_name = &self.pascal_name;649 let name = &self.name;650651 let matcher = if self.has_normal_args {652 let names = self653 .args654 .iter()655 .filter(|a| !a.is_special())656 .map(|a| &a.name);657658 quote! {{659 #(660 #names,661 )*662 }}663 } else {664 quote! {}665 };666667 let receiver = match self.mutability {668 Mutability::Mutable | Mutability::View => quote! {self.},669 Mutability::Pure => quote! {Self::},670 };671 let args = self.args.iter().map(|a| a.expand_call_arg());672673 quote! {674 #call_name::#pascal_name #matcher => {675 let result = #receiver #name(676 #(677 #args,678 )*679 )?;680 (&result).to_result()681 }682 }683 }684685 fn expand_variant_weight(&self) -> proc_macro2::TokenStream {686 let pascal_name = &self.pascal_name;687 if let Some(weight) = &self.weight {688 let matcher = if self.has_normal_args {689 let names = self690 .args691 .iter()692 .filter(|a| !a.is_special())693 .map(|a| &a.name);694695 quote! {{696 #(697 #names,698 )*699 }}700 } else {701 quote! {}702 };703 quote! {704 Self::#pascal_name #matcher => (#weight).into()705 }706 } else {707 let matcher = if self.has_normal_args {708 quote! {{..}}709 } else {710 quote! {}711 };712 quote! {713 Self::#pascal_name #matcher => ().into()714 }715 }716 }717718 fn expand_type(ty: &Type, token_stream: &mut proc_macro2::TokenStream, read_signature: bool) {719 match ty {720 Type::Path(tp) => {721 if let Some(qself) = &tp.qself {722 panic!("no receiver expected {:?}", qself.ty.span());723 }724 let path = &tp.path;725 if path.segments.len() != 1 {726 panic!("expected path to have only one segment {:?}", path.span());727 }728 let last_segment = path.segments.last().unwrap();729730 if last_segment.ident == "Vec" {731 let args = match &last_segment.arguments {732 PathArguments::AngleBracketed(e) => e,733 _ => {734 panic!("missing Vec generic {:?}", last_segment.arguments.span());735 }736 };737 let args = &args.args;738 if args.len() != 1 {739 panic!("expected only one generic for vec {:?}", args.span());740 }741 let arg = args.first().expect("first arg");742743 let ty = match arg {744 GenericArgument::Type(ty) => ty,745 _ => {746 panic!("expected first generic to be type {:?}", arg.span());747 }748 };749750 let mut vec_token = proc_macro2::TokenStream::new();751 Self::expand_type(ty, &mut vec_token, false);752 vec_token = if read_signature {753 quote! { (<Vec<#vec_token>>::SIGNATURE) }754 } else {755 quote! { <Vec<#vec_token>> }756 };757 token_stream.extend(vec_token);758 } else {759 if !last_segment.arguments.is_empty() {760 panic!(761 "unexpected generic arguments for non-vec type {:?}",762 last_segment.arguments.span()763 );764 }765766 let ident = &last_segment.ident;767 let plain_token = if read_signature {768 quote! {769 (<#ident>::SIGNATURE)770 }771 } else {772 quote! {773 #ident774 }775 };776777 token_stream.extend(plain_token);778 }779 }780781 Type::Tuple(tt) => {782 // for ty in tt.elems.iter() {783 // out.push(AbiType::try_from(ty)?)784 // }785786 let mut tuple_types = proc_macro2::TokenStream::new();787 let mut is_first = true;788789 for ty in tt.elems.iter() {790 if is_first {791 is_first = false792 } else {793 tuple_types.extend(quote!(,));794 }795 Self::expand_type(ty, &mut tuple_types, false);796 }797 tuple_types = if read_signature {798 quote! { (<(#tuple_types)>::SIGNATURE) }799 } else {800 quote! { (#tuple_types) }801 };802 token_stream.extend(tuple_types);803 }804805 // Type::Array(arr) => {806 // let wrapped = AbiType::try_from(&arr.elem)?;807 // match &arr.len {808 // Expr::Lit(l) => match &l.lit {809 // Lit::Int(i) => {810 // let num = i.base10_parse::<usize>()?;811 // Ok(AbiType::Array(Box::new(wrapped), num as usize))812 // }813 // _ => Err(syn::Error::new(arr.len.span(), "should be int literal")),814 // },815 // _ => Err(syn::Error::new(arr.len.span(), "should be literal")),816 // }817 // }818 _ => panic!("Unexpected type {ty:?}"),819 }820 // match ty {821 // AbiType::Plain(ref ident) => {822 // let plain_token = if read_signature {823 // quote! {824 // (<#ident>::SIGNATURE)825 // }826 // } else {827 // quote! {828 // #ident829 // }830 // };831832 // token_stream.extend(plain_token);833 // }834835 // AbiType::Tuple(ref tuple_type) => {836 // let mut tuple_types = proc_macro2::TokenStream::new();837 // let mut is_first = true;838839 // for ty in tuple_type {840 // if is_first {841 // is_first = false842 // } else {843 // tuple_types.extend(quote!(,));844 // }845 // Self::expand_type(ty, &mut tuple_types, false);846 // }847 // tuple_types = if read_signature {848 // quote! { (<(#tuple_types)>::SIGNATURE) }849 // } else {850 // quote! { (#tuple_types) }851 // };852 // token_stream.extend(tuple_types);853 // }854855 // AbiType::Vec(ref vec_type) => {856 // let mut vec_token = proc_macro2::TokenStream::new();857 // Self::expand_type(vec_type.as_ref(), &mut vec_token, false);858 // vec_token = if read_signature {859 // quote! { (<Vec<#vec_token>>::SIGNATURE) }860 // } else {861 // quote! { <Vec<#vec_token>> }862 // };863 // token_stream.extend(vec_token);864 // }865866 // AbiType::Array(_, _) => todo!("Array eth signature"),867 // };868 }869870 fn expand_custom_signature(&self) -> proc_macro2::TokenStream {871 let mut token_stream = TokenStream::new();872873 let mut is_first = true;874 for arg in &self.args {875 if arg.is_special() {876 continue;877 }878879 if is_first {880 is_first = false;881 } else {882 token_stream.extend(quote!(,));883 }884 Self::expand_type(&arg.ty, &mut token_stream, true);885 }886887 if !is_first {888 token_stream.extend(quote!(,));889 }890891 let func_name = self.camel_name.clone();892 let func_name = quote!(SignaturePreferences {893 open_name: Some(SignatureUnit::new(#func_name)),894 open_delimiter: Some(SignatureUnit::new("(")),895 param_delimiter: Some(SignatureUnit::new(",")),896 close_delimiter: Some(SignatureUnit::new(")")),897 close_name: None,898 });899 quote!({ ::evm_coder::make_signature!(new fn(#func_name), #token_stream) })900 }901902 fn expand_solidity_function(&self) -> proc_macro2::TokenStream {903 let camel_name = &self.camel_name;904 let mutability = match self.mutability {905 Mutability::Mutable => quote! {SolidityMutability::Mutable},906 Mutability::View => quote! { SolidityMutability::View },907 Mutability::Pure => quote! {SolidityMutability::Pure},908 };909 let result = &self.result;910911 let args = self912 .args913 .iter()914 .filter(|a| !a.is_special())915 .map(MethodArg::expand_solidity_argument);916 let docs = &self.docs;917 let screaming_name = &self.screaming_name;918 let hide = self.hide;919 let custom_signature = self.expand_custom_signature();920 let custom_signature = quote!(921 {922 const cs: FunctionSignature = #custom_signature;923 cs924 }925 );926 let is_payable = self.has_value_args;927928 quote! {929 SolidityFunction {930 docs: &[#(#docs),*],931 hide: #hide,932 selector: u32::from_be_bytes(Self::#screaming_name),933 custom_signature: #custom_signature,934 name: #camel_name,935 mutability: #mutability,936 is_payable: #is_payable,937 args: (938 #(939 #args,940 )*941 ),942 result: <UnnamedArgument<#result>>::default(),943 }944 }945 }946}947948fn generics_list(gen: &Generics) -> proc_macro2::TokenStream {949 if gen.params.is_empty() {950 return quote! {};951 }952 let params = gen.params.iter().map(|p| match p {953 syn::GenericParam::Type(id) => {954 let v = &id.ident;955 quote! {#v}956 }957 syn::GenericParam::Lifetime(lt) => {958 let v = <.lifetime;959 quote! {#v}960 }961 syn::GenericParam::Const(c) => {962 let i = &c.ident;963 quote! {#i}964 }965 });966 quote! { #(#params),* }967}968fn generics_reference(gen: &Generics) -> proc_macro2::TokenStream {969 if gen.params.is_empty() {970 return quote! {};971 }972 let list = generics_list(gen);973 quote! { <#list> }974}975fn generics_data(gen: &Generics) -> proc_macro2::TokenStream {976 let list = generics_list(gen);977 if gen.params.len() == 1 {978 quote! {#list}979 } else {980 quote! { (#list) }981 }982}983984pub struct SolidityInterface {985 generics: Generics,986 name: Box<syn::Type>,987 info: InterfaceInfo,988 methods: Vec<Method>,989 docs: Vec<String>,990}991impl SolidityInterface {992 pub fn try_from(info: InterfaceInfo, value: &ItemImpl) -> syn::Result<Self> {993 let mut methods = Vec::new();994995 for item in &value.items {996 if let ImplItem::Method(method) = item {997 methods.push(Method::try_from(method)?)998 }999 }1000 let mut docs = vec![];1001 for attr in &value.attrs {1002 let ident = parse_ident_from_path(&attr.path, false)?;1003 if ident == "doc" {1004 let args = attr.parse_meta().unwrap();1005 let value = match args {1006 Meta::NameValue(MetaNameValue {1007 lit: Lit::Str(str), ..1008 }) => str.value(),1009 _ => unreachable!(),1010 };1011 docs.push(value);1012 }1013 }1014 Ok(Self {1015 generics: value.generics.clone(),1016 name: value.self_ty.clone(),1017 info,1018 methods,1019 docs,1020 })1021 }1022 pub fn expand(self) -> proc_macro2::TokenStream {1023 let name = self.name;10241025 let solidity_name = self.info.name.to_string();1026 let call_name = pascal_ident_to_call(&self.info.name);1027 let generics = self.generics;1028 let gen_ref = generics_reference(&generics);1029 let gen_data = generics_data(&generics);1030 let gen_where = &generics.where_clause;10311032 let call_sub = self1033 .info1034 .inline_is1035 .01036 .iter()1037 .chain(self.info.is.0.iter())1038 .map(|c| Is::expand_call_def(c, &gen_ref));1039 let call_parse = self1040 .info1041 .inline_is1042 .01043 .iter()1044 .chain(self.info.is.0.iter())1045 .map(|is| Is::expand_parse(is, &gen_ref));1046 let call_variants = self1047 .info1048 .inline_is1049 .01050 .iter()1051 .chain(self.info.is.0.iter())1052 .map(|c| Is::expand_variant_call(c, &call_name, &gen_ref));1053 let weight_variants = self1054 .info1055 .inline_is1056 .01057 .iter()1058 .chain(self.info.is.0.iter())1059 .map(Is::expand_variant_weight);10601061 let inline_interface_id = self.info.inline_is.0.iter().map(Is::expand_interface_id);1062 let supports_interface = self1063 .info1064 .is1065 .01066 .iter()1067 .map(|is| Is::expand_supports_interface(is, &gen_ref));10681069 let calls = self.methods.iter().map(Method::expand_call_def);1070 let consts = self.methods.iter().map(Method::expand_const);1071 let interface_id = self.methods.iter().map(Method::expand_interface_id);1072 let parsers = self.methods.iter().map(Method::expand_parse);1073 let call_variants_this = self1074 .methods1075 .iter()1076 .map(|m| Method::expand_variant_call(m, &call_name));1077 let weight_variants_this = self.methods.iter().map(Method::expand_variant_weight);1078 let solidity_functions = self.methods.iter().map(Method::expand_solidity_function);10791080 // TODO: Inline inline_is1081 let solidity_is = self1082 .info1083 .is1084 .01085 .iter()1086 .chain(self.info.inline_is.0.iter())1087 .map(|is| is.name.to_string());1088 let solidity_events_is = self.info.events.0.iter().map(|is| is.name.to_string());1089 let solidity_generators = self1090 .info1091 .is1092 .01093 .iter()1094 .chain(self.info.inline_is.0.iter())1095 .map(|is| Is::expand_generator(is, &gen_ref));1096 let solidity_event_generators = self.info.events.0.iter().map(Is::expand_event_generator);10971098 let docs = &self.docs;10991100 quote! {1101 #[derive(Debug)]1102 #(#[doc = #docs])*1103 pub enum #call_name #gen_ref {1104 /// Inherited method1105 ERC165Call(::evm_coder::ERC165Call, ::core::marker::PhantomData<#gen_data>),1106 #(1107 #calls,1108 )*1109 #(1110 #call_sub,1111 )*1112 }1113 impl #gen_ref #call_name #gen_ref {1114 #(1115 #consts1116 )*1117 /// Return this call ERC165 selector1118 pub fn interface_id() -> ::evm_coder::types::bytes4 {1119 let mut interface_id = 0;1120 #(#interface_id)*1121 #(#inline_interface_id)*1122 u32::to_be_bytes(interface_id)1123 }1124 /// Generate solidity definitions for methods described in this interface1125 pub fn generate_solidity_interface(tc: &evm_coder::solidity::TypeCollector, is_impl: bool) {1126 use evm_coder::solidity::*;1127 use core::fmt::Write;1128 let interface = SolidityInterface {1129 docs: &[#(#docs),*],1130 name: #solidity_name,1131 selector: Self::interface_id(),1132 is: &["Dummy", "ERC165", #(1133 #solidity_is,1134 )* #(1135 #solidity_events_is,1136 )* ],1137 functions: (#(1138 #solidity_functions,1139 )*),1140 };11411142 let mut out = ::evm_coder::types::string::new();1143 if #solidity_name.starts_with("Inline") {1144 out.push_str("/// @dev inlined interface\n");1145 }1146 let _ = interface.format(is_impl, &mut out, tc);1147 tc.collect(out);1148 #(1149 #solidity_event_generators1150 )*1151 #(1152 #solidity_generators1153 )*1154 if is_impl {1155 tc.collect("/// @dev common stubs holder\ncontract Dummy {\n\tuint8 dummy;\n\tstring stub_error = \"this contract is implemented in native\";\n}\ncontract ERC165 is Dummy {\n\tfunction supportsInterface(bytes4 interfaceID) external view returns (bool) {\n\t\trequire(false, stub_error);\n\t\tinterfaceID;\n\t\treturn true;\n\t}\n}\n".into());1156 } else {1157 tc.collect("/// @dev common stubs holder\ninterface Dummy {\n}\ninterface ERC165 is Dummy {\n\tfunction supportsInterface(bytes4 interfaceID) external view returns (bool);\n}\n".into());1158 }1159 }1160 }1161 impl #gen_ref ::evm_coder::Call for #call_name #gen_ref {1162 fn parse(method_id: ::evm_coder::types::bytes4, reader: &mut ::evm_coder::abi::AbiReader) -> ::evm_coder::execution::Result<Option<Self>> {1163 use ::evm_coder::abi::AbiRead;1164 match method_id {1165 ::evm_coder::ERC165Call::INTERFACE_ID => return Ok(1166 ::evm_coder::ERC165Call::parse(method_id, reader)?1167 .map(|c| Self::ERC165Call(c, ::core::marker::PhantomData))1168 ),1169 #(1170 #parsers,1171 )*1172 _ => {},1173 }1174 #(1175 #call_parse1176 )else*1177 return Ok(None);1178 }1179 }1180 impl #generics #call_name #gen_ref1181 #gen_where1182 {1183 /// Is this contract implements specified ERC165 selector1184 pub fn supports_interface(this: &#name, interface_id: ::evm_coder::types::bytes4) -> bool {1185 interface_id != u32::to_be_bytes(0xffffff) && (1186 interface_id == ::evm_coder::ERC165Call::INTERFACE_ID ||1187 interface_id == Self::interface_id()1188 #(1189 || #supports_interface1190 )*1191 )1192 }1193 }1194 impl #generics ::evm_coder::Weighted for #call_name #gen_ref1195 #gen_where1196 {1197 #[allow(unused_variables)]1198 fn weight(&self) -> ::evm_coder::execution::DispatchInfo {1199 match self {1200 #(1201 #weight_variants,1202 )*1203 // TODO: It should be very cheap, but not free1204 Self::ERC165Call(::evm_coder::ERC165Call::SupportsInterface {..}, _) => ::frame_support::weights::Weight::from_ref_time(100).into(),1205 #(1206 #weight_variants_this,1207 )*1208 }1209 }1210 }1211 impl #generics ::evm_coder::Callable<#call_name #gen_ref> for #name1212 #gen_where1213 {1214 #[allow(unreachable_code)] // In case of no inner calls1215 fn call(&mut self, c: Msg<#call_name #gen_ref>) -> ::evm_coder::execution::ResultWithPostInfo<::evm_coder::abi::AbiWriter> {1216 use ::evm_coder::abi::AbiWrite;1217 match c.call {1218 #(1219 #call_variants,1220 )*1221 #call_name::ERC165Call(::evm_coder::ERC165Call::SupportsInterface {interface_id}, _) => {1222 let mut writer = ::evm_coder::abi::AbiWriter::default();1223 writer.bool(&<#call_name #gen_ref>::supports_interface(self, interface_id));1224 return Ok(writer.into());1225 }1226 _ => {},1227 }1228 let mut writer = ::evm_coder::abi::AbiWriter::default();1229 match c.call {1230 #(1231 #call_variants_this,1232 )*1233 _ => Err(::evm_coder::execution::Error::from("method is not available").into()),1234 }1235 }1236 }1237 }1238 }1239}1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![allow(dead_code)]1819// NOTE: In order to understand this Rust macro better, first read this chapter20// about Procedural Macros in Rust book:21// https://doc.rust-lang.org/reference/procedural-macros.html2223use proc_macro2::TokenStream;24use quote::{quote, format_ident};25use inflector::cases;26use syn::{27 Expr, FnArg, GenericArgument, Generics, Ident, ImplItem, ImplItemMethod, ItemImpl, Lit, Meta,28 MetaNameValue, PatType, PathArguments, ReturnType, Type,29 spanned::Spanned,30 parse::{Parse, ParseStream},31 parenthesized, Token, LitInt, LitStr,32};3334use crate::{35 parse_ident_from_pat, parse_ident_from_path, parse_path, parse_path_segment, parse_result_ok,36 pascal_ident_to_call, pascal_ident_to_snake_call, snake_ident_to_pascal,37 snake_ident_to_screaming,38};3940struct Is {41 name: Ident,42 pascal_call_name: Ident,43 snake_call_name: Ident,44 via: Option<(Type, Ident)>,45 condition: Option<Expr>,46}47impl Is {48 fn expand_call_def(&self, gen_ref: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {49 let name = &self.name;50 let pascal_call_name = &self.pascal_call_name;51 quote! {52 #name(#pascal_call_name #gen_ref)53 }54 }5556 fn expand_interface_id(&self) -> proc_macro2::TokenStream {57 let pascal_call_name = &self.pascal_call_name;58 quote! {59 interface_id ^= u32::from_be_bytes(#pascal_call_name::interface_id());60 }61 }6263 fn expand_supports_interface(64 &self,65 generics: &proc_macro2::TokenStream,66 ) -> proc_macro2::TokenStream {67 let pascal_call_name = &self.pascal_call_name;68 let condition = self.condition.as_ref().map(|condition| {69 quote! {70 (#condition) &&71 }72 });73 quote! {74 #condition <#pascal_call_name #generics>::supports_interface(this, interface_id)75 }76 }7778 fn expand_variant_weight(&self) -> proc_macro2::TokenStream {79 let name = &self.name;80 quote! {81 Self::#name(call) => call.weight()82 }83 }8485 fn expand_variant_call(86 &self,87 call_name: &proc_macro2::Ident,88 generics: &proc_macro2::TokenStream,89 ) -> proc_macro2::TokenStream {90 let name = &self.name;91 let pascal_call_name = &self.pascal_call_name;92 let via_typ = self93 .via94 .as_ref()95 .map(|(t, _)| quote! {#t})96 .unwrap_or_else(|| quote! {Self});97 let via_map = self98 .via99 .as_ref()100 .map(|(_, i)| quote! {.#i()})101 .unwrap_or_default();102 let condition = self.condition.as_ref().map(|condition| {103 quote! {104 if ({let this = &self; (#condition)})105 }106 });107 quote! {108 #call_name::#name(call) #condition => return <#via_typ as ::evm_coder::Callable<#pascal_call_name #generics>>::call(self #via_map, Msg {109 call,110 caller: c.caller,111 value: c.value,112 })113 }114 }115116 fn expand_parse(&self, generics: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {117 let name = &self.name;118 let pascal_call_name = &self.pascal_call_name;119 quote! {120 if let Some(parsed_call) = <#pascal_call_name #generics>::parse(method_id, reader)? {121 return Ok(Some(Self::#name(parsed_call)))122 }123 }124 }125126 fn expand_generator(&self, generics: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {127 let pascal_call_name = &self.pascal_call_name;128 quote! {129 <#pascal_call_name #generics>::generate_solidity_interface(tc, is_impl);130 }131 }132133 fn expand_event_generator(&self) -> proc_macro2::TokenStream {134 let name = &self.name;135 quote! {136 #name::generate_solidity_interface(tc, is_impl);137 }138 }139}140141#[derive(Default)]142struct IsList(Vec<Is>);143impl Parse for IsList {144 fn parse(input: ParseStream) -> syn::Result<Self> {145 let mut out = vec![];146 loop {147 if input.is_empty() {148 break;149 }150 let name = input.parse::<Ident>()?;151 let lookahead = input.lookahead1();152153 let mut condition: Option<Expr> = None;154 let mut via: Option<(Type, Ident)> = None;155156 if lookahead.peek(syn::token::Paren) {157 let contents;158 parenthesized!(contents in input);159 let input = contents;160161 while !input.is_empty() {162 let lookahead = input.lookahead1();163 if lookahead.peek(Token![if]) {164 input.parse::<Token![if]>()?;165 let contents;166 parenthesized!(contents in input);167 let contents = contents.parse::<Expr>()?;168169 if condition.replace(contents).is_some() {170 return Err(syn::Error::new(input.span(), "condition is already set"));171 }172 } else if lookahead.peek(kw::via) {173 input.parse::<kw::via>()?;174 let contents;175 parenthesized!(contents in input);176177 let method = contents.parse::<Ident>()?;178 contents.parse::<kw::returns>()?;179 let ty = contents.parse::<Type>()?;180181 if via.replace((ty, method)).is_some() {182 return Err(syn::Error::new(input.span(), "via is already set"));183 }184 } else {185 return Err(lookahead.error());186 }187188 if input.peek(Token![,]) {189 input.parse::<Token![,]>()?;190 } else if !input.is_empty() {191 return Err(syn::Error::new(input.span(), "expected end"));192 }193 }194 } else if lookahead.peek(Token![,]) || input.is_empty() {195 // Pass196 } else {197 return Err(lookahead.error());198 };199 out.push(Is {200 pascal_call_name: pascal_ident_to_call(&name),201 snake_call_name: pascal_ident_to_snake_call(&name),202 name,203 via,204 condition,205 });206 if input.peek(Token![,]) {207 input.parse::<Token![,]>()?;208 continue;209 } else {210 break;211 }212 }213 Ok(Self(out))214 }215}216217pub struct InterfaceInfo {218 name: Ident,219 is: IsList,220 inline_is: IsList,221 events: IsList,222 expect_selector: Option<u32>,223}224impl Parse for InterfaceInfo {225 fn parse(input: ParseStream) -> syn::Result<Self> {226 let mut name = None;227 let mut is = None;228 let mut inline_is = None;229 let mut events = None;230 let mut expect_selector = None;231 // TODO: create proc-macro to optimize proc-macro boilerplate? :D232 loop {233 let lookahead = input.lookahead1();234 if lookahead.peek(kw::name) {235 let k = input.parse::<kw::name>()?;236 input.parse::<Token![=]>()?;237 if name.replace(input.parse::<Ident>()?).is_some() {238 return Err(syn::Error::new(k.span(), "name is already set"));239 }240 } else if lookahead.peek(kw::is) {241 let k = input.parse::<kw::is>()?;242 let contents;243 parenthesized!(contents in input);244 if is.replace(contents.parse::<IsList>()?).is_some() {245 return Err(syn::Error::new(k.span(), "is is already set"));246 }247 } else if lookahead.peek(kw::inline_is) {248 let k = input.parse::<kw::inline_is>()?;249 let contents;250 parenthesized!(contents in input);251 if inline_is.replace(contents.parse::<IsList>()?).is_some() {252 return Err(syn::Error::new(k.span(), "inline_is is already set"));253 }254 } else if lookahead.peek(kw::events) {255 let k = input.parse::<kw::events>()?;256 let contents;257 parenthesized!(contents in input);258 if events.replace(contents.parse::<IsList>()?).is_some() {259 return Err(syn::Error::new(k.span(), "events is already set"));260 }261 } else if lookahead.peek(kw::expect_selector) {262 let k = input.parse::<kw::expect_selector>()?;263 input.parse::<Token![=]>()?;264 let value = input.parse::<LitInt>()?;265 if expect_selector266 .replace(value.base10_parse::<u32>()?)267 .is_some()268 {269 return Err(syn::Error::new(k.span(), "expect_selector is already set"));270 }271 } else if input.is_empty() {272 break;273 } else {274 return Err(lookahead.error());275 }276 if input.peek(Token![,]) {277 input.parse::<Token![,]>()?;278 } else {279 break;280 }281 }282 Ok(Self {283 name: name.ok_or_else(|| syn::Error::new(input.span(), "missing name"))?,284 is: is.unwrap_or_default(),285 inline_is: inline_is.unwrap_or_default(),286 events: events.unwrap_or_default(),287 expect_selector,288 })289 }290}291292struct MethodInfo {293 rename_selector: Option<String>,294 hide: bool,295}296impl Parse for MethodInfo {297 fn parse(input: ParseStream) -> syn::Result<Self> {298 let mut rename_selector = None;299 let mut hide = false;300 while !input.is_empty() {301 let lookahead = input.lookahead1();302 if lookahead.peek(kw::rename_selector) {303 let k = input.parse::<kw::rename_selector>()?;304 input.parse::<Token![=]>()?;305 if rename_selector306 .replace(input.parse::<LitStr>()?.value())307 .is_some()308 {309 return Err(syn::Error::new(k.span(), "rename_selector is already set"));310 }311 } else if lookahead.peek(kw::hide) {312 input.parse::<kw::hide>()?;313 hide = true;314 } else {315 return Err(lookahead.error());316 }317318 if input.peek(Token![,]) {319 input.parse::<Token![,]>()?;320 } else if !input.is_empty() {321 return Err(syn::Error::new(input.span(), "expected end"));322 }323 }324 Ok(Self {325 rename_selector,326 hide,327 })328 }329}330331trait AbiType {332 fn plain(&self) -> syn::Result<&Ident>;333 fn is_value(&self) -> bool;334 fn is_caller(&self) -> bool;335 fn is_special(&self) -> bool;336}337338impl AbiType for Type {339 fn plain(&self) -> syn::Result<&Ident> {340 let path = parse_path(self)?;341 let segment = parse_path_segment(path)?;342 if !segment.arguments.is_empty() {343 return Err(syn::Error::new(self.span(), "Not plain type"));344 }345 Ok(&segment.ident)346 }347348 fn is_value(&self) -> bool {349 if let Ok(ident) = self.plain() {350 return ident == "value";351 }352 false353 }354355 fn is_caller(&self) -> bool {356 if let Ok(ident) = self.plain() {357 return ident == "caller";358 }359 false360 }361362 fn is_special(&self) -> bool {363 self.is_caller() || self.is_value()364 }365}366367#[derive(Debug)]368struct MethodArg {369 name: Ident,370 camel_name: String,371 ty: Type,372}373impl MethodArg {374 fn try_from(value: &PatType) -> syn::Result<Self> {375 let name = parse_ident_from_pat(&value.pat)?.clone();376 Ok(Self {377 camel_name: cases::camelcase::to_camel_case(&name.to_string()),378 name,379 ty: value.ty.as_ref().clone(),380 })381 }382 fn is_value(&self) -> bool {383 self.ty.is_value()384 }385 fn is_caller(&self) -> bool {386 self.ty.is_caller()387 }388 fn is_special(&self) -> bool {389 self.ty.is_special()390 }391392 fn expand_call_def(&self) -> proc_macro2::TokenStream {393 assert!(!self.is_special());394 let name = &self.name;395 let ty = &self.ty;396397 quote! {398 #name: #ty399 }400 }401402 fn expand_parse(&self) -> proc_macro2::TokenStream {403 assert!(!self.is_special());404 let name = &self.name;405 let ty = &self.ty;406 quote! {407 #name: <#ty>::abi_read(reader)?408 }409 }410411 fn expand_call_arg(&self) -> proc_macro2::TokenStream {412 if self.is_value() {413 quote! {414 c.value.clone()415 }416 } else if self.is_caller() {417 quote! {418 c.caller.clone()419 }420 } else {421 let name = &self.name;422 quote! {423 #name424 }425 }426 }427428 fn expand_solidity_argument(&self) -> proc_macro2::TokenStream {429 let camel_name = &self.camel_name.to_string();430 let ty = &self.ty;431 quote! {432 <NamedArgument<#ty>>::new(#camel_name)433 }434 }435}436437#[derive(PartialEq)]438enum Mutability {439 Mutable,440 View,441 Pure,442}443444/// Group all keywords for this macro. Usage example:445/// #[solidity_interface(name = "B", inline_is(A))]446mod kw {447 syn::custom_keyword!(weight);448449 syn::custom_keyword!(via);450 syn::custom_keyword!(returns);451 syn::custom_keyword!(name);452 syn::custom_keyword!(is);453 syn::custom_keyword!(inline_is);454 syn::custom_keyword!(events);455 syn::custom_keyword!(expect_selector);456457 syn::custom_keyword!(rename_selector);458 syn::custom_keyword!(hide);459}460461/// Rust methods are parsed into this structure when Solidity code is generated462struct Method {463 name: Ident,464 camel_name: String,465 pascal_name: Ident,466 screaming_name: Ident,467 hide: bool,468 args: Vec<MethodArg>,469 has_normal_args: bool,470 has_value_args: bool,471 mutability: Mutability,472 result: Type,473 weight: Option<Expr>,474 docs: Vec<String>,475}476impl Method {477 fn try_from(value: &ImplItemMethod) -> syn::Result<Self> {478 let mut info = MethodInfo {479 rename_selector: None,480 hide: false,481 };482 let mut docs = Vec::new();483 let mut weight = None;484 for attr in &value.attrs {485 let ident = parse_ident_from_path(&attr.path, false)?;486 if ident == "solidity" {487 info = attr.parse_args::<MethodInfo>()?;488 } else if ident == "doc" {489 let args = attr.parse_meta().unwrap();490 let value = match args {491 Meta::NameValue(MetaNameValue {492 lit: Lit::Str(str), ..493 }) => str.value(),494 _ => unreachable!(),495 };496 docs.push(value);497 } else if ident == "weight" {498 weight = Some(attr.parse_args::<Expr>()?);499 }500 }501 let ident = &value.sig.ident;502 let ident_str = ident.to_string();503 if !cases::snakecase::is_snake_case(&ident_str) {504 return Err(syn::Error::new(ident.span(), "method name should be snake_cased\nif alternative solidity name needs to be set - use #[solidity] attribute"));505 }506507 let mut mutability = Mutability::Pure;508509 if let Some(FnArg::Receiver(receiver)) = value510 .sig511 .inputs512 .iter()513 .find(|arg| matches!(arg, FnArg::Receiver(_)))514 {515 if receiver.reference.is_none() {516 return Err(syn::Error::new(517 receiver.span(),518 "receiver should be by ref",519 ));520 }521 if receiver.mutability.is_some() {522 mutability = Mutability::Mutable;523 } else {524 mutability = Mutability::View;525 }526 }527 let mut args = Vec::new();528 for typ in value529 .sig530 .inputs531 .iter()532 .filter(|arg| matches!(arg, FnArg::Typed(_)))533 {534 let typ = match typ {535 FnArg::Typed(typ) => typ,536 _ => unreachable!(),537 };538 args.push(MethodArg::try_from(typ)?);539 }540541 if mutability != Mutability::Mutable && args.iter().any(|arg| arg.is_value()) {542 return Err(syn::Error::new(543 args.iter().find(|arg| arg.is_value()).unwrap().ty.span(),544 "payable function should be mutable",545 ));546 }547548 let result = match &value.sig.output {549 ReturnType::Type(_, ty) => ty,550 _ => return Err(syn::Error::new(value.sig.output.span(), "interface method should return Result<value>\nif there is no value to return - specify void (which is alias to unit)")),551 };552 let result = parse_result_ok(result)?;553554 let camel_name = info555 .rename_selector556 .unwrap_or_else(|| cases::camelcase::to_camel_case(&ident.to_string()));557 let has_normal_args = args.iter().filter(|arg| !arg.is_special()).count() != 0;558 let has_value_args = args.iter().any(|a| a.is_value());559560 Ok(Self {561 name: ident.clone(),562 camel_name,563 pascal_name: snake_ident_to_pascal(ident),564 screaming_name: snake_ident_to_screaming(ident),565 hide: info.hide,566 args,567 has_normal_args,568 has_value_args,569 mutability,570 result: result.clone(),571 weight,572 docs,573 })574 }575 fn expand_call_def(&self) -> proc_macro2::TokenStream {576 let defs = self577 .args578 .iter()579 .filter(|a| !a.is_special())580 .map(|a| a.expand_call_def());581 let pascal_name = &self.pascal_name;582 let docs = &self.docs;583584 if self.has_normal_args {585 quote! {586 #(#[doc = #docs])*587 #[allow(missing_docs)]588 #pascal_name {589 #(590 #defs,591 )*592 }593 }594 } else {595 quote! {#pascal_name}596 }597 }598599 fn expand_const(&self) -> proc_macro2::TokenStream {600 let screaming_name = &self.screaming_name;601 let screaming_name_signature = format_ident!("{}_SIGNATURE", &self.screaming_name);602 let custom_signature = self.expand_custom_signature();603 quote! {604 const #screaming_name_signature: ::evm_coder::custom_signature::FunctionSignature = #custom_signature;605 const #screaming_name: ::evm_coder::types::bytes4 = {606 let mut sum = ::evm_coder::sha3_const::Keccak256::new();607 let mut pos = 0;608 while pos < Self::#screaming_name_signature.unit.len {609 sum = sum.update(&[Self::#screaming_name_signature.unit.data[pos]; 1]);610 pos += 1;611 }612 let a = sum.finalize();613 [a[0], a[1], a[2], a[3]]614 };615 }616 }617618 fn expand_interface_id(&self) -> proc_macro2::TokenStream {619 let screaming_name = &self.screaming_name;620 quote! {621 interface_id ^= u32::from_be_bytes(Self::#screaming_name);622 }623 }624625 fn expand_parse(&self) -> proc_macro2::TokenStream {626 let pascal_name = &self.pascal_name;627 let screaming_name = &self.screaming_name;628 if self.has_normal_args {629 let parsers = self630 .args631 .iter()632 .filter(|a| !a.is_special())633 .map(|a| a.expand_parse());634 quote! {635 Self::#screaming_name => return Ok(Some(Self::#pascal_name {636 #(637 #parsers,638 )*639 }))640 }641 } else {642 quote! { Self::#screaming_name => return Ok(Some(Self::#pascal_name)) }643 }644 }645646 fn expand_variant_call(&self, call_name: &proc_macro2::Ident) -> proc_macro2::TokenStream {647 let pascal_name = &self.pascal_name;648 let name = &self.name;649650 let matcher = if self.has_normal_args {651 let names = self652 .args653 .iter()654 .filter(|a| !a.is_special())655 .map(|a| &a.name);656657 quote! {{658 #(659 #names,660 )*661 }}662 } else {663 quote! {}664 };665666 let receiver = match self.mutability {667 Mutability::Mutable | Mutability::View => quote! {self.},668 Mutability::Pure => quote! {Self::},669 };670 let args = self.args.iter().map(|a| a.expand_call_arg());671672 quote! {673 #call_name::#pascal_name #matcher => {674 let result = #receiver #name(675 #(676 #args,677 )*678 )?;679 (&result).to_result()680 }681 }682 }683684 fn expand_variant_weight(&self) -> proc_macro2::TokenStream {685 let pascal_name = &self.pascal_name;686 if let Some(weight) = &self.weight {687 let matcher = if self.has_normal_args {688 let names = self689 .args690 .iter()691 .filter(|a| !a.is_special())692 .map(|a| &a.name);693694 quote! {{695 #(696 #names,697 )*698 }}699 } else {700 quote! {}701 };702 quote! {703 Self::#pascal_name #matcher => (#weight).into()704 }705 } else {706 let matcher = if self.has_normal_args {707 quote! {{..}}708 } else {709 quote! {}710 };711 quote! {712 Self::#pascal_name #matcher => ().into()713 }714 }715 }716717 fn expand_type(ty: &Type, token_stream: &mut proc_macro2::TokenStream, read_signature: bool) {718 match ty {719 Type::Path(tp) => {720 if let Some(qself) = &tp.qself {721 panic!("no receiver expected {:?}", qself.ty.span());722 }723 let path = &tp.path;724 if path.segments.len() != 1 {725 panic!("expected path to have only one segment {:?}", path.span());726 }727 let last_segment = path.segments.last().unwrap();728729 if last_segment.ident == "Vec" {730 let args = match &last_segment.arguments {731 PathArguments::AngleBracketed(e) => e,732 _ => {733 panic!("missing Vec generic {:?}", last_segment.arguments.span());734 }735 };736 let args = &args.args;737 if args.len() != 1 {738 panic!("expected only one generic for vec {:?}", args.span());739 }740 let arg = args.first().expect("first arg");741742 let ty = match arg {743 GenericArgument::Type(ty) => ty,744 _ => {745 panic!("expected first generic to be type {:?}", arg.span());746 }747 };748749 let mut vec_token = proc_macro2::TokenStream::new();750 Self::expand_type(ty, &mut vec_token, false);751 vec_token = if read_signature {752 quote! { (<Vec<#vec_token>>::SIGNATURE) }753 } else {754 quote! { <Vec<#vec_token>> }755 };756 token_stream.extend(vec_token);757 } else {758 if !last_segment.arguments.is_empty() {759 panic!(760 "unexpected generic arguments for non-vec type {:?}",761 last_segment.arguments.span()762 );763 }764765 let ident = &last_segment.ident;766 let plain_token = if read_signature {767 quote! {768 (<#ident>::SIGNATURE)769 }770 } else {771 quote! {772 #ident773 }774 };775776 token_stream.extend(plain_token);777 }778 }779780 Type::Tuple(tt) => {781 // for ty in tt.elems.iter() {782 // out.push(AbiType::try_from(ty)?)783 // }784785 let mut tuple_types = proc_macro2::TokenStream::new();786 let mut is_first = true;787788 for ty in tt.elems.iter() {789 if is_first {790 is_first = false791 } else {792 tuple_types.extend(quote!(,));793 }794 Self::expand_type(ty, &mut tuple_types, false);795 }796 tuple_types = if read_signature {797 quote! { (<(#tuple_types)>::SIGNATURE) }798 } else {799 quote! { (#tuple_types) }800 };801 token_stream.extend(tuple_types);802 }803804 // Type::Array(arr) => {805 // let wrapped = AbiType::try_from(&arr.elem)?;806 // match &arr.len {807 // Expr::Lit(l) => match &l.lit {808 // Lit::Int(i) => {809 // let num = i.base10_parse::<usize>()?;810 // Ok(AbiType::Array(Box::new(wrapped), num as usize))811 // }812 // _ => Err(syn::Error::new(arr.len.span(), "should be int literal")),813 // },814 // _ => Err(syn::Error::new(arr.len.span(), "should be literal")),815 // }816 // }817 _ => panic!("Unexpected type {ty:?}"),818 }819 // match ty {820 // AbiType::Plain(ref ident) => {821 // let plain_token = if read_signature {822 // quote! {823 // (<#ident>::SIGNATURE)824 // }825 // } else {826 // quote! {827 // #ident828 // }829 // };830831 // token_stream.extend(plain_token);832 // }833834 // AbiType::Tuple(ref tuple_type) => {835 // let mut tuple_types = proc_macro2::TokenStream::new();836 // let mut is_first = true;837838 // for ty in tuple_type {839 // if is_first {840 // is_first = false841 // } else {842 // tuple_types.extend(quote!(,));843 // }844 // Self::expand_type(ty, &mut tuple_types, false);845 // }846 // tuple_types = if read_signature {847 // quote! { (<(#tuple_types)>::SIGNATURE) }848 // } else {849 // quote! { (#tuple_types) }850 // };851 // token_stream.extend(tuple_types);852 // }853854 // AbiType::Vec(ref vec_type) => {855 // let mut vec_token = proc_macro2::TokenStream::new();856 // Self::expand_type(vec_type.as_ref(), &mut vec_token, false);857 // vec_token = if read_signature {858 // quote! { (<Vec<#vec_token>>::SIGNATURE) }859 // } else {860 // quote! { <Vec<#vec_token>> }861 // };862 // token_stream.extend(vec_token);863 // }864865 // AbiType::Array(_, _) => todo!("Array eth signature"),866 // };867 }868869 fn expand_custom_signature(&self) -> proc_macro2::TokenStream {870 let mut token_stream = TokenStream::new();871872 let mut is_first = true;873 for arg in &self.args {874 if arg.is_special() {875 continue;876 }877878 if is_first {879 is_first = false;880 } else {881 token_stream.extend(quote!(,));882 }883 Self::expand_type(&arg.ty, &mut token_stream, true);884 }885886 if !is_first {887 token_stream.extend(quote!(,));888 }889890 let func_name = self.camel_name.clone();891 let func_name = quote!(SignaturePreferences {892 open_name: Some(SignatureUnit::new(#func_name)),893 open_delimiter: Some(SignatureUnit::new("(")),894 param_delimiter: Some(SignatureUnit::new(",")),895 close_delimiter: Some(SignatureUnit::new(")")),896 close_name: None,897 });898 quote!({ ::evm_coder::make_signature!(new fn(#func_name), #token_stream) })899 }900901 fn expand_solidity_function(&self) -> proc_macro2::TokenStream {902 let camel_name = &self.camel_name;903 let mutability = match self.mutability {904 Mutability::Mutable => quote! {SolidityMutability::Mutable},905 Mutability::View => quote! { SolidityMutability::View },906 Mutability::Pure => quote! {SolidityMutability::Pure},907 };908 let result = &self.result;909910 let args = self911 .args912 .iter()913 .filter(|a| !a.is_special())914 .map(MethodArg::expand_solidity_argument);915 let docs = &self.docs;916 let screaming_name = &self.screaming_name;917 let hide = self.hide;918 let custom_signature = self.expand_custom_signature();919 let custom_signature = quote!(920 {921 const cs: FunctionSignature = #custom_signature;922 cs923 }924 );925 let is_payable = self.has_value_args;926927 quote! {928 SolidityFunction {929 docs: &[#(#docs),*],930 hide: #hide,931 selector: u32::from_be_bytes(Self::#screaming_name),932 custom_signature: #custom_signature,933 name: #camel_name,934 mutability: #mutability,935 is_payable: #is_payable,936 args: (937 #(938 #args,939 )*940 ),941 result: <UnnamedArgument<#result>>::default(),942 }943 }944 }945}946947fn generics_list(gen: &Generics) -> proc_macro2::TokenStream {948 if gen.params.is_empty() {949 return quote! {};950 }951 let params = gen.params.iter().map(|p| match p {952 syn::GenericParam::Type(id) => {953 let v = &id.ident;954 quote! {#v}955 }956 syn::GenericParam::Lifetime(lt) => {957 let v = <.lifetime;958 quote! {#v}959 }960 syn::GenericParam::Const(c) => {961 let i = &c.ident;962 quote! {#i}963 }964 });965 quote! { #(#params),* }966}967fn generics_reference(gen: &Generics) -> proc_macro2::TokenStream {968 if gen.params.is_empty() {969 return quote! {};970 }971 let list = generics_list(gen);972 quote! { <#list> }973}974fn generics_data(gen: &Generics) -> proc_macro2::TokenStream {975 let list = generics_list(gen);976 if gen.params.len() == 1 {977 quote! {#list}978 } else {979 quote! { (#list) }980 }981}982983pub struct SolidityInterface {984 generics: Generics,985 name: Box<syn::Type>,986 info: InterfaceInfo,987 methods: Vec<Method>,988 docs: Vec<String>,989}990impl SolidityInterface {991 pub fn try_from(info: InterfaceInfo, value: &ItemImpl) -> syn::Result<Self> {992 let mut methods = Vec::new();993994 for item in &value.items {995 if let ImplItem::Method(method) = item {996 methods.push(Method::try_from(method)?)997 }998 }999 let mut docs = vec![];1000 for attr in &value.attrs {1001 let ident = parse_ident_from_path(&attr.path, false)?;1002 if ident == "doc" {1003 let args = attr.parse_meta().unwrap();1004 let value = match args {1005 Meta::NameValue(MetaNameValue {1006 lit: Lit::Str(str), ..1007 }) => str.value(),1008 _ => unreachable!(),1009 };1010 docs.push(value);1011 }1012 }1013 Ok(Self {1014 generics: value.generics.clone(),1015 name: value.self_ty.clone(),1016 info,1017 methods,1018 docs,1019 })1020 }1021 pub fn expand(self) -> proc_macro2::TokenStream {1022 let name = self.name;10231024 let solidity_name = self.info.name.to_string();1025 let call_name = pascal_ident_to_call(&self.info.name);1026 let generics = self.generics;1027 let gen_ref = generics_reference(&generics);1028 let gen_data = generics_data(&generics);1029 let gen_where = &generics.where_clause;10301031 let call_sub = self1032 .info1033 .inline_is1034 .01035 .iter()1036 .chain(self.info.is.0.iter())1037 .map(|c| Is::expand_call_def(c, &gen_ref));1038 let call_parse = self1039 .info1040 .inline_is1041 .01042 .iter()1043 .chain(self.info.is.0.iter())1044 .map(|is| Is::expand_parse(is, &gen_ref));1045 let call_variants = self1046 .info1047 .inline_is1048 .01049 .iter()1050 .chain(self.info.is.0.iter())1051 .map(|c| Is::expand_variant_call(c, &call_name, &gen_ref));1052 let weight_variants = self1053 .info1054 .inline_is1055 .01056 .iter()1057 .chain(self.info.is.0.iter())1058 .map(Is::expand_variant_weight);10591060 let inline_interface_id = self.info.inline_is.0.iter().map(Is::expand_interface_id);1061 let supports_interface = self1062 .info1063 .is1064 .01065 .iter()1066 .map(|is| Is::expand_supports_interface(is, &gen_ref));10671068 let calls = self.methods.iter().map(Method::expand_call_def);1069 let consts = self.methods.iter().map(Method::expand_const);1070 let interface_id = self.methods.iter().map(Method::expand_interface_id);1071 let parsers = self.methods.iter().map(Method::expand_parse);1072 let call_variants_this = self1073 .methods1074 .iter()1075 .map(|m| Method::expand_variant_call(m, &call_name));1076 let weight_variants_this = self.methods.iter().map(Method::expand_variant_weight);1077 let solidity_functions = self.methods.iter().map(Method::expand_solidity_function);10781079 // TODO: Inline inline_is1080 let solidity_is = self1081 .info1082 .is1083 .01084 .iter()1085 .chain(self.info.inline_is.0.iter())1086 .map(|is| is.name.to_string());1087 let solidity_events_is = self.info.events.0.iter().map(|is| is.name.to_string());1088 let solidity_generators = self1089 .info1090 .is1091 .01092 .iter()1093 .chain(self.info.inline_is.0.iter())1094 .map(|is| Is::expand_generator(is, &gen_ref));1095 let solidity_event_generators = self.info.events.0.iter().map(Is::expand_event_generator);10961097 let docs = &self.docs;10981099 quote! {1100 #[derive(Debug)]1101 #(#[doc = #docs])*1102 pub enum #call_name #gen_ref {1103 /// Inherited method1104 ERC165Call(::evm_coder::ERC165Call, ::core::marker::PhantomData<#gen_data>),1105 #(1106 #calls,1107 )*1108 #(1109 #call_sub,1110 )*1111 }1112 impl #gen_ref #call_name #gen_ref {1113 #(1114 #consts1115 )*1116 /// Return this call ERC165 selector1117 pub fn interface_id() -> ::evm_coder::types::bytes4 {1118 let mut interface_id = 0;1119 #(#interface_id)*1120 #(#inline_interface_id)*1121 u32::to_be_bytes(interface_id)1122 }1123 /// Generate solidity definitions for methods described in this interface1124 pub fn generate_solidity_interface(tc: &evm_coder::solidity::TypeCollector, is_impl: bool) {1125 use evm_coder::solidity::*;1126 use core::fmt::Write;1127 let interface = SolidityInterface {1128 docs: &[#(#docs),*],1129 name: #solidity_name,1130 selector: Self::interface_id(),1131 is: &["Dummy", "ERC165", #(1132 #solidity_is,1133 )* #(1134 #solidity_events_is,1135 )* ],1136 functions: (#(1137 #solidity_functions,1138 )*),1139 };11401141 let mut out = ::evm_coder::types::string::new();1142 if #solidity_name.starts_with("Inline") {1143 out.push_str("/// @dev inlined interface\n");1144 }1145 let _ = interface.format(is_impl, &mut out, tc);1146 tc.collect(out);1147 #(1148 #solidity_event_generators1149 )*1150 #(1151 #solidity_generators1152 )*1153 if is_impl {1154 tc.collect("/// @dev common stubs holder\ncontract Dummy {\n\tuint8 dummy;\n\tstring stub_error = \"this contract is implemented in native\";\n}\ncontract ERC165 is Dummy {\n\tfunction supportsInterface(bytes4 interfaceID) external view returns (bool) {\n\t\trequire(false, stub_error);\n\t\tinterfaceID;\n\t\treturn true;\n\t}\n}\n".into());1155 } else {1156 tc.collect("/// @dev common stubs holder\ninterface Dummy {\n}\ninterface ERC165 is Dummy {\n\tfunction supportsInterface(bytes4 interfaceID) external view returns (bool);\n}\n".into());1157 }1158 }1159 }1160 impl #gen_ref ::evm_coder::Call for #call_name #gen_ref {1161 fn parse(method_id: ::evm_coder::types::bytes4, reader: &mut ::evm_coder::abi::AbiReader) -> ::evm_coder::execution::Result<Option<Self>> {1162 use ::evm_coder::abi::AbiRead;1163 match method_id {1164 ::evm_coder::ERC165Call::INTERFACE_ID => return Ok(1165 ::evm_coder::ERC165Call::parse(method_id, reader)?1166 .map(|c| Self::ERC165Call(c, ::core::marker::PhantomData))1167 ),1168 #(1169 #parsers,1170 )*1171 _ => {},1172 }1173 #(1174 #call_parse1175 )else*1176 return Ok(None);1177 }1178 }1179 impl #generics #call_name #gen_ref1180 #gen_where1181 {1182 /// Is this contract implements specified ERC165 selector1183 pub fn supports_interface(this: &#name, interface_id: ::evm_coder::types::bytes4) -> bool {1184 interface_id != u32::to_be_bytes(0xffffff) && (1185 interface_id == ::evm_coder::ERC165Call::INTERFACE_ID ||1186 interface_id == Self::interface_id()1187 #(1188 || #supports_interface1189 )*1190 )1191 }1192 }1193 impl #generics ::evm_coder::Weighted for #call_name #gen_ref1194 #gen_where1195 {1196 #[allow(unused_variables)]1197 fn weight(&self) -> ::evm_coder::execution::DispatchInfo {1198 match self {1199 #(1200 #weight_variants,1201 )*1202 // TODO: It should be very cheap, but not free1203 Self::ERC165Call(::evm_coder::ERC165Call::SupportsInterface {..}, _) => ::frame_support::weights::Weight::from_ref_time(100).into(),1204 #(1205 #weight_variants_this,1206 )*1207 }1208 }1209 }1210 impl #generics ::evm_coder::Callable<#call_name #gen_ref> for #name1211 #gen_where1212 {1213 #[allow(unreachable_code)] // In case of no inner calls1214 fn call(&mut self, c: Msg<#call_name #gen_ref>) -> ::evm_coder::execution::ResultWithPostInfo<::evm_coder::abi::AbiWriter> {1215 use ::evm_coder::abi::AbiWrite;1216 match c.call {1217 #(1218 #call_variants,1219 )*1220 #call_name::ERC165Call(::evm_coder::ERC165Call::SupportsInterface {interface_id}, _) => {1221 let mut writer = ::evm_coder::abi::AbiWriter::default();1222 writer.bool(&<#call_name #gen_ref>::supports_interface(self, interface_id));1223 return Ok(writer.into());1224 }1225 _ => {},1226 }1227 let mut writer = ::evm_coder::abi::AbiWriter::default();1228 match c.call {1229 #(1230 #call_variants_this,1231 )*1232 _ => Err(::evm_coder::execution::Error::from("method is not available").into()),1233 }1234 }1235 }1236 }1237 }1238}crates/evm-coder/src/abi.rsdiffbeforeafterboth--- a/crates/evm-coder/src/abi.rs
+++ b/crates/evm-coder/src/abi.rs
@@ -352,6 +352,8 @@
macro_rules! impl_abi_readable {
($ty:ty, $method:ident, $dynamic:literal) => {
+ impl sealed::CanBePlacedInVec for $ty {}
+
impl TypeHelper for $ty {
fn is_dynamic() -> bool {
$dynamic
@@ -361,6 +363,7 @@
ABI_ALIGNMENT
}
}
+
impl AbiRead for $ty {
fn abi_read(reader: &mut AbiReader) -> Result<$ty> {
reader.$method()
@@ -370,7 +373,6 @@
}
impl_abi_readable!(bool, bool, false);
-impl_abi_readable!(uint8, uint8, false);
impl_abi_readable!(uint32, uint32, false);
impl_abi_readable!(uint64, uint64, false);
impl_abi_readable!(uint128, uint128, false);
@@ -379,6 +381,20 @@
impl_abi_readable!(address, address, false);
impl_abi_readable!(string, string, true);
+impl TypeHelper for uint8 {
+ fn is_dynamic() -> bool {
+ false
+ }
+ fn size() -> usize {
+ ABI_ALIGNMENT
+ }
+}
+impl AbiRead for uint8 {
+ fn abi_read(reader: &mut AbiReader) -> Result<uint8> {
+ reader.uint8()
+ }
+}
+
impl TypeHelper for bytes {
fn is_dynamic() -> bool {
true
@@ -397,11 +413,6 @@
/// Not all types can be placed in vec, i.e `Vec<u8>` is restricted, `bytes` should be used instead
pub trait CanBePlacedInVec {}
}
-
-impl sealed::CanBePlacedInVec for U256 {}
-impl sealed::CanBePlacedInVec for string {}
-impl sealed::CanBePlacedInVec for H160 {}
-impl sealed::CanBePlacedInVec for EthCrossAccount {}
impl<R: AbiRead + sealed::CanBePlacedInVec> AbiRead for Vec<R> {
fn abi_read(reader: &mut AbiReader) -> Result<Vec<R>> {
@@ -420,6 +431,8 @@
make_signature!(new nameof(R) fixed("[]"));
}
+impl sealed::CanBePlacedInVec for EthCrossAccount {}
+
impl TypeHelper for EthCrossAccount {
fn is_dynamic() -> bool {
address::is_dynamic() || uint256::is_dynamic()