Giter VIP home page Giter VIP logo

free-tx-modifier's Introduction

Solidity freeTx modifier

Simple implementation of freeTx solidity modifier to add a full transaction cost rebate to any function

Snippet

/** 
 * @title NiceGuy
 * @dev Implements `freeTx` modifier to add a full transaction cost rebate to any function
 */
contract NiceGuy {
    
    /**
     * @dev Transfer all ether use in the contract function back to the sender  
     */
    modifier freeTx() {
        uint256 startGas = gasleft();
        _;
        uint256 spentGas = startGas - gasleft() + 21000 + 16 * msg.data.length;
        payable(msg.sender).transfer(spentGas * tx.gasprice);
    }
    
}

Basic Usage

/**
 * @title Button created by Marto
 * @dev Usage example of NiceGuy "freeTx" modifier to add a full transaction cost rebate to any function
 */
contract Button is NiceGuy {
    uint256 public counter = 0;
    
    constructor() payable {}
    
    /**
     * @dev Transfer all ether use in the contract function back to the sender  
     */
    function push() public freeTx {
        // Make some expensive computation
        for (uint i=0; i < 500; i++) {
            counter = counter + 1;
        }
    }
    
    function getBalance() public view returns (uint256) {
        return address(this).balance;
    }
}

More example contracts coming soon

Made with ❤ by Marto (marto.eth)

free-tx-modifier's People

Contributors

0xmarto avatar

Stargazers

 avatar sam bacha avatar

Watchers

James Cloos avatar  avatar  avatar

Forkers

sambacha

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.