7

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.

7alip
  • 895
  • 6
  • 11

1 Answers1

10

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.

bguiz
  • 27,371
  • 47
  • 154
  • 243
  • 2
    This article explains one of the biggest differences in gas estimation between Ethereum and RSK https://medium.com/iovlabs-innovation-stories/the-dark-side-of-ethereum-1-64th-call-gas-reduction-ba661778568c – Ilan Olkies Jan 27 '21 at 16:08