-
Notifications
You must be signed in to change notification settings - Fork 11
/
decoder.go
45 lines (40 loc) · 894 Bytes
/
decoder.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
package ethmonitor
import (
"errors"
"github.com/ethereum/go-ethereum/accounts/abi"
"strings"
)
// Action 智能合约方法
type Action struct {
Method string // 合约方法
Inputs map[string]interface{} // 合约入参及对应的value
}
type abiDecoder struct {
abi abi.ABI
}
func newAbiDecoder(abiStr string) *abiDecoder {
a, err := abi.JSON(strings.NewReader(abiStr))
if err != nil {
panic(err)
}
return &abiDecoder{a}
}
func (d *abiDecoder) DecodeTxData(txData []byte) (*Action, error) {
if len(txData) < 4 {
return nil, errors.New("illegal tx")
}
method, err := d.abi.MethodById(txData[0:4])
if err != nil {
return nil, err
}
inputsMap := map[string]interface{}{}
err = method.Inputs.UnpackIntoMap(inputsMap, txData[4:])
if err != nil {
return nil, err
}
act := &Action{
Method: method.Name,
Inputs: inputsMap,
}
return act, nil
}