/
function.go
61 lines (50 loc) · 1.65 KB
/
function.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
// Package abi provides tools for building and parsing Ethereum ABI (Application Binary Interface) data.
package abi
import (
"fmt"
"github.com/unpackdev/solgo/ir"
)
// processFunction processes an IR function and returns a Method representation of it.
// It extracts the name, input, and output parameters of the function, sets its type to "function",
// and normalizes its state mutability.
func (b *Builder) processFunction(unit *ir.Function) (*Method, error) {
toReturn := &Method{
Name: unit.GetName(),
Inputs: make([]MethodIO, 0),
Outputs: make([]MethodIO, 0),
Type: "function",
StateMutability: b.normalizeStateMutability(unit.GetStateMutability()),
}
for _, parameter := range unit.GetParameters() {
if parameter.GetTypeDescription() == nil {
return nil, fmt.Errorf("nil type description for function parameter %s", parameter.GetName())
}
methodIo := MethodIO{
Name: parameter.GetName(),
}
toReturn.Inputs = append(
toReturn.Inputs,
b.buildMethodIO(methodIo, parameter.GetTypeDescription()),
)
}
for _, parameter := range unit.GetReturnStatements() {
if parameter.GetTypeDescription() == nil {
return nil, fmt.Errorf("nil type description for function return parameter %s", parameter.GetName())
}
methodIo := MethodIO{
Name: parameter.GetName(),
}
toReturn.Outputs = append(
toReturn.Outputs,
b.buildMethodIO(methodIo, parameter.GetTypeDescription()),
)
}
return toReturn, nil
}
func (b *Builder) GetFunctionAsABI(unit *ir.Function) ([]*Method, error) {
method, err := b.processFunction(unit)
if err != nil {
return nil, err
}
return []*Method{method}, nil
}