Skip to content

Commit

Permalink
Enable token reader (#134)
Browse files Browse the repository at this point in the history
* Enable token reader

Signed-off-by: asoliman <[email protected]>

* Add logs and disable feequoter

Signed-off-by: asoliman <[email protected]>

* Report logs

Signed-off-by: asoliman <[email protected]>

---------

Signed-off-by: asoliman <[email protected]>
  • Loading branch information
asoliman92 authored Sep 19, 2024
1 parent 94ac9fd commit 8d485eb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
5 changes: 5 additions & 0 deletions commit/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ func (p *Plugin) Reports(seqNr uint64, outcomeBytes ocr3types.Outcome) ([]ocr3ty
if outcome.MerkleRootOutcome.OutcomeType != merkleroot.ReportGenerated {
return []ocr3types.ReportWithInfo[[]byte]{}, nil
}
p.lggr.Infow("generating report",
"roots", outcome.MerkleRootOutcome.RootsToReport,
"tokenPriceUpdates", outcome.TokenPriceOutcome.TokenPrices,
"gasPriceUpdates", outcome.ChainFeeOutcome.GasPrices,
)

rep := cciptypes.CommitPluginReport{
MerkleRoots: outcome.MerkleRootOutcome.RootsToReport,
Expand Down
5 changes: 3 additions & 2 deletions commit/tokenprice/observation.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (p *processor) ObserveFeedTokenPrices(ctx context.Context) []cciptypes.Toke
// If we couldn't fetch all prices log and return only the ones we could fetch
if len(tokenPrices) != len(tokensToQuery) {
p.lggr.Errorw("token prices length mismatch", "got", tokenPrices, "want", tokensToQuery)
return []cciptypes.TokenPrice{}
}

tokenPricesUSD := make([]cciptypes.TokenPrice, 0, len(tokenPrices))
Expand All @@ -75,14 +76,14 @@ func (p *processor) ObserveFeeQuoterTokenUpdates(ctx context.Context) map[types.
return map[types.Account]plugintypes.TimestampedBig{}
}
if !supportsDestChain {
p.lggr.Debugw("oracle does not support price registry observation", "oracleID", p.oracleID)
p.lggr.Debugw("oracle does not support fee quoter observation", "oracleID", p.oracleID)
return map[types.Account]plugintypes.TimestampedBig{}
}

tokensToQuery := maps.Keys(p.cfg.OffchainConfig.TokenInfo)
// sort tokens to query to ensure deterministic order
sort.Slice(tokensToQuery, func(i, j int) bool { return tokensToQuery[i] < tokensToQuery[j] })
p.lggr.Infow("observing price registry token updates")
p.lggr.Infow("observing fee quoter token updates")
priceUpdates, err := p.tokenPriceReader.GetFeeQuoterTokenUpdates(ctx, tokensToQuery)
if err != nil {
p.lggr.Errorw("call to GetFeeQuoterTokenUpdates failed", "err", err)
Expand Down
21 changes: 18 additions & 3 deletions commit/tokenprice/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,21 @@ func (p *processor) Observation(
return Observation{}, nil
}

feedTokenPrices := p.ObserveFeedTokenPrices(ctx)
feeQuoterUpdates := p.ObserveFeeQuoterTokenUpdates(ctx)
ts := time.Now().UTC()
p.lggr.Infow(
"observed token prices",
"feed prices", feedTokenPrices,
"fee quoter updates", feeQuoterUpdates,
"timestamp", ts,
)

return Observation{
FeedTokenPrices: p.ObserveFeedTokenPrices(ctx),
FeeQuoterTokenUpdates: p.ObserveFeeQuoterTokenUpdates(ctx),
FeedTokenPrices: feedTokenPrices,
FeeQuoterTokenUpdates: feeQuoterUpdates,
FChain: fChain,
Timestamp: time.Now().UTC(),
Timestamp: ts,
}, nil
}

Expand All @@ -86,6 +96,7 @@ func (p *processor) Outcome(
_ Query,
aos []plugincommon.AttributedObservation[Observation],
) (Outcome, error) {
p.lggr.Infow("processing token price outcome")
// If set to zero, no prices will be reported (i.e keystone feeds would be active).
if p.cfg.OffchainConfig.TokenPriceBatchWriteFrequency.Duration() == 0 {
p.lggr.Debugw("TokenPriceBatchWriteFrequency is set to zero, no prices will be reported")
Expand All @@ -98,6 +109,10 @@ func (p *processor) Outcome(
}

tokenPriceOutcome := p.selectTokensForUpdate(consensusObservation)
p.lggr.Infow(
"outcome token prices",
"token prices", tokenPriceOutcome,
)
return Outcome{
TokenPrices: tokenPriceOutcome,
}, nil
Expand Down
14 changes: 5 additions & 9 deletions internal/reader/onchain_prices_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ type OnchainTokenPricesReader struct {
ContractReader contractreader.ContractReaderFacade
TokenInfo map[types.Account]pluginconfig.TokenInfo
FeeQuoterAddress types.Account
// If not enabled just return empty prices
// TODO: Remove completely once integration tests are updated
enabled bool
// FeeQuoterEnabled Flag until we discover feeQuoter address and bind it correctly
feeQuoterEnabled bool
}

func NewOnchainTokenPricesReader(
Expand Down Expand Up @@ -71,10 +70,10 @@ func (pr *OnchainTokenPricesReader) GetFeeQuoterTokenUpdates(
ctx context.Context,
tokens []ocr2types.Account,
) (map[ocr2types.Account]plugintypes.TimestampedBig, error) {
var updates []plugintypes.TimestampedBig
if !pr.enabled {
return map[types.Account]plugintypes.TimestampedBig{}, nil
if !pr.feeQuoterEnabled {
return make(map[ocr2types.Account]plugintypes.TimestampedBig), nil
}
var updates []plugintypes.TimestampedBig

boundContract := commontypes.BoundContract{
Address: string(pr.FeeQuoterAddress),
Expand Down Expand Up @@ -108,9 +107,6 @@ func (pr *OnchainTokenPricesReader) GetFeeQuoterTokenUpdates(
func (pr *OnchainTokenPricesReader) GetTokenFeedPricesUSD(
ctx context.Context, tokens []ocr2types.Account,
) ([]*big.Int, error) {
if !pr.enabled {
return []*big.Int{}, nil
}
prices := make([]*big.Int, len(tokens))
eg := new(errgroup.Group)
for idx, token := range tokens {
Expand Down
6 changes: 3 additions & 3 deletions internal/reader/onchain_prices_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ func TestOnchainTokenPricesReader_GetTokenPricesUSD(t *testing.T) {
for _, tc := range testCases {
contractReader := createMockReader(t, tc.mockPrices, tc.errorAccounts, tc.tokenInfo)
tokenPricesReader := OnchainTokenPricesReader{
ContractReader: contractReader,
TokenInfo: tc.tokenInfo,
enabled: true,
ContractReader: contractReader,
TokenInfo: tc.tokenInfo,
feeQuoterEnabled: true,
}
t.Run(tc.name, func(t *testing.T) {
ctx := context.Background()
Expand Down

0 comments on commit 8d485eb

Please sign in to comment.