I am developing a Dapp. I was wondering what are the best practices for user recommendation regarding gas price and limit in such a Dapp? Should I be using the following methods in my contract, or should I rely on the user's wallet (e.g. MetaMask) to recommend these values to the users? What are the pros and cons of each technique? In which scenarios should I use one instead of the other?


Thank you. J


My recommendation:

Configure gas-price according to user-input, allowing for the current price as default (you'll need to implement function scan):

async function getGasPrice(web3) {
    while (true) {
        const nodeGasPrice = await web3.eth.getGasPrice();
        const userGasPrice = await scan(`Enter gas-price or leave empty to use ${nodeGasPrice}: `);
        if (/^\d+$/.test(userGasPrice))
            return userGasPrice;
        if (userGasPrice == '')
            return nodeGasPrice;
        console.log('Illegal gas-price');

Configure gas-limit using estimateGas, but capped with a minimum threshold in order to handle inaccurate estimations:

const MIN_GAS_LIMIT = 100000;

async function getGasLimit(web3, transaction, userAddress, value = 0) {
    const gasLimit = await transaction.estimateGas({from: userAddress, value: value});
    return Math.max(gasLimit, MIN_GAS_LIMIT);
  • 1
    Thank you for your time and detailed answer goodvibration. – JF0001 Nov 16 '20 at 3:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.