forked from taikoxyz/taiko-mono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
345 lines (274 loc) · 8.86 KB
/
types.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package relayer
import (
"context"
"fmt"
"math/big"
"time"
"log/slog"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
"github.com/taikoxyz/taiko-mono/packages/relayer/bindings/bridge"
"github.com/taikoxyz/taiko-mono/packages/relayer/bindings/erc1155vault"
"github.com/taikoxyz/taiko-mono/packages/relayer/bindings/erc20vault"
"github.com/taikoxyz/taiko-mono/packages/relayer/bindings/erc721vault"
)
var (
ZeroHash = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")
ZeroAddress = common.HexToAddress("0x0000000000000000000000000000000000000000")
)
// IsInSlice determines whether v is in slice s
func IsInSlice[T comparable](v T, s []T) bool {
for _, e := range s {
if v == e {
return true
}
}
return false
}
type confirmer interface {
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
BlockNumber(ctx context.Context) (uint64, error)
}
// WaitReceipt keeps waiting until the given transaction has an execution
// receipt to know whether it was reverted or not.
func WaitReceipt(ctx context.Context, confirmer confirmer, txHash common.Hash) (*types.Receipt, error) {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
slog.Info("waiting for transaction receipt", "txHash", txHash.Hex())
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-ticker.C:
receipt, err := confirmer.TransactionReceipt(ctx, txHash)
if err != nil {
continue
}
if receipt.Status != types.ReceiptStatusSuccessful {
return nil, fmt.Errorf("transaction reverted, hash: %s", txHash)
}
slog.Info("transaction receipt found", "txHash", txHash.Hex())
return receipt, nil
}
}
}
// WaitConfirmations won't return before N blocks confirmations have been seen
// on destination chain.
func WaitConfirmations(ctx context.Context, confirmer confirmer, confirmations uint64, txHash common.Hash) error {
slog.Info("beginning waiting for confirmations", "txHash", txHash.Hex())
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
receipt, err := confirmer.TransactionReceipt(ctx, txHash)
if err != nil {
if err == ethereum.NotFound {
continue
}
slog.Error("encountered error getting receipt", "txHash", txHash.Hex(), "error", err)
return err
}
latest, err := confirmer.BlockNumber(ctx)
if err != nil {
return err
}
want := receipt.BlockNumber.Uint64() + confirmations
slog.Info(
"waiting for confirmations",
"txHash", txHash.Hex(),
"confirmations", confirmations,
"blockNumWillbeConfirmed", want,
"latestBlockNum", latest,
)
if latest < receipt.BlockNumber.Uint64()+confirmations {
continue
}
slog.Info("done waiting for confirmations", "txHash", txHash.Hex(), "confirmations", confirmations)
return nil
}
}
}
// DecodeMessageSentData tries to tell if it's an ETH, ERC20, ERC721, or ERC1155 bridge,
// which lets the processor look up whether the contract has already been deployed or not,
// to help better estimate gas needed for processing the message.
func DecodeMessageSentData(event *bridge.BridgeMessageSent) (EventType, CanonicalToken, *big.Int, error) {
eventType := EventTypeSendETH
var canonicalToken CanonicalToken
var amount *big.Int = big.NewInt(0)
erc20ReceiveTokensFunctionSig := "cb03d23c"
erc721ReceiveTokensFunctionSig := "a9976baf"
erc1155ReceiveTokensFunctionSig := "20b81559"
// try to see if its an ERC20
if event.Message.Data != nil && common.BytesToHash(event.Message.Data) != ZeroHash {
functionSig := event.Message.Data[:4]
if common.Bytes2Hex(functionSig) == erc20ReceiveTokensFunctionSig {
erc20VaultMD := bind.MetaData{
ABI: erc20vault.ERC20VaultABI,
}
erc20VaultABI, err := erc20VaultMD.GetAbi()
if err != nil {
return eventType, nil, big.NewInt(0), errors.Wrap(err, "erc20VaultMD.GetAbi()")
}
method, err := erc20VaultABI.MethodById(event.Message.Data[:4])
if err != nil {
return eventType, nil, big.NewInt(0), errors.Wrap(err, "tokenVaultABI.MethodById")
}
inputsMap := make(map[string]interface{})
if err := method.Inputs.UnpackIntoMap(inputsMap, event.Message.Data[4:]); err != nil {
return eventType, nil, big.NewInt(0), errors.Wrap(err, "method.Inputs.UnpackIntoMap")
}
if method.Name == "receiveToken" {
eventType = EventTypeSendERC20
// have to unpack to anonymous struct first due to abi limitation
t := inputsMap["ctoken"].(struct {
// nolint
ChainId *big.Int `json:"chainId"`
Addr common.Address `json:"addr"`
Decimals uint8 `json:"decimals"`
Symbol string `json:"symbol"`
Name string `json:"name"`
})
canonicalToken = CanonicalERC20{
ChainId: t.ChainId,
Addr: t.Addr,
Decimals: t.Decimals,
Symbol: t.Symbol,
Name: t.Name,
}
amount = inputsMap["amount"].(*big.Int)
}
}
if common.Bytes2Hex(functionSig) == erc721ReceiveTokensFunctionSig {
erc721VaultMD := bind.MetaData{
ABI: erc721vault.ERC721VaultABI,
}
erc721VaultABI, err := erc721VaultMD.GetAbi()
if err != nil {
return eventType, nil, big.NewInt(0), errors.Wrap(err, "erc20VaultMD.GetAbi()")
}
method, err := erc721VaultABI.MethodById(event.Message.Data[:4])
if err != nil {
return eventType, nil, big.NewInt(0), errors.Wrap(err, "tokenVaultABI.MethodById")
}
inputsMap := make(map[string]interface{})
if err := method.Inputs.UnpackIntoMap(inputsMap, event.Message.Data[4:]); err != nil {
return eventType, nil, big.NewInt(0), errors.Wrap(err, "method.Inputs.UnpackIntoMap")
}
if method.Name == "receiveToken" {
eventType = EventTypeSendERC721
t := inputsMap["ctoken"].(struct {
// nolint
ChainId *big.Int `json:"chainId"`
Addr common.Address `json:"addr"`
Symbol string `json:"symbol"`
Name string `json:"name"`
})
canonicalToken = CanonicalNFT{
ChainId: t.ChainId,
Addr: t.Addr,
Symbol: t.Symbol,
Name: t.Name,
}
amount = big.NewInt(1)
}
}
if common.Bytes2Hex(functionSig) == erc1155ReceiveTokensFunctionSig {
erc1155VaultMD := bind.MetaData{
ABI: erc1155vault.ERC1155VaultABI,
}
erc1155VaultABI, err := erc1155VaultMD.GetAbi()
if err != nil {
return eventType, nil, big.NewInt(0), errors.Wrap(err, "erc1155VaultMD.GetAbi()")
}
method, err := erc1155VaultABI.MethodById(event.Message.Data[:4])
if err != nil {
return eventType, nil, big.NewInt(0), errors.Wrap(err, "tokenVaultABI.MethodById")
}
inputsMap := make(map[string]interface{})
if err := method.Inputs.UnpackIntoMap(inputsMap, event.Message.Data[4:]); err != nil {
return eventType, nil, big.NewInt(0), errors.Wrap(err, "method.Inputs.UnpackIntoMap")
}
if method.Name == "receiveToken" {
eventType = EventTypeSendERC1155
t := inputsMap["ctoken"].(struct {
// nolint
ChainId *big.Int `json:"chainId"`
Addr common.Address `json:"addr"`
Symbol string `json:"symbol"`
Name string `json:"name"`
})
canonicalToken = CanonicalNFT{
ChainId: t.ChainId,
Addr: t.Addr,
Symbol: t.Symbol,
Name: t.Name,
}
amounts := inputsMap["amounts"].([]*big.Int)
for _, v := range amounts {
amount = amount.Add(amount, v)
}
}
}
} else {
amount = event.Message.Value
}
return eventType, canonicalToken, amount, nil
}
type CanonicalToken interface {
ChainID() *big.Int
Address() common.Address
ContractName() string
TokenDecimals() uint8
ContractSymbol() string
}
type CanonicalERC20 struct {
// nolint
ChainId *big.Int `json:"chainId"`
Addr common.Address `json:"addr"`
Decimals uint8 `json:"decimals"`
Symbol string `json:"symbol"`
Name string `json:"name"`
}
func (c CanonicalERC20) ChainID() *big.Int {
return c.ChainId
}
func (c CanonicalERC20) Address() common.Address {
return c.Addr
}
func (c CanonicalERC20) ContractName() string {
return c.Name
}
func (c CanonicalERC20) ContractSymbol() string {
return c.Symbol
}
func (c CanonicalERC20) TokenDecimals() uint8 {
return c.Decimals
}
type CanonicalNFT struct {
// nolint
ChainId *big.Int `json:"chainId"`
Addr common.Address `json:"addr"`
Symbol string `json:"symbol"`
Name string `json:"name"`
}
func (c CanonicalNFT) ChainID() *big.Int {
return c.ChainId
}
func (c CanonicalNFT) Address() common.Address {
return c.Addr
}
func (c CanonicalNFT) ContractName() string {
return c.Name
}
func (c CanonicalNFT) TokenDecimals() uint8 {
return 0
}
func (c CanonicalNFT) ContractSymbol() string {
return c.Symbol
}