Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you estimate the amount of gas required for a smart contract invocation?

Tags:

web3js

rsk

I want to send a transaction in RSK network and I get this message in logs: Not enough gas for transaction execution.

I got the gas limit parameter from my testing environment, using web3.eth.estimateGas.

like image 517
7alip Avatar asked Jan 27 '21 14:01

7alip


People also ask

How is gas calculated for smart contract?

The gas fee is calculated using Gas Limit * Gas Price per Unit. So if the gas limit was 20,000 and the price per unit was 200 gwei, the calculation would be 20,000 * 200 = 4,000,000 gwei or 0.004 ETH.

How are gas points calculated?

The gas calculation formula is: 21,000 (gas limit) x (50 (base fee) + 15 (Tip)), or 21,000 x (50 + 15). This returns a total gas fee of 1,365,000 gwei or 0.001365 ETH.

Do smart contracts have gas fees?

In Solidity Gas is a fee which is required to conduct a transaction on the Ethereum blockchain. Gas prices are specified in gwei. Gwei is a denomination of the cryptocurrency Ether.


1 Answers

RSK nodes have a JSON-RPC for eth_estimateGas, which is the most reliable way to perform gas estimations.

You can do this from the terminal using curl:

curl \
  -X POST \
  -H "Content-Type:application/json" \
  --data '{"jsonrpc":"2.0","method":"eth_estimateGas","params":[{"from": "0x560e6c06deb84dfa84dac14ec08ed093bdd1cb2c", "to": "0x560e6c06deb84dfa84dac14ec08ed093bdd1cb2c", "gas": "0x76c0", "gasPrice": "0x3938700", "value": "0x9184e72a", "data": "" }],"id":1}' \
  http://localhost:4444
{"jsonrpc":"2.0","id":1,"result":"0x5208"}

Alternatively, using web3.js:

web3.eth.estimateGas({"to": "0x391ec8a27d29a42c7601651d2f38b1e1895e27a1", "data": "0xe26e496319a16c8ccae126f4aac7e3010123927a4739288cd1ace12feafae9a2"})
23176

While this is the same JSON-RPC found in geth (Ethereum) and other Ethereum-compatible nodes, note that the gas calculations in RSK and Ethereum are different. Thus their implementations differ.

For example, the price of certain VM opcodes are different. Another notable point of difference related to gas estimation, is that Ethereum implements EIP-150, whereas RSK does not. This means that the 1/64 reduction in gas estimation does not apply to RSK. (The detailed implications of this on gas estimation are perhaps beyond the scope of this question.)

This means that you will expect incorrect values when running against ganache-cli (previously testrpc), which is used by default in common developer tools such as Truffle.

To get the correct gas, using the RSK-specific calculations, the best way is to use RSK Regtest when invoking eth_estimateGas for local development and testing. In other scenarios you may also use RSK Testnet and Mainnet.


The following other scenarios are also relevant, but not directly related to your question, but are also good to know:

When invoking smart contract functions that have the pure or view modifiers, no gas (and therefore gas estimation) is necessary.

When performing certain transactions that have a define invariant gas price, simply you may use that as a hard-coded constant. For example the transfer of the native currency (RBTC in this case), the invariant gas price is 21000. This assumes that no data (sometimes referred to as "message") was sent with the transaction.

like image 110
Alejandro Cavallero Avatar answered Sep 23 '22 20:09

Alejandro Cavallero