Skip to content

Commit

Permalink
Merge branch 'main' into solana/mcm-clear-root
Browse files Browse the repository at this point in the history
  • Loading branch information
jadepark-dev authored Dec 20, 2024
2 parents 880d236 + 8f67f1c commit b074ab3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
7 changes: 5 additions & 2 deletions pkg/reader/ccip.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,13 +1028,16 @@ func (r *ccipChainReader) getFeeQuoterTokenPriceUSD(ctx context.Context, tokenAd
)

if err != nil {
return cciptypes.BigInt{}, fmt.Errorf("failed to get LINK token price, addr: %v, err: %w", tokenAddr, err)
return cciptypes.BigInt{}, fmt.Errorf("failed to get token price, addr: %v, err: %w", tokenAddr, err)
}

price := timestampedPrice.Value

if price == nil {
return cciptypes.BigInt{}, fmt.Errorf("token price is nil, addr: %v", tokenAddr)
}
if price.Cmp(big.NewInt(0)) == 0 {
return cciptypes.BigInt{}, fmt.Errorf("LINK token price is 0, addr: %v", tokenAddr)
return cciptypes.BigInt{}, fmt.Errorf("token price is 0, addr: %v", tokenAddr)
}

return cciptypes.NewBigInt(price), nil
Expand Down
52 changes: 40 additions & 12 deletions pkg/reader/price_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,15 @@ func (pr *priceReader) GetFeedPricesUSD(
}

// Get price data
priceResult, err := contractResults[0].GetResult()
latestRoundData, err := pr.getPriceData(contractResults[0], boundContract)
if err != nil {
return nil, fmt.Errorf("get price for contract %s: %w", boundContract.Address, err)
}
latestRoundData, ok := priceResult.(*LatestRoundData)
if !ok {
return nil, fmt.Errorf("invalid price data type for contract %s", boundContract.Address)
return nil, err
}

// Get decimals
decimalResult, err := contractResults[1].GetResult()
decimals, err := pr.getDecimals(contractResults[1], boundContract)
if err != nil {
return nil, fmt.Errorf("get decimals for contract %s: %w", boundContract.Address, err)
}
decimals, ok := decimalResult.(*uint8)
if !ok {
return nil, fmt.Errorf("invalid decimals data type for contract %s", boundContract.Address)
return nil, err
}

// Normalize price for this contract
Expand All @@ -215,6 +207,42 @@ func (pr *priceReader) GetFeedPricesUSD(
return prices, nil
}

func (pr *priceReader) getPriceData(
result commontypes.BatchReadResult,
boundContract commontypes.BoundContract,
) (*LatestRoundData, error) {
priceResult, err := result.GetResult()
if err != nil {
return nil, fmt.Errorf("get price for contract %s: %w", boundContract.Address, err)
}
if priceResult == nil {
return nil, fmt.Errorf("priceResult value is nil for contract %s", boundContract.Address)
}
latestRoundData, ok := priceResult.(*LatestRoundData)
if !ok {
return nil, fmt.Errorf("invalid price data type for contract %s", boundContract.Address)
}
return latestRoundData, nil
}

func (pr *priceReader) getDecimals(
result commontypes.BatchReadResult,
boundContract commontypes.BoundContract,
) (*uint8, error) {
decimalResult, err := result.GetResult()
if err != nil {
return nil, fmt.Errorf("get decimals for contract %s: %w", boundContract.Address, err)
}
if decimalResult == nil {
return nil, fmt.Errorf("decimalResult value is nil for contract %s", boundContract.Address)
}
decimals, ok := decimalResult.(*uint8)
if !ok {
return nil, fmt.Errorf("invalid decimals data type for contract %s", boundContract.Address)
}
return decimals, nil
}

// prepareBatchRequest creates a batch request grouped by contract and returns the mapping of contracts to token indices
func (pr *priceReader) prepareBatchRequest(
tokens []ccipocr3.UnknownEncodedAddress,
Expand Down

0 comments on commit b074ab3

Please sign in to comment.