This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
/
abi_generator.go
119 lines (112 loc) · 3.08 KB
/
abi_generator.go
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
// Copyright (c) Gabriel de Quadros Ligneul
// SPDX-License-Identifier: MIT (see LICENSE)
package compiler
import (
"encoding/json"
"fmt"
)
type jsonAbiMethod struct {
Name string `json:"name"`
Type string `json:"type"`
StateMutability string `json:"stateMutability"`
Inputs []jsonAbiArg `json:"inputs"`
Outputs []jsonAbiArg `json:"outputs"`
}
type jsonAbiArg struct {
Name string `json:"name"`
Type string `json:"type"`
InternalType string `json:"internalType"`
Components []jsonAbiArg `json:"components"`
}
// Generate the JSON ABI for the AST.
// This function converts struct to tuples and message schemas to solidity functions.
func generateAbi(ast astSchema) []byte {
methods := generateAbiMethods(nil, ast.Reports, ast.Structs)
methods = generateAbiMethods(methods, ast.Advances, ast.Structs)
methods = generateAbiMethods(methods, ast.Inspects, ast.Structs)
result, err := json.MarshalIndent(methods, "", " ")
if err != nil {
panic(fmt.Sprintf("json marshal error: %v", err))
}
return result
}
// Generate the methods and append them to the slice
func generateAbiMethods(
methods []jsonAbiMethod,
messages []messageSchema,
structs []messageSchema,
) []jsonAbiMethod {
for _, message := range messages {
var method jsonAbiMethod
method.Name = message.Name
method.Type = "function"
method.StateMutability = "nonpayable"
for _, field := range message.Fields {
arg := generateAbiArg(field.Name, field.type_, structs)
method.Inputs = append(method.Inputs, arg)
}
methods = append(methods, method)
}
return methods
}
// Recursively generate the ABI argument given the type.
func generateAbiArg(name string, type_ any, structs []messageSchema) jsonAbiArg {
switch type_ := type_.(type) {
case typeBool:
return jsonAbiArg{
Name: name,
Type: "bool",
InternalType: "bool",
}
case typeInt:
prefix := ""
if !type_.Signed {
prefix = "u"
}
typeName := fmt.Sprintf("%vint%v", prefix, type_.Bits)
return jsonAbiArg{
Name: name,
Type: typeName,
InternalType: typeName,
}
case typeAddress:
return jsonAbiArg{
Name: name,
Type: "address",
InternalType: "address",
}
case typeBytes:
return jsonAbiArg{
Name: name,
Type: "bytes",
InternalType: "bytes",
}
case typeString:
return jsonAbiArg{
Name: name,
Type: "string",
InternalType: "string",
}
case typeArray:
elemType := generateAbiArg(name, type_.Elem, structs)
elemType.Type += "[]"
elemType.InternalType += "[]"
return elemType
case typeStructRef:
struct_ := structs[type_.Index]
var components []jsonAbiArg
for _, field := range struct_.Fields {
arg := generateAbiArg(field.Name, field.type_, structs)
components = append(components, arg)
}
return jsonAbiArg{
Name: name,
Type: "tuple",
InternalType: "struct " + struct_.Name,
Components: components,
}
default:
// This should not happen
panic(fmt.Errorf("invalid type: %T", type_))
}
}