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
1use super::{TypeCollector, SolidityTypeName, SolidityType};1use super::{TypeCollector, SolidityTypeName, SolidityType, StructCollect};
2use crate::{sealed, types::*};2use crate::{sealed, types::*};
3use core::fmt;3use core::fmt;
44
17 }17 }
18 }18 }
19
20 impl StructCollect for $ty {
21 fn name() -> String {
22 $name.to_string()
23 }
24
25 fn declaration() -> String {
26 String::default()
27 }
28 }
19 )*29 )*
20 };30 };
21}31}
modifiedpallets/common/src/eth.rsdiffbeforeafterboth
155 }155 }
156}156}
157
158#[cfg(feature = "stubgen")]
159impl ::evm_coder::solidity::SolidityType for EthCrossAccount {
160 fn names(tc: &::evm_coder::solidity::TypeCollector) -> Vec<String> {
161 let mut collected =
162 Vec::with_capacity(<Self as ::evm_coder::solidity::SolidityType>::len());
163 {
164 let mut out = String::new();
165 <address as ::evm_coder::solidity::SolidityTypeName>::solidity_name(&mut out, tc)
166 .expect("no fmt error");
167 collected.push(out);
168 }
169 {
170 let mut out = String::new();
171 <uint256 as ::evm_coder::solidity::SolidityTypeName>::solidity_name(&mut out, tc)
172 .expect("no fmt error");
173 collected.push(out);
174 }
175 collected
176 }
177
178 fn len() -> usize {
179 2
180 }
181}
182
183#[cfg(feature = "stubgen")]
184impl ::evm_coder::solidity::SolidityTypeName for EthCrossAccount {
185 fn solidity_name(
186 writer: &mut impl ::core::fmt::Write,
187 tc: &::evm_coder::solidity::TypeCollector,
188 ) -> ::core::fmt::Result {
189 write!(writer, "{}", tc.collect_struct::<Self>())
190 }
191
192 fn is_simple() -> bool {
193 false
194 }
195
196 fn solidity_default(
197 writer: &mut impl ::core::fmt::Write,
198 tc: &::evm_coder::solidity::TypeCollector,
199 ) -> ::core::fmt::Result {
200 write!(writer, "{}(", tc.collect_struct::<Self>())?;
201 address::solidity_default(writer, tc)?;
202 write!(writer, ",")?;
203 uint256::solidity_default(writer, tc)?;
204 write!(writer, ")")
205 }
206}
207
208#[cfg(feature = "stubgen")]
209impl ::evm_coder::solidity::StructCollect for EthCrossAccount {
210 fn name() -> String {
211 "EthCrossAccount".into()
212 }
213
214 fn declaration() -> String {
215 use std::fmt::Write;
216
217 let mut str = String::new();
218 writeln!(str, "/// @dev Cross account struct").unwrap();
219 writeln!(str, "struct {} {{", Self::name()).unwrap();
220 writeln!(str, "\taddress eth;").unwrap();
221 writeln!(str, "\tuint256 sub;").unwrap();
222 writeln!(str, "}}").unwrap();
223 str
224 }
225}
226157