[Ethereum] with Web3.js, display sendTransaction data field from function call

calldatacontract-invocationweb3js

From js, I typically call a function with something like this:

theFunction=someAbi.at(0x1234...);
theFunction.someMethod(param1, param2, param3, 
    {"from": 0x111222..., "to": 0x1234..., "value":web3.toWei(2, 'ether')});

I would like to also display for the users how they would achieve the same manually, using the geth console, something like

web3.eth.sendTransaction({'from':0x111222..., 'to':0x1234,
    'value':web3.toWei(2, 'ether'), 'data': 0xXXXXXXX})

So, how do I get the 0xXXXXXXXX part from my original theFunction.someMethod(…) call? I know it can be calculated, but web3 can obviously do so already, so I don't want to reinvent the wheel.

Best Answer

Use getData.

Example:

// Get the call data, so you can call the contract through some other means
var myCallData = myContractInstance.myMethod.getData(param1 [, param2, ...]);
// myCallData = '0x45ff3ff6000000000004545345345345..'