0

I'm a little new to Web3, and in NodeJS I'm trying to create a deposit-aware system. Today I'm doing the following:

ETH Transactions: Get it through getBlock, and check whether the "to" of the transaction belongs to me or not Transactions Tokens: Obtained through getPastEvents, as in the code below:

const events = await tokenContract.getPastEvents('Transfer', {
    filter: { to: walletsChunk },
    fromBlock,
    toBlock,
    maxBlockResults: 10000
});

So I have the following problems:

  • For some reason, sometimes I have the impression that getPastEvents does not return all transactions, I don't know if it could be some address limit or block range, or even another error
  • Performance, I have doubts if this is the best way to get transactions to verify they belong to me.
  • The most critical thing is that this type of transaction exists: https://etherscan.io/tx/0x4bad5eceba8501b62b9307c002957eb2a9ac5eb4ee8bc39340c801eb2abc93da. I don't know how I can get this transaction, just having the address of a wallet which is present in the transaction
2
  • 1
    You can solve this by using the Moralis API. If you are okay with using other API's I can share the solution.
    – JohnVersus
    Commented Sep 5, 2023 at 5:53
  • @JohnVersus I use the infura API, and would like to continue using it. But I accept all possible suggestions. Commented Sep 5, 2023 at 13:01

2 Answers 2

1

So to use eth_getBlockReceipts with web3.js, you need to use the extend method that allows you to basically 'extend' the class and use any RPC method.

Note that it only works in web3.js V1. It was deprecated in V4. So you need to install V1.9.0

npm i [email protected]   

Then you can use it like this:

/* 
eth_getBlockReceipts is not available in Web3.js
Create a custom class using web3.extend to implement it
*/

const Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

web3.extend({
    property: 'eth',
    methods: [{
      name: 'getBlockReceipts',
      call: 'eth_getBlockReceipts',
      params: 1,
      inputFormatter: [web3.extend.formatters.inputDefaultBlockNumberFormatter],
      outputFormatter: null
    }]
  });

async function getBlockReceipts(blockId) {
  const receipts = await web3.eth.getBlockReceipts(blockId)
  console.log(receipts)
}
  
getBlockReceipts("latest")

Just replace your node URL.

Remember, it only works on the Erigon client! Chainstack offers archive Erigon nodes on the Growth plan.

You can find a full reference in the Chainstack docs | eth_getBlockReceipts

Full disclosure: I'm a dev advocate at Chainstack.

0

For free API's there's a limit of 1000 blocks . You can use alchemy's API if you think getblock isnt working. If your scope of work is only limited to finding out whether the to address belongs to you or not then i think this code is good . The transaction which you are viewing is of Coinbase and no events was emitted in this transaction i have checked on tenderly as well.

6
  • Understanding the suggestion to check the "to". Today I use Infura, but even using a paid version, using getBlock is heavy for me, because I have to use it too because I have to use getTransactionReceipt to get all the transaction logs and check if any addresses belong to me. I think the best way would be to somehow get a log of filtered transactions, determining whether the addresses I have are included in the transaction or not. Commented Sep 5, 2023 at 13:00
  • Hi! You could use the eth_getBlockReceipts method to get all the tx receipts from a singe call if you want to go the 'checking each block' route, or you can use an API like Covalent. eth_getBlockReceipts only works on the Erigon client which is not supported by Infura as far as I know. I can give give you an overview in an answer if you are willing to check another provider. Getting logs will only show events emitted from smart contracts so if you need to pull transactions that don't come from smart contracts it won't work, plus is super heavy for the node so providers limit quite heavily. Commented Sep 5, 2023 at 13:47
  • Got it @Dave Dev Lawyer Chainstack. In that case, to use eth_getBlockReceipts would I still use the web3js lib? Would I just change my node? Or would I have to use an external API Commented Sep 6, 2023 at 11:05
  • yeah you can use web3.js, ethers, or a regular POST request. You just need to use a node running in Erigon. Chainstack offers Erigon archive nodes but not on the free plan, unfortunately. You can check this doc about eth_getBlockReceipt > docs.chainstack.com/reference/ethereum-getblockreceipts < or this guide > docs.chainstack.com/docs/… < I made both of those. Feel free to reach out if you need help or suggestions! Commented Sep 6, 2023 at 12:32
  • 1
    Perfect @Dave Dev Advocate Chainstack, I think this might suit me, thank you very much. I think my question is how to use this in web3js. I couldn't find examples of how to make this type of request in NodeJS, the closest was a Python example from QuickNode. (Could you answer me as an Answer?) py: from web3 import Web3, HTTPProvider 2w3 = Web3(HTTPProvider('docs-demo.quiknode.pro/')) 3params = "0xEDA8CE" 4request = w3.provider.make_request('eth_getBlockReceipts', [params]) 5print(request) Commented Sep 6, 2023 at 20:17

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.