-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathbatch_bhs.go
81 lines (70 loc) · 2.34 KB
/
batch_bhs.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
package blockhashstore
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/batch_blockhash_store"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
)
type batchBHSConfig interface {
EvmGasLimitDefault() uint32
}
type BatchBlockhashStore struct {
config batchBHSConfig
txm txmgr.TxManager
abi *abi.ABI
batchbhs batch_blockhash_store.BatchBlockhashStoreInterface
lggr logger.Logger
}
func NewBatchBHS(
config batchBHSConfig,
fromAddresses []ethkey.EIP55Address,
txm txmgr.TxManager,
batchbhs batch_blockhash_store.BatchBlockhashStoreInterface,
chainID *big.Int,
gethks keystore.Eth,
lggr logger.Logger,
) (*BatchBlockhashStore, error) {
abi, err := batch_blockhash_store.BatchBlockhashStoreMetaData.GetAbi()
if err != nil {
return nil, errors.Wrap(err, "building ABI")
}
return &BatchBlockhashStore{
config: config,
txm: txm,
abi: abi,
batchbhs: batchbhs,
lggr: lggr,
}, nil
}
func (b *BatchBlockhashStore) GetBlockhashes(ctx context.Context, blockNumbers []*big.Int) ([][32]byte, error) {
blockhashes, err := b.batchbhs.GetBlockhashes(&bind.CallOpts{Context: ctx}, blockNumbers)
if err != nil {
return nil, errors.Wrap(err, "getting blockhashes")
}
return blockhashes, nil
}
func (b *BatchBlockhashStore) StoreVerifyHeader(ctx context.Context, blockNumbers []*big.Int, blockHeaders [][]byte, fromAddress common.Address) error {
payload, err := b.abi.Pack("storeVerifyHeader", blockNumbers, blockHeaders)
if err != nil {
return errors.Wrap(err, "packing args")
}
_, err = b.txm.CreateEthTransaction(txmgr.NewTx{
FromAddress: fromAddress,
ToAddress: b.batchbhs.Address(),
EncodedPayload: payload,
GasLimit: b.config.EvmGasLimitDefault(),
Strategy: txmgr.NewSendEveryStrategy(),
}, pg.WithParentCtx(ctx))
if err != nil {
return errors.Wrap(err, "creating transaction")
}
return nil
}