0

I want to get smart contract functions return value sent by a specified smart contract within a range of blocks. I found a [manual] working method to achieve this:

async function getTxsByAccount(contractAddress) {
  // get current block number
  let blockNumber = await web3.eth.getBlockNumber();
  // get block data
  let block = await web3.eth.getBlock(blockNumber, true);
  // loop through each transaction
  block.transactions.forEach(tx => {
    // filter out transactions for a specific smart contract
    console.log((tx.to));
    if (contractAddress == tx.to) {
      console.log(tx);
    }
  });
}

getTxsByAccount('contractAddress');

However, this method is filtering if first getting block data and filtering transactions for a specified smart contract. It may become tedious with large block range. Furthermore, it requires additional processing to get returned values of functions because it only gives a transaction input. I also found another method from web3 API, 'call' method:

web3.eth.call({
    to: "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", // contract address
    data: "0xc6888fa10000000000000000000000000000000000000000000000000000000000000003"
})
.then(console.log);

> "0x000000000000000000000000000000000000000000000000000000000000000a"

By using this method, I can directly search for data sent by a specified smart contract without any filtering. But the API says:

Executes a message call transaction, which is directly executed in the VM of the node, but never mined into the blockchain.

That means we can only get the data for the transactions which are never mined into the blockchain. However, in my case transactions are already mined to the blockchain.

I would rephrase my question as: is there any direct method to get a smart contract (a specific smart contract) functions return value? or is it possible to use the call method to achieve this? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.