-
Notifications
You must be signed in to change notification settings - Fork 0
/
contract.go
69 lines (55 loc) · 1.54 KB
/
contract.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
package proto
import (
"fmt"
"github.com/airbloc/solgen/utils"
"github.com/ethereum/go-ethereum/accounts/abi"
)
type contract struct {
PackageName string
Services []Service
Messages []message
contractName string
typeOptions Options
}
const EmptyMessage = "google.protobuf.Empty"
func (c *contract) parseMessage(name string, comment string, args abi.Arguments) {
msg := &message{
Args: make([]argument, len(args)),
Comment: comment,
Name: name,
contractName: c.contractName,
typeOptions: c.typeOptions,
}
msg.parseArguments(args)
c.Messages = append(c.Messages, *msg)
}
func (c *contract) parseContract(contractAbi abi.ABI) {
service := Service{
Comment: c.contractName,
Name: c.contractName,
Methods: make([]method, len(contractAbi.Methods)),
}
methodIndex := 0
for methodName, methodInfo := range contractAbi.Methods {
inputMessage := fmt.Sprintf("Request%s", utils.Capitalize(methodName))
outputMessage := fmt.Sprintf("Response%s", utils.Capitalize(methodName))
if len(methodInfo.Inputs) == 0 {
inputMessage = EmptyMessage
} else {
c.parseMessage(inputMessage, methodInfo.Sig(), methodInfo.Inputs)
}
if len(methodInfo.Outputs) == 0 {
outputMessage = EmptyMessage
} else {
c.parseMessage(outputMessage, methodInfo.Sig(), methodInfo.Outputs)
}
service.Methods[methodIndex] = method{
Name: methodName,
Input: inputMessage,
Output: outputMessage,
}
methodIndex += 1
}
c.PackageName = utils.ToSnakeCase(c.contractName)
c.Services = []Service{service}
}