-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
} | ||
prices[idx] = price | ||
prices[idx] = latestRoundData.Answer |
There was a problem hiding this comment.
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.
13a5ad9
to
f942eea
Compare
Signed-off-by: asoliman <[email protected]>
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to normalize the price to 18 decimals before you call this function, see https://github.com/smartcontractkit/ccip/blob/5e1b6d179ca8428b52a025597381c32edecf37a7/core/services/ocr2/plugins/ccip/internal/pricegetter/evm.go#L175-L184
There was a problem hiding this comment.
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.
Signed-off-by: asoliman <[email protected]>
Signed-off-by: asoliman <[email protected]>
Signed-off-by: asoliman <[email protected]>
Signed-off-by: asoliman <[email protected]>
Signed-off-by: asoliman <[email protected]>
Signed-off-by: asoliman <[email protected]>
Signed-off-by: asoliman <[email protected]>
Signed-off-by: asoliman <[email protected]>
4cd7d97
to
6ef8ee4
Compare
Signed-off-by: asoliman <[email protected]>
There was a problem hiding this 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
Co-authored-by: Makram <[email protected]>
Co-authored-by: Makram <[email protected]>
Signed-off-by: asoliman <[email protected]>
Test Coverage
|
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.