[Ethereum] How to get value of input parameters from Transaction history

historytransactions

I would like to read a successful transaction on an ETH block and I would like to get the values of input parameters that were passed to the function for that transaction. I am not talking of the

data

field in the transaction. And I am aware of using TransactionReceipt from web3 in my webapp to look at other details of this transaction.

For example, I have a method in the contract:

 function setName(string n) {.....} 

Now when this function was called as

contract.setName("Abcde")....

Now sometime in future as part of audit, I would like to use the transaction id and read transaction details and read the value "Abcde" that was passed to this function for that particular transaction.

I want to know if this is possible and if it is, how do I do it.

Best Answer

I am not talking of the data field in the transaction.

Well, you are, actually. The data field is the input parameters. What you need to know is how to decode them. It's well worth studying the Ethereum ABI if you really want to understand this.

For tools to help, have a look at web3.eth.abi (check you are using v1.0 or higher of Web3). Unfortunately this doesn't do the whole job for you - it could be extended to do so - but it can help.

In your example, your function call will have a signature as follows:

> web3.eth.abi.encodeFunctionSignature('setName(string)');
'0xc47f0027'

So, if you find a data field that begins c47f0027, you know that it is a call to your setName function and that the remaining data is the string parameter.

In your example, the string data will look like this: 000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000054162636465000000000000000000000000000000000000000000000000000000. This is the ABI string representation of "Abcde").

So you extract this from the data and feed it into Web3 to decode it as follows:

> web3.eth.abi.decodeParameter('string','000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000054162636465000000000000000000000000000000000000000000000000000000');
'Abcde'

Summary

To summarise your example, the full data field in the transaction would be "0xc47f0027000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000054162636465000000000000000000000000000000000000000000000000000000".

  • The first 8 hex characters, "c47f0027" (ignore the 0x if present), mean that this is a call to your setName(string n) function.

  • The remaining data is your string parameter and can be decoded using Web3 as above.

Appendix

These libraries look to automate the above, but I haven't tested them: