How to read information from ethereum transaction using transactionHash in web3?

12,056

Solution 1

There are two ways to get output from the smart contracts

  • You read the state directly using the smart contract ABI and calls

  • The transaction contains Solidity events which you can parse from the transaction logs

For both cases the support has to be written to the smart contract and in this question the smart contract code itself is missing, so it is not possible to tell exactly how to do it.

Generally, nodes do not support reading historical state. They support reading historical event logs though, so if you need to access the data over time series then you need to write your smart contract so that it emits events.

Solution 2

It's a bit late, but maybe my answer will help others struggling with this.

When you make a transaction via a smart contract function call, the input parameters are ABI.encoded into the transaction data, along with the function selector, which is the first 4bytes of the hash of the function signature.

For example, calling function set(string memory str) with str = 'hello' will result in the following transaction data:

0x4ed3885e0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000

Where 4ed3885e is the function selector and the rest is the input parameter encoded.

To my knowledge there are two ways of decoding input parameters from transaction data.

I'll paste an example for decoding 4 input parameters, using both methods.

Web3 example:

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('https...')); // your web3 provider

var transaction = '0xe7ad9ce53fd1d1559dc6ca57e67ed5c8cd612b6264ddc4f78b0736ad24a5bc29';

web3.eth.getTransaction(transaction, function(err, tx){
    let tx_data = tx.input;
    let input_data = '0x' + tx_data.slice(10);  // get only data without function selector

    let params = web3.eth.abi.decodeParameters(['bytes32', 'string', 'string', 'string'], input_data);
    console.log(params);
});

Abi-decoder example:

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('https...'));  // your web3 provider
var abiDecoder = require('abi-decoder');

var ABI = ... ;   // abi of your contract
var transaction = '0xe7ad9ce53fd1d1559dc6ca57e67ed5c8cd612b6264ddc4f78b0736ad24a5bc29';

web3.eth.getTransaction(transaction, function(err, tx){
    abiDecoder.addABI(ABI);
    let tx_data = tx.input;

    let decoded_data = abiDecoder.decodeMethod(tx_data);
    let params = decoded_data.params;

    let param_values = [];
    for(i in params){  // loop to print parameters without unnecessary info
      param_values.push(params[i].name + " : " + params[i].value);
    }
    console.log(param_values);
});
Share:
12,056

Related videos on Youtube

Sanket Shevkar
Author by

Sanket Shevkar

I am a third-year Computer Science and Engineering student from Symbiosis Institute of Technology.

Updated on May 31, 2022

Comments

  • Sanket Shevkar
    Sanket Shevkar almost 2 years

    Say I had updated a variable x as(x=10) inside the smart contract. I stored the transactionHash. Then I again changed the value of x=20 then I'll get a new transaction hash. So is there any way using the transactionHash of the earlier updation(x=10) with which I can see the what value of x it was?