This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
/
method.go
113 lines (100 loc) · 3.13 KB
/
method.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
// VulcanizeDB
// Copyright © 2019 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package types
import (
"fmt"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
// Method is our custom method struct
type Method struct {
Name string
Const bool
Args []Field
Return []Field
}
// Result is used to hold instance of result from method call with given inputs and block
type Result struct {
Method
Inputs []interface{} // Will only use addresses
Output interface{}
PgType string // Holds output pg type
Block int64
}
// NewMethod unpacks abi.Method into our custom Method struct
func NewMethod(m abi.Method) Method {
inputs := make([]Field, len(m.Inputs))
for i, input := range m.Inputs {
inputs[i] = Field{}
inputs[i].Name = input.Name
inputs[i].Type = input.Type
inputs[i].Indexed = input.Indexed
switch inputs[i].Type.T {
case abi.HashTy, abi.AddressTy:
inputs[i].PgType = "CHARACTER VARYING(66)"
case abi.IntTy, abi.UintTy:
inputs[i].PgType = "NUMERIC"
case abi.BoolTy:
inputs[i].PgType = "BOOLEAN"
case abi.BytesTy, abi.FixedBytesTy:
inputs[i].PgType = "BYTEA"
case abi.ArrayTy:
inputs[i].PgType = "TEXT[]"
case abi.FixedPointTy:
inputs[i].PgType = "MONEY" // use shopspring/decimal for fixed point numbers in go and money type in postgres?
default:
inputs[i].PgType = "TEXT"
}
}
outputs := make([]Field, len(m.Outputs))
for i, output := range m.Outputs {
outputs[i] = Field{}
outputs[i].Name = output.Name
outputs[i].Type = output.Type
outputs[i].Indexed = output.Indexed
switch outputs[i].Type.T {
case abi.HashTy, abi.AddressTy:
outputs[i].PgType = "CHARACTER VARYING(66)"
case abi.IntTy, abi.UintTy:
outputs[i].PgType = "NUMERIC"
case abi.BoolTy:
outputs[i].PgType = "BOOLEAN"
case abi.BytesTy, abi.FixedBytesTy:
outputs[i].PgType = "BYTEA"
case abi.ArrayTy:
outputs[i].PgType = "TEXT[]"
case abi.FixedPointTy:
outputs[i].PgType = "MONEY" // use shopspring/decimal for fixed point numbers in go and money type in postgres?
default:
outputs[i].PgType = "TEXT"
}
}
return Method{
Name: m.Name,
Const: m.Const,
Args: inputs,
Return: outputs,
}
}
// Sig returns the hash signature for the method
func (m Method) Sig() common.Hash {
types := make([]string, len(m.Args))
i := 0
for _, arg := range m.Args {
types[i] = arg.Type.String()
i++
}
return crypto.Keccak256Hash([]byte(fmt.Sprintf("%v(%v)", m.Name, strings.Join(types, ","))))
}