1

In uniswap to fetch the current price I simply fetch the reserves and then the price is given by TOKEN_RESERVES / WETH_RESERVES. I want to compare the price deviation from the chainlink price. However I am struggling with how chainlink formats its price calculation. How should I deal with decimals since token might or might not have 18 decimals?

We can fetch chainlink price using:

(, int256 price, , , ) = priceFeed.latestRoundData();

But how can I compare with the Uniswap price format?

2 Answers 2

1

You can get the number of decimals in the corresponding Chainlink Price Feed by calling the decimals view function, and then format the same to 18 decimals as per your requirement, like:

(, int256 price, , , ) = priceFeed.latestRoundData();
uint256 price_in_18_decimals = uint256(price) * 10 ** (18 - priceFeed.decimals());
1

Hope you are doing good! :)

You can normally get the decimal value in the price feed. There should be a decimal() function in the interface, so you can get the decimal value and format it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.