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?