Skip to content

Commit

Permalink
Use TokenDecimals in on chain reader
Browse files Browse the repository at this point in the history
Signed-off-by: asoliman <[email protected]>
  • Loading branch information
asoliman92 committed Aug 19, 2024
1 parent 9c4952d commit dc73503
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
1 change: 1 addition & 0 deletions commit/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (p *PluginFactory) NewReportingPlugin(config ocr3types.ReportingPluginConfi
onChainTokenPricesReader = reader.NewOnchainTokenPricesReader(
tokenPricesCr,
offchainConfig.PriceSources,
offchainConfig.TokenDecimals,
)
}

Expand Down
27 changes: 6 additions & 21 deletions internal/reader/onchain_prices_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ type OnchainTokenPricesReader struct {
// Reader for the chain that will have the token prices on-chain
ContractReader commontyps.ContractReader
PriceSources map[types.Account]pluginconfig.ArbitrumPriceSource
TokenDecimals map[types.Account]uint8
}

func NewOnchainTokenPricesReader(
contractReader commontyps.ContractReader,
priceSources map[types.Account]pluginconfig.ArbitrumPriceSource,
tokenDecimals map[types.Account]uint8,
) *OnchainTokenPricesReader {
return &OnchainTokenPricesReader{
ContractReader: contractReader,
PriceSources: priceSources,
TokenDecimals: tokenDecimals,
}
}

Expand Down Expand Up @@ -66,12 +69,12 @@ func (pr *OnchainTokenPricesReader) GetTokenPricesUSD(
if err != nil {
return fmt.Errorf("failed to get token price for %s: %w", token, err)
}
decimals, err := pr.getFeedDecimals(ctx, token)
if err != nil {
decimals, ok := pr.TokenDecimals[token]
if !ok {
return fmt.Errorf("failed to get decimals for %s: %w", token, err)
}

prices[idx] = calculateUsdPer1e18TokenAmount(rawTokenPrice, *decimals)
prices[idx] = calculateUsdPer1e18TokenAmount(rawTokenPrice, decimals)
return nil
})
}
Expand Down Expand Up @@ -111,24 +114,6 @@ func (pr *OnchainTokenPricesReader) getRawTokenPrice(ctx context.Context, token
return latestRoundData.Answer, nil
}

func (pr *OnchainTokenPricesReader) getFeedDecimals(ctx context.Context, token types.Account) (*uint8, error) {
var decimals *uint8
if err :=
pr.ContractReader.GetLatestValue(
ctx,
consts.ContractNamePriceAggregator,
consts.MethodNameGetDecimals,
primitives.Unconfirmed,
nil,
&decimals,
//boundContract,
); err != nil {
return nil, fmt.Errorf("decimals call failed for token %s: %w", token, err)
}

return decimals, nil
}

// Input price is USD per full token, with 18 decimal precision
// Result price is USD per 1e18 of smallest token denomination, with 18 decimal precision
// Examples:
Expand Down
11 changes: 9 additions & 2 deletions pluginconfig/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,15 @@ type CommitOffchainConfig struct {

// PriceSources is a map of Arbitrum price sources for each token.
// Note that the token address is that on the remote chain.
PriceSources map[types.Account]ArbitrumPriceSource `json:"priceSources"`
TokenDecimals map[types.Account]uint8 `json:"decimals"`
PriceSources map[types.Account]ArbitrumPriceSource `json:"priceSources"`

// TokenDecimals is a map of token decimals for each token.
// As **not** each node supports both the
// 1. Token price feed chain (where we get the token price in USD)
// 2. Destination chain (where we can get the token decimals from PriceRegistry).
// So to be able to calculate the effective price we need both token decimals.
// This is why we need to store the token decimals in the config.
TokenDecimals map[types.Account]uint8 `json:"decimals"`

// TokenPriceChainSelector is the chain selector for the chain on which
// the token prices are read from.
Expand Down

0 comments on commit dc73503

Please sign in to comment.