git.delta.rocks / unique-network / refs/commits / 3e03fdf5b91e

difftreelog

feat Implementations solidity traits to generate solidity code for macro AbiCoder

Trubnikov Sergey2022-11-21parent: #a16933b.patch.diff
in: master

3 files changed

modifiedcrates/evm-coder/procedural/src/abi_derive.rsdiffbeforeafterboth
32 name,
33 is_named_fields,
34 field_names.clone(),
35 field_types.clone(),
36 );
32 let abi_write = impl_abi_write(name, is_named_fields, params_count, field_names);37 let abi_write = impl_abi_write(name, is_named_fields, params_count, field_names.clone());
33 // let solidity_tuple_type =38 let solidity_type = impl_solidity_type(name, field_types.clone(), params_count);
39 let solidity_type_name = impl_solidity_type_name(name, field_types.clone(), params_count);
40 let solidity_struct_collect = impl_solidity_struct_collect(name, field_names, field_types);
3441
35 Ok(quote! {42 Ok(quote! {
36 #can_be_plcaed_in_vec43 #can_be_plcaed_in_vec
37 #abi_type44 #abi_type
38 #abi_read45 #abi_read
39 #abi_write46 #abi_write
47 #solidity_type
48 #solidity_type_name
49 #solidity_struct_collect
40 })50 })
41}51}
4252
174 )184 )
175}185}
186
187fn impl_solidity_type<'a>(
188 name: &syn::Ident,
189 field_types: impl Iterator<Item = &'a syn::Type> + Clone,
190 params_count: usize,
191) -> proc_macro2::TokenStream {
192 let len = proc_macro2::Literal::usize_suffixed(params_count);
193 quote! {
194 #[cfg(feature = "stubgen")]
195 impl ::evm_coder::solidity::SolidityType for #name {
196 fn names(tc: &::evm_coder::solidity::TypeCollector) -> Vec<String> {
197 let mut collected =
198 Vec::with_capacity(<Self as ::evm_coder::solidity::SolidityType>::len());
199 #({
200 let mut out = String::new();
201 <#field_types as ::evm_coder::solidity::SolidityTypeName>::solidity_name(&mut out, tc)
202 .expect("no fmt error");
203 collected.push(out);
204 })*
205 collected
206 }
207
208 fn len() -> usize {
209 #len
210 }
211 }
212 }
213}
214
215fn impl_solidity_type_name<'a>(
216 name: &syn::Ident,
217 field_types: impl Iterator<Item = &'a syn::Type> + Clone,
218 params_count: usize,
219) -> proc_macro2::TokenStream {
220 let arg_dafaults = field_types.enumerate().map(|(i, ty)| {
221 let mut defult_value = quote!(<#ty as ::evm_coder::solidity::SolidityTypeName
222 >::solidity_default(writer, tc)?;);
223 let last_item = params_count - 1;
224 if i != last_item {
225 defult_value.extend(quote! {write!(writer, ",")?;})
226 }
227 defult_value
228 });
229
230 quote! {
231 #[cfg(feature = "stubgen")]
232 impl ::evm_coder::solidity::SolidityTypeName for #name {
233 fn solidity_name(
234 writer: &mut impl ::core::fmt::Write,
235 tc: &::evm_coder::solidity::TypeCollector,
236 ) -> ::core::fmt::Result {
237 write!(writer, "{}", tc.collect_struct::<Self>())
238 }
239
240 fn is_simple() -> bool {
241 false
242 }
243
244 fn solidity_default(
245 writer: &mut impl ::core::fmt::Write,
246 tc: &::evm_coder::solidity::TypeCollector,
247 ) -> ::core::fmt::Result {
248 write!(writer, "{}(", tc.collect_struct::<Self>())?;
249
250 #(#arg_dafaults)*
251
252 write!(writer, ")")
253 }
254 }
255 }
256}
257
258fn impl_solidity_struct_collect<'a>(
259 name: &syn::Ident,
260 field_names: impl Iterator<Item = proc_macro2::Ident> + Clone,
261 field_types: impl Iterator<Item = &'a syn::Type> + Clone,
262) -> proc_macro2::TokenStream {
263 let string_name = name.to_string();
264 let name_type = field_names
265 .into_iter()
266 .zip(field_types.into_iter())
267 .map(|(name, ty)| {
268 let name = format!("{}", name);
269 quote!(
270 write!(str, "\t{} ", <#ty as ::evm_coder::solidity::StructCollect>::name()).unwrap();
271 write!(str, "{};", #name).unwrap();
272 )
273 });
274
275 quote! {
276 #[cfg(feature = "stubgen")]
277 impl ::evm_coder::solidity::StructCollect for #name {
278 fn name() -> String {
279 #string_name.into()
280 }
281
282 fn declaration() -> String {
283 use std::fmt::Write;
284
285 let mut str = String::new();
286 writeln!(str, "/// @dev Cross account struct").unwrap();
287 writeln!(str, "struct {} {{", Self::name()).unwrap();
288 #(#name_type)*
289 writeln!(str, "}}").unwrap();
290 str
291 }
292 }
293 }
294}
176295
modifiedcrates/evm-coder/src/solidity/impls.rsdiffbeforeafterboth
--- a/crates/evm-coder/src/solidity/impls.rs
+++ b/crates/evm-coder/src/solidity/impls.rs
@@ -1,4 +1,4 @@
-use super::{TypeCollector, SolidityTypeName, SolidityType};
+use super::{TypeCollector, SolidityTypeName, SolidityType, StructCollect};
 use crate::{sealed, types::*};
 use core::fmt;
 
@@ -16,6 +16,16 @@
 					write!(writer, $default)
 				}
             }
