Skip to content

Commit

Permalink
ocr3 - Start using contract writer (#992)
Browse files Browse the repository at this point in the history
Start using the contract writer by implementing the gas prices ccip
reader method.
  • Loading branch information
dimkouv authored Jun 12, 2024
1 parent b556a07 commit 847bf07
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-impalas-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ccip": patch
---

#added gas fees implementation using contract writer interface
49 changes: 44 additions & 5 deletions core/services/ocr3/plugins/ccip/internal/reader/ccip.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package reader

import "C"
import (
"context"
"errors"
Expand All @@ -11,17 +10,29 @@ import (
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
"github.com/smartcontractkit/chainlink-common/pkg/types/query"
"github.com/smartcontractkit/chainlink-common/pkg/types/query/primitives"
"golang.org/x/sync/errgroup"
)

var (
ErrChainReaderNotFound = errors.New("chain reader not found")
ErrContractReaderNotFound = errors.New("contract reader not found")
ErrContractWriterNotFound = errors.New("contract writer not found")
)

// TODO: unit test the implementation when the actual contract reader and writer interfaces are finalized and mocks can be generated.
type CCIPChainReader struct {
contractReaders map[cciptypes.ChainSelector]types.ContractReader
contractWriters map[cciptypes.ChainSelector]types.ChainWriter
destChain cciptypes.ChainSelector
}

func NewCCIPChainReader(contractReaders map[cciptypes.ChainSelector]types.ContractReader, contractWriters map[cciptypes.ChainSelector]types.ChainWriter, destChain cciptypes.ChainSelector) *CCIPChainReader {
return &CCIPChainReader{
contractReaders: contractReaders,
contractWriters: contractWriters,
destChain: destChain,
}
}

func (r *CCIPChainReader) CommitReportsGTETimestamp(ctx context.Context, dest cciptypes.ChainSelector, ts time.Time, limit int) ([]cciptypes.CommitPluginReportWithMeta, error) {
if err := r.validateReaderExistence(dest); err != nil {
return nil, err
Expand Down Expand Up @@ -121,10 +132,28 @@ func (r *CCIPChainReader) NextSeqNum(ctx context.Context, chains []cciptypes.Cha
}

func (r *CCIPChainReader) GasPrices(ctx context.Context, chains []cciptypes.ChainSelector) ([]cciptypes.BigInt, error) {
if err := r.validateReaderExistence(chains...); err != nil {
if err := r.validateWriterExistence(chains...); err != nil {
return nil, err
}
panic("implement me")

eg := new(errgroup.Group)
gasPrices := make([]cciptypes.BigInt, len(chains))
for i, chain := range chains {
i, chain := i, chain
eg.Go(func() error {
gasPrice, err := r.contractWriters[chain].GetFeeComponents(ctx)
if err != nil {
return fmt.Errorf("failed to get gas price: %w", err)
}
gasPrices[i] = cciptypes.NewBigInt(&gasPrice.ExecutionFee)
return nil
})
}

if err := eg.Wait(); err != nil {
return nil, err
}
return gasPrices, nil
}

func (r *CCIPChainReader) Close(ctx context.Context) error {
Expand All @@ -135,7 +164,17 @@ func (r *CCIPChainReader) validateReaderExistence(chains ...cciptypes.ChainSelec
for _, ch := range chains {
_, exists := r.contractReaders[ch]
if !exists {
return fmt.Errorf("chain %d: %w", ch, ErrChainReaderNotFound)
return fmt.Errorf("chain %d: %w", ch, ErrContractReaderNotFound)
}
}
return nil
}

func (r *CCIPChainReader) validateWriterExistence(chains ...cciptypes.ChainSelector) error {
for _, ch := range chains {
_, exists := r.contractWriters[ch]
if !exists {
return fmt.Errorf("chain %d: %w", ch, ErrContractWriterNotFound)
}
}
return nil
Expand Down

0 comments on commit 847bf07

Please sign in to comment.