Web3.js – Understanding web3.eth.sendTransaction Parameters

go-ethereumweb3js

I have been trying to use the sendTransaction function to transfer Ether from one account to another:

but the issue I am having is understanding the different parts of the transactionObject. From the github wiki, here is the explanation:

  • Explanation:

    Object – The transaction object to send:

    from: String – The address for the sending account. Uses the web3.eth.defaultAccount property, if not specified.

    to: String - (optional) The destination address of the message, left undefined for a contract-creation transaction.

    value: Number|String|BigNumber – (optional) The value transferred for the transaction in Wei, also the endowment if it's a contract-creation transaction.

    gas: Number|String|BigNumber – (optional, default: To-Be-Determined) The amount of gas to use for the transaction (unused gas is refunded).

    gasPrice: Number|String|BigNumber – (optional, default: To-Be-Determined) The price of gas for this transaction in wei, defaults to the mean network gas price.

    data: String – (optional) Either a byte string containing the associated data of the message, or in the case of a contract-creation transaction, the initialisation code.

    nonce: Number – (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.
    Function – (optional) If you pass a callback the HTTP request is made asynchronous. See this note for details.

so from this explanation, I would assume that something like this would be composed:

transactionObject = {
  from: "0xA0A0A0A01",
  to: "0xA0A0A0A02",
  value: web3.eth.getBalance("0xA0A0A0A01"),
  gas: "----",
  gasPrice: "----",
  data: "----",
  nonce: 0,
}

  • QUESTIONS/SITUATION:

I'm not sure about any of the values that are under the value value. Should they even be set? I understand that for instance gas is what is used as the currency for transactions over ethereum, but how do I know the maximum I can put the gas to be or the minimum? And what is the default: To-Be-Determined mean?

Best Answer

The gas price is what you're willing to pay for each unit of gas that your transaction will use. You can see statistics about the gas price being used here : https://etherscan.io/chart/gasprice I usually use around 20 Gwei when not in a hurry. If you leave it to default your node will try to use the best value.

The gas limit is the maximum amount of gas you're willing to spend. For a simple transaction (sending ether) you should be fine with 30,000. If your transaction is executing a contract then it's getting more complex, you need to know how much gas this contract might use.

So you could use the default value for the gas price, and 30000 as the gas.