I am implementing a smart contract function to work from my backend using the owner wallet. This function will be called when user does some kind of activation in frontend so this function can be called thousands of times. Since I am going to pay for the transaction fees I want to make sure it will not cost too much.

Transaction cost of function is 143675.

I am on polygon network. When the gas price is something between 150-400 gwei it is kind of affordable. However, when I look at the gas price history, I can see that it can sometimes be extreme numbers like 5000-1000 gwei.

If I do not check or override the gas price, if let's say 50 users do the operation when gas price is 1000 gwei, it will cost me around 70 MATIC which is not affordable.

I want to prevent this transaction if it costs me too much.

  • What happens if I set the gas price statically from ethers.js and current gas price is higher? Will it queue the transaction(make it pending) or reject?
  • If it rejects, Do I have to check the gas price and return error(try again later) if it is too high?
  • Is there any other option or practice for such use case?
  • it will usually set in pending with other transactions. if it will ever be mined idk. I think you need to store the transactions requests in a queue/stack in your backend. And should try to find a service provider that tell you the current gas costs. If they are normal then send the transactions (as long the gas price is okay) and check after that if they are mined. The problem in polygon is that the gas price change quickly and not stable
    – Majd TL
    May 10 at 11:49
  • You could also set a low gas price and send it. The tx wont be mined immediatly but when gas prices are lower then it will be processed. Just be careful with sending multiple tx like this because if one with a large nonce is processed before anotherone with a lower nonce, then the last one will fail. May 10 at 18:18
  • @FranciscoAramburu What you mean is, if I set the gas price higher for a tx with higher nonce it can be processed before the one with lower gas price set and lower nonce so it will fail the last tx? how can I control this? Is it the only way to use the estimated price in practice? May 11 at 12:48


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.