Search code examples
ethereumsoliditysmartcontractsweb3jsweb3py

How to read Ethereum contract storage values which are not emitted?


I want to read the contract storage for '0x9EC55d57208cb28a7714A2eA3468bD9d5bB15125' at block 16368474 on BSC . I tried -

const Web3 = require('web3')
const rpcURL = "https://bsc-dataseed1.binance.org/"
const web3 = new Web3(rpcURL)

let contractAddress = '0x9EC55d57208cb28a7714A2eA3468bD9d5bB15125'
for (index = 0; index < 10; index++){
 console.log(`[${index}]` + 
   web3.eth.getStorageAt(contractAddress, index,16368474).then(res=>{ 
    console.log(res)
   }) 
   )
}

This runs into an error Error: Returned error: header not found .I tried changing blocks but run into the same error . The error only resolves if I don't pass 16368474 but that uses the latest block and that's not what I want . I tried the same with web3.py . It runs into the same error. What am I doing wrong ? Is there an alternate way of getting storage ?


Solution

  • Your code is valid - the issue is that the node provider doesn't support querying historic states.

    Solution: Use a different provider that supports archive node, e.g. Moralis.

    const Web3 = require("web3");
    const web3 = new Web3("https://speedy-nodes-nyc.moralis.io/<your_api_key>/bsc/mainnet/archive");
    const contractAddress = '0x9EC55d57208cb28a7714A2eA3468bD9d5bB15125';
    web3.eth.getStorageAt(contractAddress, 0, 16368474).then(res => {
        console.log(res);
    });