-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
abi.go
196 lines (164 loc) · 5.61 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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"
ocr2keepers "github.com/smartcontractkit/chainlink-automation/pkg/v2"
)
type evmRegistryPackerV2_0 struct {
abi abi.ABI
}
// enum UpkeepFailureReason
// https://github.com/smartcontractkit/chainlink/blob/d9dee8ea6af26bc82463510cb8786b951fa98585/contracts/src/v0.8/interfaces/AutomationRegistryInterface2_0.sol#L94
const (
UPKEEP_FAILURE_REASON_NONE = iota
UPKEEP_FAILURE_REASON_UPKEEP_CANCELLED
UPKEEP_FAILURE_REASON_UPKEEP_PAUSED
UPKEEP_FAILURE_REASON_TARGET_CHECK_REVERTED
UPKEEP_FAILURE_REASON_UPKEEP_NOT_NEEDED
UPKEEP_FAILURE_REASON_PERFORM_DATA_EXCEEDS_LIMIT
UPKEEP_FAILURE_REASON_INSUFFICIENT_BALANCE
)
func NewEvmRegistryPackerV2_0(abi abi.ABI) *evmRegistryPackerV2_0 {
return &evmRegistryPackerV2_0{abi: abi}
}
func (rp *evmRegistryPackerV2_0) UnpackCheckResult(key ocr2keepers.UpkeepKey, raw string) (EVMAutomationUpkeepResult20, error) {
var (
result EVMAutomationUpkeepResult20
)
b, err := hexutil.Decode(raw)
if err != nil {
return result, err
}
out, err := rp.abi.Methods["checkUpkeep"].Outputs.UnpackValues(b)
if err != nil {
return result, fmt.Errorf("%w: unpack checkUpkeep return: %s", err, raw)
}
block, id, err := splitKey(key)
if err != nil {
return result, err
}
result = EVMAutomationUpkeepResult20{
Block: uint32(block.Uint64()),
ID: id,
Eligible: true,
}
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.Eligible = false
}
// if NONE we expect the perform data. if TARGET_CHECK_REVERTED we will have the error data in the perform data used for off chain lookup
if result.FailureReason == UPKEEP_FAILURE_REASON_NONE || (result.FailureReason == UPKEEP_FAILURE_REASON_TARGET_CHECK_REVERTED && len(rawPerformData) > 0) {
var ret0 = new(performDataWrapper)
err = pdataABI.UnpackIntoInterface(ret0, "check", rawPerformData)
if err != nil {
return result, 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) ([]ocr2keepers.UpkeepResult, error) {
var (
enc = EVMAutomationEncoder20{}
decoded []ocr2keepers.UpkeepResult
out []interface{}
err error
b []byte
ok bool
)
if out, err = rp.abi.Methods["transmit"].Inputs.UnpackValues(raw); 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)
}
if b, ok = out[1].([]byte); !ok {
return nil, fmt.Errorf("unexpected value type in transaction")
}
if decoded, err = enc.DecodeReport(b); err != nil {
return nil, fmt.Errorf("error during decoding report while unpacking TransmitTxInput: %w", err)
}
return decoded, 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 performDataWrapper struct {
Result performDataStruct
}
type performDataStruct struct {
CheckBlockNumber uint32 `abi:"checkBlockNumber"`
CheckBlockhash [32]byte `abi:"checkBlockhash"`
PerformData []byte `abi:"performData"`
}