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']