2

I want to send an ERC20 token (Ropsten network). So I execute a function 'transfer' on a contract. When The transaction is sent, I can get the gas used in the receipt. Few months ago this amount was 36879 (0xff82bbc36b89e9ba428863c639ceebd87115c1012cdbf94b881641579aba07fe), now it is 51879 (0xe0bc6f9faad1135f3dad652603864792b7b1f16d7148c550fe580772952c3130) and I got a 'out of gas' error message.

Questions:

1) the transaction fees are (gasPrice * gas), right ? If I set gasPrice and gas, why, gas used (36879) is not enough anymore ?

2) If I set the value of gas, why must I need to provide gasLimit also ?

3) How can I know the exact transaction fees (gas used) before sending it ?

2

Parameters gas and gasLimit are the same.

Parameter gasPrice has nothing to do with your question.

The gas for a transaction depends on what's being executed.

If you are transferring ether to an externally-owned account, then it will be 21,000.

If you are calling a smart-contract function (which includes transferring ether to a smart-contract), then it will be higher than 21,000.

How much higher? That depends on the actual set of opcodes being executed, as well as the storage (state-variables) being changed during the execution.

And since these two things are not necessarily the same every time you execute a function (even if it's the same function, and moreover - even if you're passing the same arguments), the gas required is not necessarily the same.

In general, the gas parameter is there to protect you from spending too much.

If you are 100% confident that the code is not "malicious" (for example, if it's your code), then you can just use a high enough value which will ensure the execution of your transaction.

Only the actual gas required will be used, even though you've stated a higher amount.

Note, however, that the account which you use in order to execute the transaction must hold sufficient funds (i.e., gas * gasPrice) in order for it to be executed, even if not all the gas is required for the execution.

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.