1

I'm trying to get Eth blocks by blockNum using Alchemy's apis.

The docs mention two methods: getBlockByHash and getBlockByNum, but those methods don't exist on the web3.eth object. Here are the methods listed on the web3.eth object that contain 'block':

'transactionConfirmationBlocks', 
'transactionBlockTimeout', 
'blockHeaderTimeout', 
'defaultBlock', 
'getBlockNumber', 
'getBlock', 
'getBlockTransactionCount', 
'getBlockUncleCount', 
'getTransactionFromBlock'

getBlock is shown in many code examples in the docs, including in the getBlockByNumber doc (https://docs.alchemy.com/alchemy/apis/ethereum/eth-getblockbynumber). But everywhere it is used in the docs just shows it called like const blockNumber = await web3.eth.getBlock('latest', true);

In that example it is only returning the blockNumber, which is something I already have. I'm trying to get metadata about a block using its blockNum, which the docs suggest is possible. I've called getBlock 4 different ways and they produce the following errors.

Method 1: getBlock(block_identifier=blockNum, full_transactions=True)

Error 1: FetchError: request to https://eth-mainnet.g.alchemy.io/v2/api_key failed, reason: getaddrinfo ENOTFOUND eth-mainnet.g.alchemy.io


Method 2: getBlockByHash({hash: "0xc0f4906fea23cf6f3cce98cb44e8e1449e455b28d684dfa9ff65426495584de6", boolean: "true"}) seen in docs (https://docs.alchemy.com/alchemy/apis/ethereum/eth-getblockbyhash)

Error 2: TypeError: web3.eth.getBlockByHash is not a function


Method 3: getBlock({ hash: hash, boolean: "true"})

Error 3: TypeError: web3.eth.getBlockByHash is not a function


Method 4: getBlock(hash)

Error 4: FetchError: request to https://eth-mainnet.g.alchemy.io/v2/api_key failed, reason: getaddrinfo ENOTFOUND eth-mainnet.g.alchemy.io


I've asked this question in a few places including Alchemys Discord. All I'm trying to is get transaction history (including block dates) from eth Contract addresses. I'd like to do this without having to run a full node locally, which is why Alchemy stuck out as a good option. I've tried BigQuery and the docs there are also super outdated.

Any suggestions of updated resources or how to run above function properly would be greatly appreciated.

1 Answer 1

2

this is the code you can run in web3.js. In this case this is eth_getBlockByNumber.

var Web3 = require('web3');
var node_URL = 'CHAINSTACK_NODE_URL';
var web3 = new Web3(node_URL);
web3.eth.getBlock('latest', 'True', (err, block) => {
    console.log(block)
})

This code was taken from Chainstack's docs, from the Node API reference page. There are many RPC examples (in web3.py as well).

Those errors could also be because of the node provider. Try with a different one, like Chainstack. (Remember that you will need an archive node if the blocks you are querying are older than the last 128.)

1
  • The API is shot. I ran getBlock using 'latest' which is the only argument that produces an actual block result. It even returns null if you pass the exact hash of the block returned in the 'latest' call.
    – T. Young
    Commented Aug 2, 2022 at 2:00

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.