[Ethereum] Decoding transaction input data with web3j (Java)

abigo-ethereumjavaweb3j

My use case is the following:
I would like to have a daemon monitoring how users are interacting with my smart contract.
The way I see it is to look at incoming blocks and try to decode input data with a to address corresponding to my contract.
I was unable to find in web3J a way to decode data.

1- Is there any way with web3J to decode the data ?

2- Have I any other way to achieve my goal than decode the input data ?
Events seems to me as an hard choice as I will not be able to easily monitor every things. And this methods cannot apply to smart contract that I don't own.

Thanks for the help.

Best Answer

I guess sort of for the first question. If you look at what a transaction is, to a node its just a big long byte string. If the bytes are convertible to ASCII, you can use use the

web3.toAscii()

That may or may not give you something. For instance, here's a tx a I did on Ropsten where you get some readable input data back and some non-readable data: https://ropsten.etherscan.io/tx/0xe4058b8f612f20600d8aae1230d93b7b5c63398ddde0a3a6aed236659f425c0e (just click the input Convert to Ascii at the bottom)

That said, the best (easiest) way to do this is to own the dapp. So when they fill out a form, you just save the input data. The next best like you said is to use Events. They are annoying, but you could have just one long event at the beginning of your contracts:

    contract Test {
       event Input(string desc, address _party, uint _input1, string _input2);  

     function somefunc(uint _input1, string _input2) returns(bool success){
        Input("Input",msg.sender,_input1,_input2);
      return true;
   }
}

But then for other people's contracts your out of luck since you don't have this event.

Related Topic