+
+			impl StructCollect for $ty {
+				fn name() -> String {
+					$name.to_string()
+				}
+
+				fn declaration() -> String {
+					String::default()
+				}
+			}
         )*
     };
 }
modifiedpallets/common/src/eth.rsdiffbeforeafterboth
--- a/pallets/common/src/eth.rs
+++ b/pallets/common/src/eth.rs
@@ -154,72 +154,3 @@
 		}
 	}
 }
-
-#[cfg(feature = "stubgen")]
-impl ::evm_coder::solidity::SolidityType for EthCrossAccount {
-	fn names(tc: &::evm_coder::solidity::TypeCollector) -> Vec<String> {
-		let mut collected =
-			Vec::with_capacity(<Self as ::evm_coder::solidity::SolidityType>::len());
-		{
-			let mut out = String::new();
-			<address as ::evm_coder::solidity::SolidityTypeName>::solidity_name(&mut out, tc)
-				.expect("no fmt error");
-			collected.push(out);
-		}
-		{
-			let mut out = String::new();
-			<uint256 as ::evm_coder::solidity::SolidityTypeName>::solidity_name(&mut out, tc)
-				.expect("no fmt error");
-			collected.push(out);
-		}
-		collected
-	}
-
-	fn len() -> usize {
-		2
-	}
-}
-
-#[cfg(feature = "stubgen")]
-impl ::evm_coder::solidity::SolidityTypeName for EthCrossAccount {
-	fn solidity_name(
-		writer: &mut impl ::core::fmt::Write,
-		tc: &::evm_coder::solidity::TypeCollector,
-	) -> ::core::fmt::Result {
-		write!(writer, "{}", tc.collect_struct::<Self>())
-	}
-
-	fn is_simple() -> bool {
-		false
-	}
-
-	fn solidity_default(
-		writer: &mut impl ::core::fmt::Write,
-		tc: &::evm_coder::solidity::TypeCollector,
-	) -> ::core::fmt::Result {
-		write!(writer, "{}(", tc.collect_struct::<Self>())?;
-		address::solidity_default(writer, tc)?;
-		write!(writer, ",")?;
-		uint256::solidity_default(writer, tc)?;
-		write!(writer, ")")
-	}
-}
-
-#[cfg(feature = "stubgen")]
-impl ::evm_coder::solidity::StructCollect for EthCrossAccount {
-	fn name() -> String {
-		"EthCrossAccount".into()
-	}
-
-	fn declaration() -> String {
-		use std::fmt::Write;
-
-		let mut str = String::new();
-		writeln!(str, "/// @dev Cross account struct").unwrap();
-		writeln!(str, "struct {} {{", Self::name()).unwrap();
-		writeln!(str, "\taddress eth;").unwrap();
-		writeln!(str, "\tuint256 sub;").unwrap();
-		writeln!(str, "}}").unwrap();
-		str
-	}
-}