0

I am trying to get the latest ETH price from the Chainlink priceFeed contract. The response shows up like this 171617306235 as a BigNumber. So there is a couple of issues for me to be solved.

1 - My local variables are uint256. How can I assign the Chainlink response ( int256 ) to a uint256 variable?

2 - As evident, some calculations need to be done on the Chainlink response to make it a human-readable ETH price. How can I achieve this goal? For instance, how I should read something like $1716.17 as the Ethereum price within my JS or Typescript file?

Thanks in advance

2 Answers 2

1

To add to Ali's answer

  • the decimals in a price feed's answer can be obtained programmatically as part of the API response reference
  • the number of decimals can also be manually from the contract addresses reference page .... just click on "Show More Details" next to the relevant network.

If you're getting data from the Chainlink API and the API returns int256 for the answer (i.e. price see reference) , maybe you should change your local variables to int256. Or you can try and use one of these sorts of libraries to safely to convert to uint256.

The question is whether you really need the price data to be a uint -- and if so cast/convert later rather than at the API fetching stage.

1

The returned price from chainlink data feeds contains 8 additional digits along with the price for example 1716 17306235

If you only want to price and not the additional digits then

uint256 price = uint256(171617306235) / 100000000;

This will return you 1716

And if you want also eight digits after the price you can get like this

uint256 eightDigits = 171617306235 % 100000000;

It will return you 17306235

And if you want only two digits after price as you mentioned in the question, then

uint256 twoDigits = eightDigits / 1000000;

it will give you 17

After that, you can combine them and get your desired results.

3
  • Thanks for your answer, but uint256 cannot be assigned to a int256! Jul 31 at 9:27
  • Chainlink response is a int256 and your variable is uint256. Jul 31 at 9:28
  • This is called type casting, I use this in my projects so you can also. Aug 1 at 5:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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