0

I'm new to Ethereum world. I've just manage to create an example that calculates transaction fee and sends a signed transaction without a contract using Web3js library.

My issue is that my test account is running out of balance pretty quickly. I must be doing something wrong. I did this transaction: https://kovan.etherscan.io/tx/0x5cb10c454d8d331e35ba3812522ea9cd5b82fdb7ecde75c5b48ec1550d99cc98

If you see the details:

Value: 0.000106 Ether ($0.00) 
Gas Limit: 21000 
Gas Used By Txn: 21000 
Gas Price: 0.000106 Ether (106,000 Gwei) 
Actual Tx Cost/Fee: 2.226 Ether ($0.000000)

I see the actual transaction fee is 2.226 Ether!

I'm calculating the transaction fee as follows:

var transactionObject = {
  from: sender,
  value: amount
}
w3.eth.estimateGas(transactionObject, function(error, gas) {
  w3.eth.getGasPrice(function (error, gasPrice) {
    var gasPrice = Number(gasPrice);
    var transactionFee = gasPrice * gas;
    console.log(transactionFee);
 })
});

Then this transaction fee, I am using it on my transaction:

w3.eth.getBalance(sender, function(error, balance) {
  if (balance < (transactionFee + transactionValue)) {
    reject("You don't have enough balance to cover this transaction");
  } else {
    w3.eth.getTransactionCount(sender).then(function(count) {
      var transactionObject = {
        nonce: w3.utils.toHex(count),
        from: sender,
        to: receiver,
        gasPrice: w3.utils.toHex(transactionFee),
        value: w3.utils.toHex(transactionValue),
        data: ''
      }
      w3.eth.estimateGas(transactionObject, function (error, gas) {
        transactionObject.gasLimit = gas;
        var tx = new Tx(transactionObject);
        tx.sign(privateKey);
        var serializedTx = tx.serialize();
        w3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), function (error, result) {
          if(!error) {
            resolve(result);
          } else {
            reject(error)
          }
        })
      });
    }).catch(function(error) {
      reject(error);
    });
  }
})

Am I assigning incorrectly a value or missing some conversion?

3
  • Have you considered converting explicitly the gas to wei? Seems like it is taking them as ethers.
    – ranchalp
    Dec 15, 2017 at 16:21
  • @ranchalp do you mean, changing this line: gasPrice: w3.utils.toHex(transactionFee) to gasPrice: w3.utils.toHex(w3.utils.toWeis(transactionFee))?
    – Eduardo
    Dec 15, 2017 at 16:23
  • sounds like you are on the right track... your gas price of 106,000 Gwei is 1000x higher than normal Jan 31, 2022 at 0:15

1 Answer 1

0

I found the issue, I was passing to:

gasPrice: w3.utils.toHex(transactionFee)

The whole fee: gas * gasPrice.That caused that internally my transaction fee was multiplied again by the gas.

I need to pass:

gasLimit: w3.utils.toHex(gas),
gasPrice: w3.utils.toHex(gasPrice)
1
  • Yes, but it should be gas: rather than gasLimit:. Apr 17, 2019 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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