Is it possible to specify gas cost for a particular function call? E.g. Let's say I want a transfer() function to cost 50000000 gas, would I be able to hardcode this into the function?

1 Answer 1


Are you asking how-to set the limit on a transaction, so that it will succeed if the contract requires 5m gas or less? If so, you can specify the limit in web3 as follows:

let tx = await myContract.methods.myMethod(params...).send({ gas: 5000000 })

Or maybe are you looking to ensure that a transaction consumes at least 5m gas? I'm not sure why you would want that but you could by adding a loop to the end of your function, something like:

function expensiveTransfer() public {
  uint maxGasLeft = gasleft();
  require(maxGasLeft > 5000000);
  maxGasLeft -= 5000000;

  ...normal function logic...

  while(gasleft() > maxGasLeft) {
    // wasting money till you spent enough
  • Instead of wasting money you can think of this as a mandatory donation to miner. Commented Mar 9, 2019 at 20:37

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.