How to calculate what gas price to use for transactions on RSK?
Asked Answered
G

1

3

I need to know how to set a proper gas price for my transactions on RSK network. I am familiar with the eth_gasPrice approach from Ethereum:

$ curl https://public-node.testnet.rsk.co -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_gasPrice","params":[],"id":1}'
{"jsonrpc":"2.0","id":1,"result":"0x3938700"}

Is the above recommended for RSK as well?

Guessrope answered 13/1, 2021 at 13:0 Comment(1)
I am a developer who uses Truffle framework to publish smart contracts on RSK. I also need to know the gas price and use it in my projects. In the RSK Truffle boxes they have a interesting approach to query and use the gas price. It is using the query mentioned above, saving in a file and reading it in truffle-config file. I recommended it for developers: developers.rsk.co/tutorials/truffle-boxes/rsk-starter-boxJuror
C
9

You have several ways to calculate the gas price.

From an end user point of view, you can get the gas price from:

  • RSK Stats
  • Metamask/Nifty/MEW proposed gasPrice value when preparing a transaction

From a developer point of view, you need to know that RSK has a minimumGasPrice limit, this means that if you set a gasPrice below that minimum, your transaction will be rejected.

How to get gasPrice:

Using JSON-RPC method eth_gasPrice. This gives you a current average gas price in the network. It's a good idea to specify an additional 5% gas price as a buffer, in order to be above the average gas price.

The example code above you have is correct:

$ curl https://public-node.testnet.rsk.co -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_gasPrice","params":[],"id":1}'
{"jsonrpc":"2.0","id":1,"result":"0x3938700"}

... however, do the following as well to calculate the amount with buffer:

$ node -e "console.log(0x3938700 / 20 * 21);"
63000000

eth_gasPrice returns 60 million, but we use 63 million when submitting transactions. For those familiar with Ethereum development, this is the same method as you would use there.

How to get minimumGasPrice:

Using JSON-RPC method eth_getBlockByNumber with best block number as parameter and getting minimumGasPrice from the response. As the minimum gas price may change 1% between blocks, see RSKIP-09, it is recommended to specify an additional 10% gas price as buffer, to ensure a 10 block window of valid gasPrice. This caters to a pessimistic scenario.

Example code to perform the above:

curl https://public-node.testnet.rsk.co -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest",false],"id":1}'
{
   "jsonrpc" : "2.0",
   "result" : {
      "cumulativeDifficulty" : "0x2b7cd0b2",
      "size" : "0xbcc",
      "uncles" : [],
      "gasUsed" : "0x546fe",
      "hash" : "0x68abc09397ec6ae77acf52c195638dd9f93e5756eb1856c81e8e30cbbeef6b39",
      "difficulty" : "0x2b7cd0b2",
      "miner" : "0x1fab9a0e24ffc209b01faa5a61ad4366982d0b7f",
      "bitcoinMergedMiningMerkleProof" : "0x8bfb89a6e8a8fa0fee018e9dd0d86c0aea40806d3e8fe45e9454a250b763d978c812164690d7af62b31743db9944cc5ac6038d210ef6d1e4fb08c712f9a318c0538845ed4d3f3b61879aba5e79c58bc754213a9872f54c2da44051b3c0e04d4be382b6f1303386afc08c991a85aae39fa3263cbcbd9d4a05d8a2d0797204902de56af20198ad4a3b2c553e0c2500da50a536fd10233204c5a9837802f6816f740a146e58ebae0d7be3ca3a79f7d2a942e5ade058baa53e252162fefc136f33434e84753446aad913ea4baafd6bcaa145d37b9b3b85fec42d7bf47d5efef8e000",
      "paidFees" : "0x1395b92f0000",
      "stateRoot" : "0xb7fc903f8f60a3e0d0d80ea58348e9637e3a9a4c8f9de6e089326c9bfbb698ed",
      "transactions" : [ "<< redacted for brevity >>" ],
      "totalDifficulty" : "0x2eff7f12ec2c2655d",
      "bitcoinMergedMiningHeader" : "0x00000020901806378d89a22c6aaa772bab42ec41dfdb4dce77d361181600000000000000a0782738fa6b7ef70644de0aca393597462d9fb97d59c92680eba5e5a2b66af14c6efe5f5c75161904657655",
      "receiptsRoot" : "0xc930346e224a837fc426b900c07d3f744f4c82ac789d05624dbbe4e008dc2f22",
      "hashForMergedMining" : "0xb58502263dd8363d0c88287eb3436c731ff5763e5ed4d4919306b028001738f9",
      "transactionsRoot" : "0x9cc25cf985416ef18fa171f59b9d03f6921bad3c93f956dc0d472f9429b81167",
      "gasLimit" : "0x67c280",
      "bitcoinMergedMiningCoinbaseTransaction" : "0x00000000000000801d9be7d007fe4505303282a8fdd52df91d1292256a776a2181521c4513ee6c246088ac0000000000000000266a24aa21a9edee78f53b94245e73c99fb98a96b539836fa82b85a20de1c0d7984f0eabd198f600000000000000002a6a52534b424c4f434b3ab58502263dd8363d0c88287eb3436c731ff5763e5ed4d4919306b028001738f900000000",
      "logsBloom" : "0x00000000000000000000001000000008000000000040000000000000000000000000400000050000000400000000000400000000010000000000000000000000000000000080000000000004000000000000000000000080000000004000000000000000800000000000001000000000000000000000000002000008080000000000000000000000000001000208000000000000040000000000000420080000000000020008100000000000020010000080000000000082001000000000000000000001040000000000000000080000000030000001000880200004100200000000000020000000002000000000100000000000000000010200000000000000",
      "minimumGasPrice" : "0x387ee40",
      "timestamp" : "0x5ffe6e40",
      "number" : "0x1738f9",
      "parentHash" : "0x1276aa271525f757166acf767cd6100a25d0cba18f6caf925299276f3d439404",
      "sha3Uncles" : "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
      "extraData" : "0xd1018f504150595255532d62643739313837"
   },
   "id" : 1
}

The response for this is pretty large (see above), however, it does contain the info we need: "minimumGasPrice" : "0x387ee40",. The final step to include the buffer:

$ node -e "console.log(0x387ee40 / 10 * 11);"
65164000

eth_getBlockByNumber.minimumgasPrice returns approximately 59 million, but we use approximately 65 million when submitting transactions.

For those familiar with Ethereum development, this is approach is different and only available on RSK.

Circumfluent answered 13/1, 2021 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.