2

In eth-lightwallet, there are two types of Tx creation functions;

txutils.valueTx(txObject)

and

txutils.functionTx(abi, functionName, args, txObject)

My guess is; although their inputs are very different, they both result in a Tx object that looks like this:

tx = {
  nonce: '..',
  gasPrice: '..', 
  gasLimit: '..',
  to: '..', 
  value: '..', 
  data: '...'
}

and the only difference is that data is empty in valueTx, and it's the hex-encoded version of the function call in functionTx. Is this correct? Is data the only difference between two?

Also, how can I compute data myself, given that I know the contract address, abi and the function name/parameters? Is there another library for this?

Thanks,

3

The difference between the two is that valueTx simply creates a transaction that sends a 'value' (ether/wei etc.) to a certain address on the blockchain, as opposed to functionTx that creates a transaction that executes a function on the blockchain (note that you can also pass a 'value' in this case along with the function call).

The 'data' argument is a hex representation of the arguments that a function receives, and there are a few different methods to create it.

I'm not sure how you use the library, but if you use web3, the easiest way in my opinion is to generate the data by calling getData on the function object.

For example, if you want to create the hex data that represents the arguments that you want to pass to a 'foo' function, you can do the following -

// for a given foo function in the contract -
// function foo(uint256 a, uint256 b) {}

// create a contract instance -
var contract = web3.eth.contract(contractAbi);

// generate the hex data for the arguments
var data = contract.foo.getData(100, 200);

// at this point, data value will be the hex string that
// represents the "100,  200" arguments.
// note that you might need to add the '0x' prefix to
// the data if it's not already there, some wallets are strict about it
  • Great! getData looks like what I'm looking for. So I assume I can send a 0-value valueTx (instead of a functionTx) just by adding data: contract.foo.getData(). – jeff Jan 23 '17 at 9:58
  • It depends on what you're trying to achieve - I don't see the value in using valueTx without passing on a value, since that would be equivalent to sending 0 wei to an address (unless that's what you're trying to do for some reason). In most cases, if you want to pass 0 value, it means you want to execute a function, and thus you will use functionTx. – nergall Jan 23 '17 at 12:06
  • I just want to use another transaction creation (and signing) library to make a function call, that's why I want to learn if they have the same structure. – jeff Jan 23 '17 at 20:38
  • Ah in that case, sure. You can simply generate the data using the contract.foo.getData() call and then pass that data along with the other args to the library you're using (with 0 value). – nergall Jan 24 '17 at 8:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.