|
| 1 | +package ccipevm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/ethereum/go-ethereum/common" |
| 9 | + "github.com/smartcontractkit/chainlink-common/pkg/types" |
| 10 | + ccipcommon "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/common" |
| 11 | + evmconfig "github.com/smartcontractkit/chainlink/v2/core/capabilities/ccip/configs/evm" |
| 12 | + evmrelaytypes "github.com/smartcontractkit/chainlink/v2/core/services/relay/evm/types" |
| 13 | +) |
| 14 | + |
| 15 | +// ChainCWProvider is a struct that implements the ChainRWProvider interface for EVM chains. |
| 16 | +type ChainCWProvider struct{} |
| 17 | + |
| 18 | +// GetChainReader returns a new ContractReader for EVM chains. |
| 19 | +func (g ChainCWProvider) GetChainReader(ctx context.Context, params ccipcommon.ChainReaderProviderOpts) (types.ContractReader, error) { |
| 20 | + var chainReaderConfig evmrelaytypes.ChainReaderConfig |
| 21 | + if params.ChainID == params.DestChainID { |
| 22 | + chainReaderConfig = evmconfig.DestReaderConfig |
| 23 | + } else { |
| 24 | + chainReaderConfig = evmconfig.SourceReaderConfig |
| 25 | + } |
| 26 | + |
| 27 | + if !params.Ofc.CommitEmpty() && params.Ofc.Commit.PriceFeedChainSelector == params.ChainSelector { |
| 28 | + params.Lggr.Debugw("Adding feed reader config", "chainID", params.ChainID) |
| 29 | + chainReaderConfig = evmconfig.MergeReaderConfigs(chainReaderConfig, evmconfig.FeedReaderConfig) |
| 30 | + } |
| 31 | + |
| 32 | + if isUSDCEnabled(params.Ofc) { |
| 33 | + params.Lggr.Debugw("Adding USDC reader config", "chainID", params.ChainID) |
| 34 | + chainReaderConfig = evmconfig.MergeReaderConfigs(chainReaderConfig, evmconfig.USDCReaderConfig) |
| 35 | + } |
| 36 | + |
| 37 | + if params.ChainID == params.HomeChainID { |
| 38 | + params.Lggr.Debugw("Adding home chain reader config", "chainID", params.ChainID) |
| 39 | + chainReaderConfig = evmconfig.MergeReaderConfigs(chainReaderConfig, evmconfig.HomeChainReaderConfigRaw) |
| 40 | + } |
| 41 | + |
| 42 | + marshaledConfig, err := json.Marshal(chainReaderConfig) |
| 43 | + if err != nil { |
| 44 | + return nil, fmt.Errorf("failed to marshal chain reader config: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + cr, err := params.Relayer.NewContractReader(ctx, marshaledConfig) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + |
| 52 | + return cr, nil |
| 53 | +} |
| 54 | + |
| 55 | +// GetChainWriter returns a new ContractWriter for EVM chains. |
| 56 | +func (g ChainCWProvider) GetChainWriter(ctx context.Context, params ccipcommon.ChainWriterProviderOpts) (types.ContractWriter, error) { |
| 57 | + var fromAddress common.Address |
| 58 | + transmitter, ok := params.Transmitters[types.NewRelayID(params.ChainFamily, params.ChainID)] |
| 59 | + if ok { |
| 60 | + fromAddress = common.HexToAddress(transmitter[0]) |
| 61 | + } |
| 62 | + |
| 63 | + evmConfig, err := evmconfig.ChainWriterConfigRaw( |
| 64 | + fromAddress, |
| 65 | + defaultCommitGasLimit, |
| 66 | + params.ExecBatchGasLimit) |
| 67 | + if err != nil { |
| 68 | + return nil, fmt.Errorf("failed to create EVM chain writer config: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + chainWriterConfig, err := json.Marshal(evmConfig) |
| 72 | + if err != nil { |
| 73 | + return nil, fmt.Errorf("failed to marshal EVM chain writer config: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + cw, err := params.Relayer.NewContractWriter(ctx, chainWriterConfig) |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("failed to create chain writer for chain %s: %w", params.ChainID, err) |
| 79 | + } |
| 80 | + |
| 81 | + return cw, nil |
| 82 | +} |
| 83 | + |
| 84 | +func isUSDCEnabled(ofc ccipcommon.OffChainConfig) bool { |
| 85 | + if ofc.ExecEmpty() { |
| 86 | + return false |
| 87 | + } |
| 88 | + |
| 89 | + return ofc.Execute.IsUSDCEnabled() |
| 90 | +} |
0 commit comments