9

For calculating transaction fee (from web3), need to use gas price at that moment

eth.getTransaction("txhash").gasPrice

and multiply actually on how much gas was used

eth.getTransactionReceipt("txhash").gasUsed

Is it correct? Or it could be calculated more simple?

18

Hopefully by the end of this explanation, you'll have a good understanding of the transaction fees that go along with Ether transactions. First of all, we have to understand what gas is (SKIP THIS IF YOU ALREADY HAVE A GOOD UNDERSTANDING OF GAS):

  • gas:

    This is actually a currency all on it's own used by the whole Ethereum network. It's used for the purposes of paying Miners for their work. Miners are people who working on "processing" the payments so-to-speak. Now... you may ask "why isn't Ether used as this currency instead?" Why does there need to be a totally different currency and complicate our life more? Well it partially has to do with how Crypto currencies are always changing in value, and if you had to code into the Ethereum network to adjust for these values, things become more complicated. So the solution was to separate the gas from the Ether and then just look at the exchange rate for Ether when deciding how much the gas will cost you to do your transaction.

So now that we know what gas is, how do we use it? Well in the Ethereum network, the minimum amount of gas that is needed for a normal transaction to take place is 21000 gas. But, people have the option of putting in more gas. Now how it works is, the more gas you put, the more Miners are interested in mining your transaction, hence the time it takes for your transaction to happen is lower.

But lets assume you don't care about transaction time, and currently for Ether it really doesn't take long at all even if you use the minimum amount of gas. So lets say you choose to put 21000 as your gas value. Cool. But how much does that gas cost? Remember in the description of gas above, we said that the Ethereum Network looks at the gas/Ether exchange rate. So what happens here is that, when you picked your gas value, the exchange rate is found, and those 2 values are multiplied:

gas * (Ether price / gas) = Ether price = transaction fee

So lets refer to (Ether price/gas) as gasPrice. Then the equation is:

gas * gasPrice = transaction fee

Now we know how much the transaction fee is! (the smallest possible fee to be exact).

Now if you wanted to for instance withdraw all of your balance, you would first need to calculate the transaction fee, then subtract that from the balance you want to withdraw, and then do the withdrawl. Here is an example:

  • Example:

    var sender = web3.eth.accounts[0];//sending the either
    var receiver = web3.eth.accounts[1];//receiving the ether
    var balance = web3.eth.getBalance(sender);//finding it's total balance
    var transactionObject = {
      from: sender,
      to: receiver
    }
    
    var transactionFee = web3.eth.gasPrice * 21001;
    
    transactionObject.value = balance - transactionFee;
    
    web3.eth.sendTransaction(transactionObject, myCallbackFunction);
    

NOTE: you'll often see the following code replacing the 21001 above:

web3.eth.estimateGas(transactionObject)

and the reason this is done is because of good practice. Transactions of Ether are not the only thing you can do with the Ethereum Network. Different actions cost different amounts of gas. It is good practice to use the estimateGas function in general.

Also you may have noticed I used 21001 instead of 21000 in the example above. I have noticed other people using this value, and I assume it's because of some small details that maybe sometimes cause an issue with the transaction fee (what if the estimated gas price changes right after you find it for instance?), so that is why I use it too, but I think this is might be more useful to have as a question on it's own on this website :)

  • ty for detailed explanation). It's all okey, when I send transactions by my own. But, when u have a payment system, which need to listen transactions for some accounts, this accounts could send transaction with another gas value (for example 30000) or this gas value could change in future, be much higher, and use all this 30000 amount of gas, and it wont be incorrect to calculate fee on already mined transactions. web3.eth.gasPrice from docs it calculates on latest blocks median gas price, it's okey, when u send it now). – DeV1doR Jul 10 '17 at 8:43
1

You might want to look at the web3 documentation. As described there, you can get the current gas price like this:

var gasPrice = web3.eth.gasPrice;

The rest looks good.

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.