What I am trying to do is simply increase the gas costs of running a solidity function, making sure that the right amount of gwei is spent/burned.

Any good, simple, and a clean way of doing this ?
-Solidity version is 5.x

Note: a different approach can be this (also a question I asked) How to send ETH to address(0) from msg.sender address using a token's contract, Solidity 5.x?

  • I just have to ask. What kind of development are you doing that requires you to waste gas? The whole community strives to save tiniest amount of gas and then there is you. – Iftifar Taz Oct 13 '19 at 6:09
  • It is a new token, named Hertz, deflationary with a constantly rising price. I can share the source code once everything is implemented. This is the last piece I need to add. – Damir Olejar Oct 13 '19 at 11:40

If you refer to the docs about globally available variables, the two variables that will be useful in this case are

gasleft() returns (uint256)


tx.gasprice (uint)

The former will return the amount of gas that is remaining, and the latter gives you the gas price set for this transaction.

With that, the amount of gas to burn is

gasToBurn = weiToBurn / tx.gasPrice

Then your function just needs to keep consuming gas until it's burned the right amount (with a loop for example). But how specifically you go about burning that gas is up to you.

| improve this answer | |

1st impossible thing:

You can't make sure to burn exact amount of ether when calling a function. Solidity smart contracts use gas not ether/gwei to execute operations and transaction sender specifies the gas price. So a function that requires 200 gas to execute may cost one user 200 wei and another user 600 wei.

2nd impossible thing:

You can't just burn gas in solidity. When you do some operation you burn gas. For example i++ would cost you 674 gas.

Based on this idea you can write a modifier or function to burn gas.

pragma solidity  ^0.5.11;
contract SillyContract {
    address private owner;
    constructor() public {
        owner = msg.sender;
    modifier wasteEther(uint256 _lowestLimit) {
        uint256 counter = 0;
        while(gasleft() > _lowestLimit) counter++;
    function getOwner() public view wasteEther(2900000) returns(address aa) {
        return owner;

Here wasteEther() modifier will continue to waste gas untill 2900000 gas remains. You can use more complicated operations than counter++ to consume gas quicker.

| improve this answer | |
  • No such thing as "quicker" here. Either it will go into a block and be executed to completion, or it won't (based on the incentive for miners, i.e., gas-price used and gas-limit specified). I believe that due to the while loop, getOwner.estimateGas() cannot be called, and so no miner will ever want to execute it (though I'm not entirely sure about this one, since I've never been on the mining side of the game). – goodvibration Oct 13 '19 at 6:30
  • Also note that despite their name, modifiers are not designated for state-changing, and in fact, it is considered a very bad practice to do so. Their sole purpose is to provide a rather clean manner (coding-convention if you will) for performing condition-verification. – goodvibration Oct 13 '19 at 6:32
  • Don't worry about the gwei part, I have done all the calculations. I just need to transfer N amount of ether from msg.sender to address(0). Please help me here, I have deployed your code, executed getOwner, and nothing happens. – Damir Olejar Oct 13 '19 at 11:44
  • Edited a question with a link to an alternative solution to a same problem. It is also a question I have asked. – Damir Olejar Oct 13 '19 at 12:08
  • 1
    As for your first impossible thing: you can see the gas price with tx.gasprice (solidity.readthedocs.io/en/v0.5.10/…) and use that in the calculations. Furthermore, if all operations are inside your contracts, you can calculate how much the rest of the contract requires gas and therefore you should be able to calculate correctly how much to burn extra gas. – Lauri Peltonen Oct 13 '19 at 13:07

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.