3

I want to send eth by using web3 on testnet. Source address is 0xbddf0bf3ac858d7fb8a2bdda55884d61779ba5a9. The destination is 0x8005ceb675d2ff8c989cc95354438b9fab568681.

I found the example on web3 reference page. I can imagine how to use it. However I don't know what is "data" and how to calculate the gasLimit.

var Tx = require('ethereumjs-tx');
var privateKey = new Buffer('xxx', 'hex')

var GAS = web3.eth.estimateGas({
    to: "0x8005ceb675d2ff8c989cc95354438b9fab568681", 
    data: "WHAT IS DATA"
});

var rawTx = {
  nonce: '0x00',
  gasPrice: GAS,
  gasLimit: 'HOW AMOUNT OF GAS SHOULD I SET',
  to: '0x8005ceb675d2ff8c989cc95354438b9fab568681',
  value: '0x01',
  data: 'WHAT IS DATA'
}

var tx = new Tx(rawTx);
tx.sign(privateKey);

var serializedTx = tx.serialize();
web3.eth.sendRawTransaction(serializedTx.toString('hex'), function(err, hash) {
  if (!err)
    console.log(hash);
});

https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendrawtransaction


Update 1

Seems data is optional value or something...

data - all of the interesting stuff goes here

What is the ethereum transaction data structure?

In Bitcoin, transaction fee is calculated by the size of transaction. Thus, I thought "data" is related to fee...

2

To just send ETH, set gasLimit to 21,000 and remove data (or set it to empty ''). The base fee of 21,000 gas assumes that to is not a contract.

If to is a contract and you wanted to invoke a function on to, you would set data according to the function you wanted to invoke and any arguments it needed. For this, the value of data would be encoded according to the ABI. Using web3.eth.estimateGas can help see how much gas is needed to execute the function and whatever functions it may also invoke.

data can also be used to simply store bytes on the blockchain. If to is not a contract, you can statically compute the fee (in addition to the base 21,000 gas) as 4 gas for a zero byte, 68 gas for non-zero byte. Related: Mist: What does "intrinsic gas too low" mean?

  • Thank you very much for your informative answer. I'm reading about links you shared to me. – zono Jul 16 '17 at 9:06
  • Welcome! Later on if you try do more things with data to contracts, you will want to be aware of fallback-function. – eth Jul 16 '17 at 9:21
  • Thanks. I'm trying to catch up it step by step. At beginning, I will do sending ETH, create a contract and create ERC20 token. These are my first goal. – zono Jul 16 '17 at 9:29
2

Gas: For a transaction between two private-key controlled accounts (not sending to smart contract) costs 21000 gas. If you are sending a transaction to a smart contract that has some functionality, you have to pay for that functionality with gas. You can estimate the gas costs but keep in mind that it is not always possible to know the gas consumption prior to calling, so just give a high enough value. I recommend testing your entire contract on Remix first to get a feeling for the gas costs.

Data: You can send arbitrary data in all transactions. Keep in mind that sending data is fairly expensive (in gas!). Normally, when sending from one private key controlled account to another you keep this empty (default value). This data field is also used to tell a smart contract which function you are calling. The function signature is encoded in the first four bytes of the keccak hash of the function name including parameters.

  • Thank you so much. Your answer is very clear to understand for me. eth game answered to me earlier so I accepted him answer. thanks. – zono Jul 16 '17 at 9:02

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.