q9f / eth.rb

a straightforward library to build, sign, and broadcast ethereum transactions anywhere you can run ruby.
https://q9f.github.io/eth.rb
Apache License 2.0
200 stars 86 forks source link

Auto set working gas fee values? #230

Closed stebo closed 1 year ago

stebo commented 1 year ago

Hi all 👋 and thanks a lot for providing this Gem :)

I wondered whats the correct way to determine & set the different gas fee values, e.g. to successfully run a mint tx on a contract?

client = Eth::Client.create(alchemy_com_polygon_node)
# default values will be set
# client.max_fee_per_gas = 42.69 GWei
# client.max_priority_fee_per_gas = 1.01 GWei

Which would not be enough to submit a transaction to polygon Mainnet (100-150 GWei) at the moment.

How do you deal with this? Any way to use the lib to get current gas fees from a node or calculate based on an ABI what should be required? Or would you suggest using a secondary API to check for current fees every now and then?

q9f commented 1 year ago

Yes, take a look at eth_gasPrice:

client.eth_gas_price["result"].to_i(16)
# => 24171894406

Obviously, add some error handling.

If you think it's too low or you want to add priority, you could also do something like add 20%:

(client.eth_gas_price["result"].to_i(16) * 1.2).to_i
# => 29754321063

Priority fee is just a tip, you could leave this at the default I guess or just add something like 42% to incenitivize miners:

(client.max_priority_fee_per_gas * 1.42).to_i
# => 1434200000
stebo commented 1 year ago

Thanks a lot for the quick reply & clarification, totally missed that one! 🙇