Search code examples
javascriptblockchainethereumethers.js

Unable to retrieve blockchain data from AlchemyProdiver through ethers.js


So, I have the following piece of code in my React Native application:

const network = 'goerli' // use rinkeby testnet
const provider = new ethers.providers.AlchemyProvider(network, API_KEY)

I am calling the following method to fetch the current block number on the goerli testnet:

provider.getBlockNumber().then(
    result => console.log(result)
).catch(
    error => console.log(error)
)

I am getting the following error on my console:

[Error: missing response (requestBody="{\"method\":\"eth_blockNumber\",\"params\":[],\"id\":48,\"jsonrpc\":\"2.0\"}", requestMethod="POST", serverError={"line":34814,"column":24,"sourceURL":"http://192.168.18.83:19000/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&hot=false"}, url="https://eth-goerli.alchemyapi.io/v2/API_KEY", code=SERVER_ERROR, version=web/5.6.1)]

The request shows up fine on my Alchemy dashboard and even gives me the output there, but I am unable to fetch any information from the request into my app and I have no idea why. For example, this is the response I am shown on my Alchemy dashboard:

{"jsonrpc":"2.0","id":47,"result":"0x71ade8"}

The response is totally fine but I am getting an error when using any functions that fetch the blockchain informattion


Solution

  • So, the API was returning the data correctly but my react native app wasn't recognizing it as a valid API call.

    In order to solve this problem, I simply downgraded the ethers library version to 4.0.49

    npm install [email protected]
    

    Now the API calls are working correctly and there is no error