2

I am trying to price arbitrary ERC-20 tokens at past blocks, and it turns out this is a challenging problem. Currently, I query the Uniswap v3 subgraph at a past block, but this doesn't always return a value, particularly if a pool wasn't created at that block. Some pegged assets don't have Uniswap pools at all, like cWBTC and aWBTC.

This led me to try to deduce (in code) if an asset I cannot price is pegged to another asset I can price by using the 1inch spot-price-aggregator. This works pretty well, although cWBTC is not pegged 1:1 to WBTC. The 1inch spot price aggregator does however have a price value for it, so I tried to make a web3 call on the contract at a past block. This worked for block 18200000 but not for block 17200000. I went to etherscan to look at the aggregator contract and sure enough it was deployed between these two block numbers. The contract didn't exist at 17200000, but why can't it execute on past blockchain data? Is there an alternative? I'm trying to go as far back as block 12369620.

For reference, here is my code:

from web3 import Web3
from etherscan.contracts import Contract

w3 = Web3(Web3.HTTPProvider('https://eth-mainnet.g.alchemy.com/v2/~~~apikey~~~'))

address = w3.to_checksum_address('0x0AdDd25a91563696D8567Df78D5A01C9a991F9B8')

api = Contract(address = address,api_key = 'mykey')

abi = api.get_abi()

contract = w3.eth.contract(address = address, abi = abi)

tokenAddress = w3.to_checksum_address('0xccf4429db6322d5c611ee964527d42e5d685dd6a')

val =contract.functions.getRateToEth(tokenAddress,True).call(block_identifier = 18203687)

error:

web3.exceptions.BadFunctionCallOutput: Could not decode contract function call to getRateToEth with return data: b'', output_types: ['uint256']

1 Answer 1

1

Given the issues you've encountered, I'd like to suggest two potential solutions:

  1. Consider using an earlier version of the spot-price-aggregator. It might have its drawbacks; older versions weren't modified without reason, but it could be a viable option for many tokens. Previous versions of the aggregator can be found in the README of the repository. It's worth noting that older versions might not have the same accuracy or support as the latest one, but they could serve well for your specific use case.

  2. Another approach is to use a tool like Hardhat to create a local fork of the blockchain at a specific block. You can then deploy the latest version of the spot-price-aggregator in this fork to determine the prices. And since you're operating on a fork from a specific block N, the blockchain state won't change while the spot-price-aggregator is deployed and the prices are being determined. Local fork ensures that no extraneous transactions interfere with your price determination process in the local fork even after several blocks.

Both of these solutions have their drawbacks and aren't necessarily the most convenient or universal. However, they can be quite effective for determining the prices of the tokens you're interested in at a specific block. If you come across any more straightforward solutions, please do not hesitate to share them here. Your insights could be invaluable to others facing similar challenges.

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.