2

Apologies if this is a daft question but from some searching on Google I'm not able to find the answers I'm looking for (maybe I'm not searching for the right thing).

I want to be able to access the price data for a specific token (doesn't matter which, needs to be user specified), at a particular block from Eth's on-chain data.

I.e. I want to supply a block number and a token address and I want to get back the price that specific token had at that block.

Is this possible? And if so is it possible through ethers.js or do I need to use something else?

1
  • Usually contracts do not have historical prices to consult on-chain. You could store the prices you will be using in a contract if you want.
    – Ismael
    Jan 31 at 7:07

1 Answer 1

3

You should have archive node. Only then you can query the blockchain at specific block number. Regarding the prices you have 2 options:

  1. Oracles, like Chainlink's price feeds. For example, call the latestAnswer at specific block number in contract 0x5f4ec3df9cbd43714fe2740f5e3616155c5b8419 und you'll get the ETH price.

  2. Not for all tokens you find oracles and price feeds, then you have to query the DEXes yourself, like Uniswap. Again, at a specific block get the reserves for the pair and its ratio gives you the price. The reserves are in wei, so convert to ether, as tokens may have different decimals values.


This should answer the question, but I have also decided to write an article about this: https://dev.kit.eco/ethereum-get-token-price-at-a-specific-block-onchain

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.