-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
abi.go
159 lines (135 loc) · 4.51 KB
/
abi.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package evm
import (
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/smartcontractkit/ocr2keepers/pkg/chain"
"github.com/smartcontractkit/ocr2keepers/pkg/types"
)
type evmRegistryPackerV2_0 struct {
abi abi.ABI
}
func NewEvmRegistryPackerV2_0(abi abi.ABI) *evmRegistryPackerV2_0 {
return &evmRegistryPackerV2_0{abi: abi}
}
func (rp *evmRegistryPackerV2_0) UnpackCheckResult(key types.UpkeepKey, raw string) (types.UpkeepResult, error) {
b, err := hexutil.Decode(raw)
if err != nil {
return types.UpkeepResult{}, err
}
out, err := rp.abi.Methods["checkUpkeep"].Outputs.UnpackValues(b)
if err != nil {
return types.UpkeepResult{}, fmt.Errorf("%w: unpack checkUpkeep return: %s", err, raw)
}
result := types.UpkeepResult{
Key: key,
State: types.Eligible,
}
upkeepNeeded := *abi.ConvertType(out[0], new(bool)).(*bool)
rawPerformData := *abi.ConvertType(out[1], new([]byte)).(*[]byte)
result.FailureReason = *abi.ConvertType(out[2], new(uint8)).(*uint8)
result.GasUsed = *abi.ConvertType(out[3], new(*big.Int)).(**big.Int)
result.FastGasWei = *abi.ConvertType(out[4], new(*big.Int)).(**big.Int)
result.LinkNative = *abi.ConvertType(out[5], new(*big.Int)).(**big.Int)
if !upkeepNeeded {
result.State = types.NotEligible
} else {
var ret0 = new(res)
err = pdataABI.UnpackIntoInterface(ret0, "check", rawPerformData)
if err != nil {
return types.UpkeepResult{}, err
}
result.CheckBlockNumber = ret0.Result.CheckBlockNumber
result.CheckBlockHash = ret0.Result.CheckBlockhash
result.PerformData = ret0.Result.PerformData
}
// This is a default placeholder which is used since we do not get the execute gas
// from checkUpkeep result. This field is overwritten later from the execute gas
// we have for an upkeep in memory. TODO (AUTO-1482): Refactor this
result.ExecuteGas = 5_000_000
return result, nil
}
func (rp *evmRegistryPackerV2_0) UnpackPerformResult(raw string) (bool, error) {
b, err := hexutil.Decode(raw)
if err != nil {
return false, err
}
out, err := rp.abi.Methods["simulatePerformUpkeep"].
Outputs.UnpackValues(b)
if err != nil {
return false, fmt.Errorf("%w: unpack simulatePerformUpkeep return: %s", err, raw)
}
return *abi.ConvertType(out[0], new(bool)).(*bool), nil
}
func (rp *evmRegistryPackerV2_0) UnpackUpkeepResult(id *big.Int, raw string) (activeUpkeep, error) {
b, err := hexutil.Decode(raw)
if err != nil {
return activeUpkeep{}, err
}
out, err := rp.abi.Methods["getUpkeep"].Outputs.UnpackValues(b)
if err != nil {
return activeUpkeep{}, fmt.Errorf("%w: unpack getUpkeep return: %s", err, raw)
}
type upkeepInfo struct {
Target common.Address
ExecuteGas uint32
CheckData []byte
Balance *big.Int
Admin common.Address
MaxValidBlocknumber uint64
LastPerformBlockNumber uint32
AmountSpent *big.Int
Paused bool
OffchainConfig []byte
}
temp := *abi.ConvertType(out[0], new(upkeepInfo)).(*upkeepInfo)
au := activeUpkeep{
ID: id,
PerformGasLimit: temp.ExecuteGas,
CheckData: temp.CheckData,
}
return au, nil
}
func (rp *evmRegistryPackerV2_0) UnpackTransmitTxInput(raw []byte) ([]types.UpkeepResult, error) {
out, err := rp.abi.Methods["transmit"].Inputs.UnpackValues(raw)
if err != nil {
return nil, fmt.Errorf("%w: unpack TransmitTxInput return: %s", err, raw)
}
if len(out) < 2 {
return nil, fmt.Errorf("invalid unpacking of TransmitTxInput in %s", raw)
}
decodedReport, err := chain.NewEVMReportEncoder().DecodeReport(out[1].([]byte))
if err != nil {
return nil, fmt.Errorf("error during decoding report while unpacking TransmitTxInput: %w", err)
}
return decodedReport, nil
}
var (
// rawPerformData is abi encoded tuple(uint32, bytes32, bytes). We create an ABI with dummy
// function which returns this tuple in order to decode the bytes
pdataABI, _ = abi.JSON(strings.NewReader(`[{
"name":"check",
"type":"function",
"outputs":[{
"name":"ret",
"type":"tuple",
"components":[
{"type":"uint32","name":"checkBlockNumber"},
{"type":"bytes32","name":"checkBlockhash"},
{"type":"bytes","name":"performData"}
]
}]
}]`,
))
)
type res struct {
Result performDataStruct
}
type performDataStruct struct {
CheckBlockNumber uint32 `abi:"checkBlockNumber"`
CheckBlockhash [32]byte `abi:"checkBlockhash"`
PerformData []byte `abi:"performData"`
}