-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
transmitter.go
99 lines (84 loc) · 2.94 KB
/
transmitter.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
package ocrcommon
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/v2/common/txmgr/types"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr"
"github.com/smartcontractkit/chainlink/v2/core/services/pg"
)
type roundRobinKeystore interface {
GetRoundRobinAddress(chainID *big.Int, addresses ...common.Address) (address common.Address, err error)
}
type txManager interface {
CreateTransaction(txRequest txmgr.TxRequest, qopts ...pg.QOpt) (tx txmgr.Tx, err error)
}
type Transmitter interface {
CreateEthTransaction(ctx context.Context, toAddress common.Address, payload []byte, txMeta *txmgr.TxMeta) error
FromAddress() common.Address
}
type transmitter struct {
txm txManager
fromAddresses []common.Address
gasLimit uint32
effectiveTransmitterAddress common.Address
strategy types.TxStrategy
checker txmgr.TransmitCheckerSpec
chainID *big.Int
keystore roundRobinKeystore
}
// NewTransmitter creates a new eth transmitter
func NewTransmitter(
txm txManager,
fromAddresses []common.Address,
gasLimit uint32,
effectiveTransmitterAddress common.Address,
strategy types.TxStrategy,
checker txmgr.TransmitCheckerSpec,
chainID *big.Int,
keystore roundRobinKeystore,
) (Transmitter, error) {
// Ensure that a keystore is provided.
if keystore == nil {
return nil, errors.New("nil keystore provided to transmitter")
}
return &transmitter{
txm: txm,
fromAddresses: fromAddresses,
gasLimit: gasLimit,
effectiveTransmitterAddress: effectiveTransmitterAddress,
strategy: strategy,
checker: checker,
chainID: chainID,
keystore: keystore,
}, nil
}
func (t *transmitter) CreateEthTransaction(ctx context.Context, toAddress common.Address, payload []byte, txMeta *txmgr.TxMeta) error {
roundRobinFromAddress, err := t.keystore.GetRoundRobinAddress(t.chainID, t.fromAddresses...)
if err != nil {
return errors.Wrap(err, "skipped OCR transmission, error getting round-robin address")
}
_, err = t.txm.CreateTransaction(txmgr.TxRequest{
FromAddress: roundRobinFromAddress,
ToAddress: toAddress,
EncodedPayload: payload,
FeeLimit: t.gasLimit,
ForwarderAddress: t.forwarderAddress(),
Strategy: t.strategy,
Checker: t.checker,
Meta: txMeta,
}, pg.WithParentCtx(ctx))
return errors.Wrap(err, "skipped OCR transmission")
}
func (t *transmitter) FromAddress() common.Address {
return t.effectiveTransmitterAddress
}
func (t *transmitter) forwarderAddress() common.Address {
for _, a := range t.fromAddresses {
if a == t.effectiveTransmitterAddress {
return common.Address{}
}
}
return t.effectiveTransmitterAddress
}