21

The default gas price is 0.02e12 Wei. But while creating transactions on the command line or Geth's Javascript console you can define your own gasPrice:

web3.eth.sendTransaction({/* ... */, gasPrice: 123456789});

In Mist browser based Ethereum Wallet, there is a slider to set the gas price from "low" (slower) to "high" (faster). What are these limits actually? What are realistic gas prices, and what happens if it is chosen too low or "too high"?

3 Answers 3

18

Can I set the gas price to what ever I want?

Yes, you can. But miners have a default strategy for determining gas price to charge and if the amount you're willing to pay is below that, your transaction will be rejected. (Try setting the slider all the way to the left in Mist and try to send a transaction.)

What are these limits actually?

Completely arbitrary and made up by the gas price oracle running on miners. There is no necessity to use this oracle, you could swap it out for a really greedy or really kind one. It probably helps the stability of the network to leave things as is though.

What are realistic gas prices, what happens if it is chosen too low or "too high"?

Realistic would be assuming most people don't mess with the gas price oracle settings when they mine and they use all defaults. Which means as you fill up more and more of the 3,141,592 maximum gas in the block, you'll need to provide a higher and higher gas price. (Which by default discourages overusing gas / filling blocks.) Too low = your transaction won't go through. Too high = miners will probably thank you. (There is no too high with gas prices.)

1
  • 2
    if he tries setting the slider in Mist all the way to the left, he will submit a transaction which will not be processed, and he might find himself with a locked account, since the transaction is going to be pending forever, and all subsequent transaction won't be processed either. So, you are essentially suggesting him to block his funds.
    – Nulik
    Commented Dec 11, 2017 at 20:17
5

To see what gas prices are currently being accepted by miners, and how long it will take your transaction to confirm with a given gas price, see EthGasStation.info. Here is a snapshot of the conditions at the time I wrote this post:

enter image description here

3

You should send your transaction with safe gas price. Here is my code how I am getting gas price:

async function getCurrentGasPrices() {
  let response = await axios.get('https://ethgasstation.info/json/ethgasAPI.json');
  let prices = {
    low: response.data.safeLow/10,
    medium: response.data.average/10,
    high: response.data.fast/10
  };
  return prices;
}

Following function will show price in ETH:

async function actualNetworkFee() {
  return new Promise(async (resolve) => {
    let gasPrices = await getCurrentGasPrices();
    let low = parseFloat(((21000 * gasPrices.low) / 1e9).toFixed(8));
    let medium = parseFloat(((21000 * gasPrices.medium) /  1e9).toFixed(8));
    let high = parseFloat(((21000 * gasPrices.high) /  1e9).toFixed(8));

    let fee = { low, medium, high };
    console.log('Ether fee',fee)
    resolve(fee)
  })
}

You need to set price in following way when sending ETH:

async function transferEther(fromAddress, toAddress, amount, privateKey, nonceAdd, networkFeeType) {

  return new Promise(async (resolve, reject) => {
    try {
      web3js.eth.getTransactionCount(fromAddress, async (err, txCount) => {

        networkFeeType = networkFeeType.toLowerCase();
        var nonce = web3js.utils.toHex(txCount + nonceAdd)
        let gasPrices = await getCurrentGasPrices();

        let value = web3js.utils.toHex(web3.utils.toWei(amount.toString(), 'ether'));
        let gas = 21000;
        let gasPrice =web3js.utils.toHex(gasPrices.low* 1e9);

        if (networkFeeType == 'avg') {
          gasPrice = web3js.utils.toHex(gasPrices.medium* 1e9);
        }
        else if (networkFeeType == 'max') {
          gasPrice = web3js.utils.toHex(gasPrices.high* 1e9);
        }
        let details = {
          "to": toAddress,
          "value": value,
          "gas": gas,
          "gasPrice": gasPrice,
          "nonce": nonce,
          "chainId": parseInt(process.env.chainID) // EIP 155 chainId - mainnet: 1, rinkeby: 4
        };

        const transaction = new EthereumTx(details, { chain: process.env.chain });
        privateKey = privateKey.split('0x');
        let privKey = Buffer.from(privateKey[1], 'hex');
        transaction.sign(privKey);

        const serializedTransaction = transaction.serialize();

        web3js.eth.sendSignedTransaction('0x' + serializedTransaction.toString('hex'), (err, id) => {
          if (err) {
            resolve({ success: false, err: err.toString() });
          }
          const url = config.get("ETHERSCAN_API") + `/tx/${id}`;
          console.log(url);

          resolve({ success: true, hash: id });
        });
      })
    }
    catch (err) { reject(err) }
  });
}

Hopefully this code will help a lot of developers :)

Your Answer

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

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