Is there a way to make the user pay “aggressive” gas fees automatically instead of them choosing that in MetaMask

gasgas-pricemetamaskpending-transactionstransactions

I am having issues with MetaMask in my DApp because users are using wrong or invalid RPCs.

  1. Is there a way of making users add a network automatically to MetaMask with the right RPC and override the chain they have (in case they already have it)?

In addition to this, some users reported that the transactions are not being mined. I was able to check that the problem is related to the gas fees offered, so the transactions will be mined correctly if they choose "aggressive" in their MetaMask for fees:

  1. Is there a way to make this setup in MetaMask a default for my DApp, instead of them having to update this manually every time?

Best Answer

  1. You can provide a button to add/switch network in your dApp and utilise the wallet_addEthereumChain and wallet_switchEthereumChain RPC methods to prompt the user to add a specific, pre-configured network to their MetaMask wallet, as mentioned in this MetaMask docs.

  2. The optimal way would be to estimate the gas usage by calculating the same while calling a particular smart contract function, using estimateGas function like contract.method.estimateGas() if using ethers.js or web3.eth.estimateGas() if using web3.js. You can potentially add some buffer on top of the estimated gas usage and gas price values, and use the same while making the actual transaction or function call, which would be equivalent to aggressive option of MetaMask.

    You can refer to this post for ethers.js and this post for web3.js.

Related Topic