1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
use std::collections::BTreeSet;
use inflector::cases;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::{
parenthesized,
parse::{Parse, ParseStream},
spanned::Spanned,
Expr, FnArg, Generics, Ident, ImplItem, ImplItemMethod, ItemImpl, Lit, LitInt, LitStr, Meta,
MetaNameValue, PatType, Path, PathArguments, ReturnType, Token, Type,
};
use crate::{
parse_ident_from_pat, parse_ident_from_path, parse_path, parse_path_segment,
pascal_ident_to_call, snake_ident_to_pascal, snake_ident_to_screaming,
};
struct Is {
name: Ident,
pascal_call_name: Ident,
via: Option<(Type, Ident)>,
condition: Option<Expr>,
}
impl Is {
fn expand_call_def(&self, gen_ref: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let name = &self.name;
let pascal_call_name = &self.pascal_call_name;
quote! {
#name(#pascal_call_name #gen_ref)
}
}
fn expand_interface_id(&self) -> proc_macro2::TokenStream {
let pascal_call_name = &self.pascal_call_name;
quote! {
interface_id ^= u32::from_be_bytes(#pascal_call_name::interface_id());
}
}
fn expand_supports_interface(
&self,
generics: &proc_macro2::TokenStream,
) -> proc_macro2::TokenStream {
let pascal_call_name = &self.pascal_call_name;
let condition = self.condition.as_ref().map(|condition| {
quote! {
(#condition) &&
}
});
quote! {
#condition <#pascal_call_name #generics>::supports_interface(this, interface_id)
}
}
fn expand_variant_call(
&self,
call_name: &proc_macro2::Ident,
generics: &proc_macro2::TokenStream,
) -> proc_macro2::TokenStream {
let name = &self.name;
let pascal_call_name = &self.pascal_call_name;
let via_typ = self
.via
.as_ref()
.map_or_else(|| quote! {Self}, |(t, _)| quote! {#t});
let via_map = self
.via
.as_ref()
.map(|(_, i)| quote! {.#i()})
.unwrap_or_default();
let condition = self.condition.as_ref().map(|condition| {
quote! {
if ({let this = &self; (#condition)})
}
});
quote! {
#call_name::#name(call) #condition => return <#via_typ as ::evm_coder::Callable<#pascal_call_name #generics>>::call(self #via_map, ::evm_coder::types::Msg {
call,
caller: c.caller,
value: c.value,
})
}
}
fn expand_parse(&self, generics: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let name = &self.name;
let pascal_call_name = &self.pascal_call_name;
quote! {
if let Some(parsed_call) = <#pascal_call_name #generics>::parse(method_id, reader)? {
return Ok(Some(Self::#name(parsed_call)))
}
}
}
fn expand_generator(&self, generics: &proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let pascal_call_name = &self.pascal_call_name;
quote! {
<#pascal_call_name #generics>::generate_solidity_interface(tc, is_impl);
}
}
fn expand_event_generator(&self) -> proc_macro2::TokenStream {
let name = &self.name;
quote! {
#name::generate_solidity_interface(tc, is_impl);
}
}
}
#[derive(Default)]
struct IsList(Vec<Is>);
impl Parse for IsList {
fn parse(input: ParseStream) -> syn::Result<Self> {
let mut out = vec![];
loop {
if input.is_empty() {
break;
}
let name = input.parse::<Ident>()?;
let lookahead = input.lookahead1();
let mut condition: Option<Expr> = None;
let mut via: Option<(Type, Ident)> = None;
if lookahead.peek(syn::token::Paren) {
let contents;
parenthesized!(contents in input);
let input = contents;
while !input.is_empty() {
let lookahead = input.lookahead1();
if lookahead.peek(Token![if]) {
input.parse::<Token![if]>()?;
let contents;
parenthesized!(contents in input);
let contents = contents.parse::<Expr>()?;
if condition.replace(contents).is_some() {
return Err(syn::Error::new(input.span(), "condition is already set"));
}
} else if lookahead.peek(kw::via) {
input.parse::<kw::via>()?;
let contents;
parenthesized!(contents in input);
let method = contents.parse::<Ident>()?;
contents.parse::<kw::returns>()?;
let ty = contents.parse::<Type>()?;
if via.replace((ty, method)).is_some() {
return Err(syn::Error::new(input.span(), "via is already set"));
}
} else {
return Err(lookahead.error());
}
if input.peek(Token![,]) {
input.parse::<Token![,]>()?;
} else if !input.is_empty() {
return Err(syn::Error::new(input.span(), "expected end"));
}
}
} else if lookahead.peek(Token![,]) || input.is_empty() {
} else {
return Err(lookahead.error());
};
out.push(Is {
pascal_call_name: pascal_ident_to_call(&name),
name,
via,
condition,
});
if input.peek(Token![,]) {
input.parse::<Token![,]>()?;
continue;
}
break;
}
Ok(Self(out))
}
}
pub struct InterfaceInfo {
name: Ident,
is: IsList,
inline_is: IsList,
events: IsList,
expect_selector: Option<u32>,
enum_attrs: Vec<TokenStream>,
enum_variant_attrs: BTreeSet<Ident>,
}
impl Parse for InterfaceInfo {
fn parse(input: ParseStream) -> syn::Result<Self> {
let mut name = None;
let mut is = None;
let mut inline_is = None;
let mut events = None;
let mut expect_selector = None;
let mut enum_attrs = Vec::new();
let mut enum_variant_attrs = BTreeSet::new();
loop {
let lookahead = input.lookahead1();
if lookahead.peek(kw::name) {
let k = input.parse::<kw::name>()?;
input.parse::<Token![=]>()?;
if name.replace(input.parse::<Ident>()?).is_some() {
return Err(syn::Error::new(k.span(), "name is already set"));
}
} else if lookahead.peek(kw::is) {
let k = input.parse::<kw::is>()?;
let contents;
parenthesized!(contents in input);
if is.replace(contents.parse::<IsList>()?).is_some() {
return Err(syn::Error::new(k.span(), "is is already set"));
}
} else if lookahead.peek(kw::inline_is) {
let k = input.parse::<kw::inline_is>()?;
let contents;
parenthesized!(contents in input);
if inline_is.replace(contents.parse::<IsList>()?).is_some() {
return Err(syn::Error::new(k.span(), "inline_is is already set"));
}
} else if lookahead.peek(kw::events) {
let k = input.parse::<kw::events>()?;
let contents;
parenthesized!(contents in input);
if events.replace(contents.parse::<IsList>()?).is_some() {
return Err(syn::Error::new(k.span(), "events is already set"));
}
} else if lookahead.peek(kw::expect_selector) {
let k = input.parse::<kw::expect_selector>()?;
input.parse::<Token![=]>()?;
let value = input.parse::<LitInt>()?;
if expect_selector
.replace(value.base10_parse::<u32>()?)
.is_some()
{
return Err(syn::Error::new(k.span(), "expect_selector is already set"));
}
} else if lookahead.peek(Token![enum]) {
input.parse::<Token![enum]>()?;
let contents;
parenthesized!(contents in input);
enum_attrs.push(contents.parse()?);
} else if lookahead.peek(kw::enum_attr) {
input.parse::<kw::enum_attr>()?;
let contents;
parenthesized!(contents in input);
enum_variant_attrs.insert(contents.parse()?);
} else if input.is_empty() {
break;
} else {
return Err(lookahead.error());
}
if input.peek(Token![,]) {
input.parse::<Token![,]>()?;
} else {
break;
}
}
Ok(Self {
name: name.ok_or_else(|| syn::Error::new(input.span(), "missing name"))?,
is: is.unwrap_or_default(),
inline_is: inline_is.unwrap_or_default(),
events: events.unwrap_or_default(),
expect_selector,
enum_attrs,
enum_variant_attrs,
})
}
}
struct MethodInfo {
rename_selector: Option<String>,
hide: bool,
enum_attrs: Vec<TokenStream>,
}
impl Parse for MethodInfo {
fn parse(input: ParseStream) -> syn::Result<Self> {
let mut rename_selector = None;
let mut hide = false;
let mut enum_attrs = Vec::new();
while !input.is_empty() {
let lookahead = input.lookahead1();
if lookahead.peek(kw::rename_selector) {
let k = input.parse::<kw::rename_selector>()?;
input.parse::<Token![=]>()?;
if rename_selector
.replace(input.parse::<LitStr>()?.value())
.is_some()
{
return Err(syn::Error::new(k.span(), "rename_selector is already set"));
}
} else if lookahead.peek(kw::hide) {
input.parse::<kw::hide>()?;
hide = true;
} else if lookahead.peek(Token![enum]) {
input.parse::<Token![enum]>()?;
let contents;
parenthesized!(contents in input);
enum_attrs.push(contents.parse()?);
} else {
return Err(lookahead.error());
}
if input.peek(Token![,]) {
input.parse::<Token![,]>()?;
} else if !input.is_empty() {
return Err(syn::Error::new(input.span(), "expected end"));
}
}
Ok(Self {
rename_selector,
hide,
enum_attrs,
})
}
}
trait AbiTypeHelper {
fn plain(&self) -> syn::Result<&Ident>;
fn is_value(&self) -> bool;
fn is_caller(&self) -> bool;
fn is_special(&self) -> bool;
}
impl AbiTypeHelper for Type {
fn plain(&self) -> syn::Result<&Ident> {
let path = parse_path(self)?;
let segment = parse_path_segment(path)?;
if !segment.arguments.is_empty() {
return Err(syn::Error::new(self.span(), "Not plain type"));
}
Ok(&segment.ident)
}
fn is_value(&self) -> bool {
if let Ok(ident) = self.plain() {
return ident == "Value";
}
false
}
fn is_caller(&self) -> bool {
if let Ok(ident) = self.plain() {
return ident == "Caller";
}
false
}
fn is_special(&self) -> bool {
self.is_caller() || self.is_value()
}
}
struct MethodArg {
name: Ident,
camel_name: String,
ty: Type,
}
impl MethodArg {
fn try_from(value: &PatType) -> syn::Result<Self> {
let name = parse_ident_from_pat(&value.pat)?.clone();
Ok(Self {
camel_name: cases::camelcase::to_camel_case(&name.to_string()),
name,
ty: value.ty.as_ref().clone(),
})
}
fn is_value(&self) -> bool {
self.ty.is_value()
}
fn is_caller(&self) -> bool {
self.ty.is_caller()
}
fn is_special(&self) -> bool {
self.ty.is_special()
}
fn expand_call_def(&self) -> proc_macro2::TokenStream {
assert!(!self.is_special());
let name = &self.name;
let ty = &self.ty;
quote! {
#name: #ty
}
}
fn expand_parse(&self) -> proc_macro2::TokenStream {
assert!(!self.is_special());
let name = &self.name;
let ty = &self.ty;
quote! {
#name: {
let value = <#ty as ::evm_coder::abi::AbiRead>::abi_read(reader)?;
if !is_dynamic {reader.bytes_read(<#ty as ::evm_coder::abi::AbiType>::size())};
value
}
}
}
fn expand_call_arg(&self) -> proc_macro2::TokenStream {
if self.is_value() {
quote! {
c.value.clone()
}
} else if self.is_caller() {
quote! {
c.caller.clone()
}
} else {
let name = &self.name;
quote! {
#name
}
}
}
fn expand_solidity_argument(&self) -> proc_macro2::TokenStream {
let camel_name = &self.camel_name.to_string();
let ty = &self.ty;
quote! {
<NamedArgument<#ty>>::new(#camel_name)
}
}
}
#[derive(PartialEq)]
enum Mutability {
Mutable,
View,
Pure,
}
mod kw {
syn::custom_keyword!(via);
syn::custom_keyword!(returns);
syn::custom_keyword!(name);
syn::custom_keyword!(is);
syn::custom_keyword!(inline_is);
syn::custom_keyword!(events);
syn::custom_keyword!(expect_selector);
syn::custom_keyword!(enum_attr);
syn::custom_keyword!(rename_selector);
syn::custom_keyword!(hide);
}
struct Method {
name: Ident,
camel_name: String,
pascal_name: Ident,
screaming_name: Ident,
hide: bool,
args: Vec<MethodArg>,
has_normal_args: bool,
has_value_args: bool,
mutability: Mutability,
result: Box<Type>,
docs: Vec<String>,
enum_attrs: Vec<TokenStream>,
}
impl Method {
fn try_from(value: &mut ImplItemMethod, variant_attrs: &BTreeSet<Ident>) -> syn::Result<Self> {
let mut info = MethodInfo {
rename_selector: None,
hide: false,
enum_attrs: Vec::new(),
};
let mut docs = Vec::new();
let mut to_remove = Vec::new();
let mut extra_enum_attrs = Vec::new();
for (i, attr) in value.attrs.iter().enumerate() {
let ident = parse_ident_from_path(&attr.path, false)?;
if ident == "solidity" {
info = attr.parse_args::<MethodInfo>()?;
to_remove.push(i);
} else if ident == "doc" {
let args = attr.parse_meta().unwrap();
let value = match args {
Meta::NameValue(MetaNameValue {
lit: Lit::Str(str), ..
}) => str.value(),
_ => unreachable!(),
};
docs.push(value);
} else if variant_attrs.contains(ident) {
let path = &attr.path;
let tokens = &attr.tokens;
extra_enum_attrs.push(quote! {#path #tokens});
to_remove.push(i);
}
}
for i in to_remove.iter().rev() {
value.attrs.remove(*i);
}
let ident = &value.sig.ident;
let ident_str = ident.to_string();
if !cases::snakecase::is_snake_case(&ident_str) {
return Err(syn::Error::new(ident.span(), "method name should be snake_cased\nif alternative solidity name needs to be set - use #[solidity] attribute"));
}
let mut mutability = Mutability::Pure;
if let Some(FnArg::Receiver(receiver)) = value
.sig
.inputs
.iter()
.find(|arg| matches!(arg, FnArg::Receiver(_)))
{
if receiver.reference.is_none() {
return Err(syn::Error::new(
receiver.span(),
"receiver should be by ref",
));
}
if receiver.mutability.is_some() {
mutability = Mutability::Mutable;
} else {
mutability = Mutability::View;
}
}
let mut args = Vec::new();
for typ in value
.sig
.inputs
.iter()
.filter(|arg| matches!(arg, FnArg::Typed(_)))
{
let typ = match typ {
FnArg::Typed(typ) => typ,
FnArg::Receiver(_) => unreachable!(),
};
args.push(MethodArg::try_from(typ)?);
}
if mutability != Mutability::Mutable && args.iter().any(MethodArg::is_value) {
return Err(syn::Error::new(
args.iter().find(|arg| arg.is_value()).unwrap().ty.span(),
"payable function should be mutable",
));
}
let result = match &value.sig.output {
ReturnType::Type(_, ty) => ty,
ReturnType::Default => 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)")),
};
let camel_name = info
.rename_selector
.unwrap_or_else(|| cases::camelcase::to_camel_case(&ident.to_string()));
let has_normal_args = args.iter().filter(|arg| !arg.is_special()).count() != 0;
let has_value_args = args.iter().any(MethodArg::is_value);
Ok(Self {
name: ident.clone(),
camel_name,
pascal_name: snake_ident_to_pascal(ident),
screaming_name: snake_ident_to_screaming(ident),
hide: info.hide,
args,
has_normal_args,
has_value_args,
mutability,
result: result.clone(),
docs,
enum_attrs: [info.enum_attrs, extra_enum_attrs].concat(),
})
}
fn expand_call_def(&self) -> proc_macro2::TokenStream {
let defs = self
.args
.iter()
.filter(|a| !a.is_special())
.map(MethodArg::expand_call_def);
let pascal_name = &self.pascal_name;
let docs = &self.docs;
let enum_attrs = &self.enum_attrs;
if self.has_normal_args {
quote! {
#(#[doc = #docs])*
#(#[#enum_attrs])*
#[allow(missing_docs)]
#pascal_name {
#(
#defs,
)*
}
}
} else {
quote! {
#(#[doc = #docs])*
#(#[#enum_attrs])*
#[allow(missing_docs)]
#pascal_name
}
}
}
fn expand_const(&self) -> proc_macro2::TokenStream {
let screaming_name = &self.screaming_name;
let screaming_name_signature = format_ident!("{}_SIGNATURE", &self.screaming_name);
let custom_signature = self.expand_custom_signature();
quote! {
const #screaming_name_signature: ::evm_coder::custom_signature::SignatureUnit = #custom_signature;
const #screaming_name: ::evm_coder::types::Bytes4 = {
let mut sum = ::evm_coder::sha3_const::Keccak256::new();
let mut pos = 0;
while pos < Self::#screaming_name_signature.len {
sum = sum.update(&[Self::#screaming_name_signature.data[pos]; 1]);
pos += 1;
}
let a = sum.finalize();
[a[0], a[1], a[2], a[3]]
};
}
}
fn expand_interface_id(&self) -> proc_macro2::TokenStream {
let screaming_name = &self.screaming_name;
quote! {
interface_id ^= u32::from_be_bytes(Self::#screaming_name);
}
}
fn expand_parse(&self) -> proc_macro2::TokenStream {
let pascal_name = &self.pascal_name;
let screaming_name = &self.screaming_name;
if self.has_normal_args {
let args_iter = self.args.iter().filter(|a| !a.is_special());
let arg_type = args_iter.clone().map(|a| &a.ty);
let parsers = args_iter.map(MethodArg::expand_parse);
quote! {
Self::#screaming_name => {
let is_dynamic = false #(|| <#arg_type as ::evm_coder::abi::AbiType>::is_dynamic())*;
return Ok(Some(Self::#pascal_name {
#(
#parsers,
)*
}))
}
}
} else {
quote! { Self::#screaming_name => return Ok(Some(Self::#pascal_name)) }
}
}
fn expand_variant_call(
&self,
result_macro_name: &Path,
call_name: &proc_macro2::Ident,
) -> proc_macro2::TokenStream {
let pascal_name = &self.pascal_name;
let name = &self.name;
let matcher = if self.has_normal_args {
let names = self
.args
.iter()
.filter(|a| !a.is_special())
.map(|a| &a.name);
quote! {{
#(
#names,
)*
}}
} else {
quote! {}
};
let receiver = match self.mutability {
Mutability::Mutable | Mutability::View => quote! {self.},
Mutability::Pure => quote! {Self::},
};
let args = self.args.iter().map(MethodArg::expand_call_arg);
quote! {
#call_name::#pascal_name #matcher => {
#[allow(deprecated)]
let result = #receiver #name(
#(
#args,
)*
);
let result = #result_macro_name!(result);
result.map(|post| {
<Self as ::evm_coder::Contract>::map_post(post, |res| {
let mut writer = ::evm_coder::abi::AbiWriter::default();
<_ as AbiWrite>::abi_write(&res, &mut writer);
writer
})
})
}
}
}
fn expand_custom_signature(&self) -> proc_macro2::TokenStream {
let mut args = TokenStream::new();
let mut has_params = false;
for arg in self.args.iter().filter(|a| !a.is_special()) {
has_params = true;
let ty = &arg.ty;
args.extend(quote! {nameof(<#ty>::SIGNATURE)});
args.extend(quote! {fixed(",")});
}
if has_params {
args.extend(quote! {shift_left(1)});
}
let func_name = self.camel_name.clone();
quote! { ::evm_coder::make_signature!(new fixed(#func_name) fixed("(") #args fixed(")")) }
}
fn expand_solidity_function(&self) -> proc_macro2::TokenStream {
let camel_name = &self.camel_name;
let mutability = match self.mutability {
Mutability::Mutable => quote! {SolidityMutability::Mutable},
Mutability::View => quote! { SolidityMutability::View },
Mutability::Pure => quote! {SolidityMutability::Pure},
};
let result = &self.result;
let args = self
.args
.iter()
.filter(|a| !a.is_special())
.map(MethodArg::expand_solidity_argument);
let docs = &self.docs;
let screaming_name = &self.screaming_name;
let hide = self.hide;
let custom_signature = self.expand_custom_signature();
let is_payable = self.has_value_args;
quote! {
SolidityFunction {
docs: &[#(#docs),*],
hide: #hide,
selector: u32::from_be_bytes(Self::#screaming_name),
custom_signature: #custom_signature,
name: #camel_name,
mutability: #mutability,
is_payable: #is_payable,
args: (
#(
#args,
)*
),
result: <UnnamedArgument<#result>>::default(),
}
}
}
}
fn generics_list(gen: &Generics) -> proc_macro2::TokenStream {
if gen.params.is_empty() {
return quote! {};
}
let params = gen.params.iter().map(|p| match p {
syn::GenericParam::Type(id) => {
let v = &id.ident;
quote! {#v}
}
syn::GenericParam::Lifetime(lt) => {
let v = <.lifetime;
quote! {#v}
}
syn::GenericParam::Const(c) => {
let i = &c.ident;
quote! {#i}
}
});
quote! { #(#params),* }
}
fn generics_reference(gen: &Generics) -> proc_macro2::TokenStream {
if gen.params.is_empty() {
return quote! {};
}
let list = generics_list(gen);
quote! { <#list> }
}
fn generics_stub(gen: &Generics) -> proc_macro2::TokenStream {
if gen.params.is_empty() {
return quote! {};
}
let params = (0..gen.params.len()).map(|_| quote! {()});
quote! {<#(#params,)*>}
}
fn generics_data(gen: &Generics) -> proc_macro2::TokenStream {
let list = generics_list(gen);
if gen.params.len() == 1 {
quote! {#list}
} else {
quote! { (#list) }
}
}
pub struct SolidityInterface {
generics: Generics,
name: Box<syn::Type>,
result_macro_name: Path,
info: InterfaceInfo,
methods: Vec<Method>,
docs: Vec<String>,
}
impl SolidityInterface {
pub fn try_from(info: InterfaceInfo, value: &mut ItemImpl) -> syn::Result<Self> {
let mut methods = Vec::new();
for item in &mut value.items {
if let ImplItem::Method(method) = item {
methods.push(Method::try_from(method, &info.enum_variant_attrs)?);
}
}
let mut docs = vec![];
for attr in &value.attrs {
let ident = parse_ident_from_path(&attr.path, false)?;
if ident == "doc" {
let args = attr.parse_meta().unwrap();
let value = match args {
Meta::NameValue(MetaNameValue {
lit: Lit::Str(str), ..
}) => str.value(),
_ => unreachable!(),
};
docs.push(value);
}
}
let mut result_macro_name = parse_path(&value.self_ty)?.clone();
if let Some(last) = result_macro_name.segments.iter_mut().last() {
last.ident = format_ident!("{}_result", &last.ident);
last.arguments = PathArguments::None;
}
Ok(Self {
generics: value.generics.clone(),
name: value.self_ty.clone(),
result_macro_name,
info,
methods,
docs,
})
}
#[allow(clippy::too_many_lines)]
pub fn expand(self) -> proc_macro2::TokenStream {
let name = self.name;
let solidity_name = self.info.name.to_string();
let call_name = pascal_ident_to_call(&self.info.name);
let generics = self.generics;
let gen_ref = generics_reference(&generics);
let gen_data = generics_data(&generics);
let gen_stub = generics_stub(&generics);
let gen_where = &generics.where_clause;
let call_sub = self
.info
.inline_is
.0
.iter()
.chain(self.info.is.0.iter())
.map(|c| Is::expand_call_def(c, &gen_ref));
let call_parse = self
.info
.inline_is
.0
.iter()
.chain(self.info.is.0.iter())
.map(|is| Is::expand_parse(is, &gen_ref));
let call_variants = self
.info
.inline_is
.0
.iter()
.chain(self.info.is.0.iter())
.map(|c| Is::expand_variant_call(c, &call_name, &gen_ref));
let inline_interface_id = self.info.inline_is.0.iter().map(Is::expand_interface_id);
let supports_interface = self
.info
.is
.0
.iter()
.map(|is| Is::expand_supports_interface(is, &gen_ref));
let calls = self.methods.iter().map(Method::expand_call_def);
let consts = self.methods.iter().map(Method::expand_const);
let interface_id = self.methods.iter().map(Method::expand_interface_id);
let parsers = self.methods.iter().map(Method::expand_parse);
let call_variants_this = self
.methods
.iter()
.map(|m| Method::expand_variant_call(m, &self.result_macro_name, &call_name));
let solidity_functions = self.methods.iter().map(Method::expand_solidity_function);
let solidity_is = self
.info
.is
.0
.iter()
.chain(self.info.inline_is.0.iter())
.map(|is| is.name.to_string());
let solidity_events_is = self.info.events.0.iter().map(|is| is.name.to_string());
let solidity_generators = self
.info
.is
.0
.iter()
.chain(self.info.inline_is.0.iter())
.map(|is| Is::expand_generator(is, &gen_ref));
let solidity_event_generators = self.info.events.0.iter().map(Is::expand_event_generator);
let solidity_events_idents = self.info.events.0.iter().map(|is| is.name.clone());
let docs = &self.docs;
let enum_attrs = &self.info.enum_attrs;
let expect_selector = self.info.expect_selector.map(|s| {
quote! {
const _: () = assert!(#s == u32::from_be_bytes(<#call_name #gen_stub>::interface_id()), "selector mismatch, review contained function selectors");
}
});
quote! {
#(
const _: ::core::marker::PhantomData<#solidity_events_idents> = ::core::marker::PhantomData;
)*
#[derive(Debug)]
#(#[doc = #docs])*
#(#[#enum_attrs])*
pub enum #call_name #gen_ref {
ERC165Call(::evm_coder::ERC165Call, ::core::marker::PhantomData<#gen_data>),
#(
#calls,
)*
#(
#call_sub,
)*
}
#expect_selector
impl #gen_ref #call_name #gen_ref {
#(
#consts
)*
pub const fn interface_id() -> ::evm_coder::types::Bytes4 {
let mut interface_id = 0;
#(#interface_id)*
#(#inline_interface_id)*
u32::to_be_bytes(interface_id)
}
#[cfg(feature = "stubgen")]
pub fn generate_solidity_interface(tc: &evm_coder::solidity::TypeCollector, is_impl: bool) {
use evm_coder::solidity::*;
use core::fmt::Write;
let interface = SolidityInterface {
docs: &[#(#docs),*],
name: #solidity_name,
selector: Self::interface_id(),
is: &["Dummy", "ERC165", #(
#solidity_is,
)* #(
#solidity_events_is,
)* ],
functions: (#(
#solidity_functions,
)*),
};
let mut out = ::evm_coder::types::String::new();
if #solidity_name.starts_with("Inline") {
out.push_str("/// @dev inlined interface\n");
}
let _ = interface.format(is_impl, &mut out, tc);
tc.collect(out);
#(
#solidity_event_generators
)*
#(
#solidity_generators
)*
if is_impl {
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());
} else {
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());
}
}
}
impl #gen_ref ::evm_coder::Call for #call_name #gen_ref {
fn parse(method_id: ::evm_coder::types::Bytes4, reader: &mut ::evm_coder::abi::AbiReader) -> ::evm_coder::abi::Result<Option<Self>> {
use ::evm_coder::abi::AbiRead;
match method_id {
::evm_coder::ERC165Call::INTERFACE_ID => return Ok(
::evm_coder::ERC165Call::parse(method_id, reader)?
.map(|c| Self::ERC165Call(c, ::core::marker::PhantomData))
),
#(
#parsers,
)*
_ => {},
}
#(
#call_parse
)else*
return Ok(None);
}
}
impl #generics #call_name #gen_ref
#gen_where
{
pub fn supports_interface(this: &#name, interface_id: ::evm_coder::types::Bytes4) -> bool {
interface_id != u32::to_be_bytes(0xffffff) && (
interface_id == ::evm_coder::ERC165Call::INTERFACE_ID ||
interface_id == Self::interface_id()
#(
|| #supports_interface
)*
)
}
}
impl #generics ::evm_coder::Callable<#call_name #gen_ref> for #name
#gen_where
{
#[allow(unreachable_code)] fn call(&mut self, c: ::evm_coder::types::Msg<#call_name #gen_ref>) -> ::evm_coder::ResultWithPostInfoOf<Self, ::evm_coder::abi::AbiWriter> {
use ::evm_coder::abi::AbiWrite;
match c.call {
#(
#call_variants,
)*
#call_name::ERC165Call(::evm_coder::ERC165Call::SupportsInterface {interface_id}, _) => {
let mut writer = ::evm_coder::abi::AbiWriter::default();
writer.bool(&<#call_name #gen_ref>::supports_interface(self, interface_id));
return Ok(<Self as ::evm_coder::Contract>::with_default_post(writer.into()));
}
_ => {},
}
match c.call {
#(
#call_variants_this,
)*
_ => Err(<Self as ::evm_coder::Contract>::with_default_post("method is not available".into())),
}
}
}
}
}
}