Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CCIP-2958] Token price reader implementation #67

Merged
merged 22 commits into from
Aug 20, 2024
Merged

Conversation

asoliman92
Copy link
Contributor

@asoliman92 asoliman92 commented Aug 14, 2024

Use Aggregator interface in price reader and get it ready for multi contract binding. Multi contract binding as well as fixing the tests will be in the next PR once we merge new chain reader changes that use binding instead of name.

}
prices[idx] = price
prices[idx] = latestRoundData.Answer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a couple of things we need to check before we return this price; we need to make sure that the price is in USD with 18 decimals, per 1e18 of the smallest token denomination. See https://github.com/smartcontractkit/ccip/blob/89519db20808d4210c1f9025d1355fd0b0076585/contracts/src/v0.8/ccip/PriceRegistry.sol#L152-L157 for details.

@asoliman92 asoliman92 marked this pull request as ready for review August 15, 2024 15:21
@asoliman92 asoliman92 changed the title Price reader Token price reader implementation Aug 15, 2024
@asoliman92 asoliman92 requested a review from makramkd August 15, 2024 15:22
internal/reader/onchain_prices_reader.go Show resolved Hide resolved
Comment on lines 65 to 71
rawTokenPrice, err := pr.getRawTokenPrice(ctx, token)
if err != nil {
return fmt.Errorf("failed to get token price for %s: %w", token, err)
}
decimals, err := pr.getTokenDecimals(ctx, token)
if err != nil {
return fmt.Errorf("failed to get decimals for %s: %w", token, err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you can use the BatchGetLatestValue API to do these calls as a batch call, it should be more efficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But they're different contracts for each token, What's the efficiency you mean here?

// 1 USDC = 1.00 USD per full token, each full token is 1e6 units -> 1 * 1e18 * 1e18 / 1e6 = 1e30
// 1 ETH = 2,000 USD per full token, each full token is 1e18 units -> 2000 * 1e18 * 1e18 / 1e18 = 2_000e18
// 1 LINK = 5.00 USD per full token, each full token is 1e18 units -> 5 * 1e18 * 1e18 / 1e18 = 5e18
func calculateUsdPer1e18TokenAmount(price *big.Int, decimals uint8) *big.Int {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm the token price isn't always to 18 decimals though, that depends on the feed.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is what the function is doing. It also shows in the tests, it's normalizing to 18 decimals depending on the decimals sent as a parameter.

pluginconfig/commit.go Show resolved Hide resolved
internal/reader/onchain_prices_reader_test.go Outdated Show resolved Hide resolved
internal/reader/onchain_prices_reader.go Show resolved Hide resolved
internal/reader/onchain_prices_reader.go Show resolved Hide resolved
pluginconfig/commit.go Outdated Show resolved Hide resolved
@makramkd makramkd requested review from winder and rstout August 19, 2024 17:23
@asoliman92 asoliman92 changed the title Token price reader implementation Token price reader implementation [CCIP-2958] Aug 20, 2024
@asoliman92 asoliman92 changed the title Token price reader implementation [CCIP-2958] [CCIP-2958] Token price reader implementation Aug 20, 2024
Copy link
Collaborator

@makramkd makramkd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost there - once these are addressed things LGTM

internal/reader/onchain_prices_reader_test.go Outdated Show resolved Hide resolved
pluginconfig/commit.go Outdated Show resolved Hide resolved
pluginconfig/commit.go Outdated Show resolved Hide resolved
pluginconfig/commit.go Show resolved Hide resolved
@asoliman92 asoliman92 enabled auto-merge (squash) August 20, 2024 12:17
Copy link

Test Coverage

Branch Coverage
price-reader 61.7%
ccip-develop 61.8%

@asoliman92 asoliman92 merged commit 1b2d9ea into ccip-develop Aug 20, 2024
4 checks passed
@mateusz-sekara mateusz-sekara deleted the price-reader branch November 8, 2024 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants