Payable Rule

Make functions payable whenever you can. Because adding a payable keyword lowers the number of opcodes being executed, thus lowering the gas consumption.

Example

pragma solidity ^0.8.17;

contract Constants {
    uint256 public counter = 1;

    function nonPayable() external {
        counter = 1000;
    }

    function madePayable() external payable {
        counter = 1000;
    }
}

Calling function nonPayable would cost 616 gas and calling function madePayable would cost 3543 gas only, proving that making function payable could save around 3000 gas.

Test Environment

  • Compiler: solc 0.8.17
  • Testing Framework: Foundry

ETH saved over 100 transactions

100 tx * 30 gwei * 0.000000001 * (3543-616) = 0.008781 ~= 0.009 ETH1

USD Saved over 100 transactions

  • At \$4000 / ETH: \$36
  • At \$3000/ETH: \$27
  • At \$2000/ETH: \$19
  1. Assumed gas price: 30 gwei 

results matching ""

    No results matching ""