Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Local Memory Type Variable Instead of Global Storage Type Variable in Event to Save Gas #159

Closed
HighBe opened this issue Aug 19, 2022 · 0 comments

Comments

@HighBe
Copy link

HighBe commented Aug 19, 2022

Hi, we recently have conducted a systematic study about Solidity event usage, evolution, and impact, and we are attempting to build a tool to improve the practice of Solidity event use based on our findings. We have tried our prototype tool on some of the most popular GitHub Solidity repositories, and for your repository, we find a potential optimization of gas consumption arisen from event use.

The point is that when we use emit operation to store the value of a certain variable, local memory type variable would be preferable to global storage type (state) variable if they hold the same value. The reason is that an extra SLOAD operation would be needed to access the variable if it is storage type, and the SLOAD operation costs 800 gas.

For your repository, we find that the following event use can be improved:

  • FraxUniV3Farm_V2.sol
    function name:setMultipliers
    event name:  MaxVeFXSMultiplier  
            LockedStakeMaxMultiplierUpdated  
            veFXSPerFraxForMaxBoostUpdated  
            veFXSBoostScaleFactor
    variable:    vefxs_max_multiplier->_vefxs_max_multiplier  
            lock_max_multiplier->_lock_max_multiplier  
            vefxs_per_frax_for_max_boost->_vefxs_per_frax_for_max_boost  
            vefxs_boost_scale_factor->_vefxs_boost_scale_factor
    function setMultipliers(
        uint256 _lock_max_multiplier, 
        uint256 _vefxs_max_multiplier, 
        uint256 _vefxs_per_frax_for_max_boost,
        uint256 _vefxs_boost_scale_factor
    ) external onlyByOwnGov {
        require(_lock_max_multiplier >= MULTIPLIER_PRECISION, "Mult must be >= MULTIPLIER_PRECISION");
        require(_vefxs_max_multiplier >= 0, "veFXS mul must be >= 0");
        require(_vefxs_per_frax_for_max_boost > 0, "veFXS pct max must be >= 0");
        require(_vefxs_boost_scale_factor > 0, "veFXS boost scale factor must be >= 0");

        lock_max_multiplier = _lock_max_multiplier;
        vefxs_max_multiplier = _vefxs_max_multiplier;
        vefxs_per_frax_for_max_boost = _vefxs_per_frax_for_max_boost;
        vefxs_boost_scale_factor = _vefxs_boost_scale_factor;

        emit MaxVeFXSMultiplier(vefxs_max_multiplier);
        emit LockedStakeMaxMultiplierUpdated(lock_max_multiplier);
        emit veFXSPerFraxForMaxBoostUpdated(vefxs_per_frax_for_max_boost);
        emit veFXSBoostScaleFactor(vefxs_boost_scale_factor);
    }

  function name:setLockedStakeTimeForMinAndMaxMultiplier
  event name:  LockedStakeTimeForMaxMultiplier
  variable:    lock_time_for_max_multiplier->_lock_time_for_max_multiplier

    function setLockedStakeTimeForMinAndMaxMultiplier(uint256 _lock_time_for_max_multiplier, uint256 _lock_time_min) external onlyByOwnGov {
        require(_lock_time_for_max_multiplier >= 1, "Mul max time must be >= 1");
        require(_lock_time_min >= 1, "Mul min time must be >= 1");

        lock_time_for_max_multiplier = _lock_time_for_max_multiplier;
        lock_time_min = _lock_time_min;

        emit LockedStakeTimeForMaxMultiplier(lock_time_for_max_multiplier);
        emit LockedStakeMinTime(_lock_time_min);
    }
  • veFXSYieldDistributor.sol
    function name:setYieldDuration
    event name:  YieldDurationUpdated
    variable:    yieldDuration->_yieldDuration
    function setYieldDuration(uint256 _yieldDuration) external onlyByOwnGov {
        require(periodFinish == 0 || block.timestamp > periodFinish, "Previous yield period must be complete before changing the duration for the new period");
        yieldDuration = _yieldDuration;
        emit YieldDurationUpdated(yieldDuration);
    }
  • FraxCrossChainFarmSushi.sol
    function name:setMultipliers
    event name:  MaxVeFXSMultiplier  
            LockedStakeMaxMultiplierUpdated  
            veFXSPerFraxForMaxBoostUpdated  
    variable:    vefxs_max_multiplier->_vefxs_max_multiplier  
            lock_max_multiplier->_lock_max_multiplier  
            vefxs_per_frax_for_max_boost->_vefxs_per_frax_for_max_boost  
    function setMultipliers(uint256 _lock_max_multiplier, uint256 _vefxs_max_multiplier, uint256 _vefxs_per_frax_for_max_boost) external onlyByOwnGov {
        require(_lock_max_multiplier >= MULTIPLIER_PRECISION, "Mult must be >= MULTIPLIER_PRECISION");
        require(_vefxs_max_multiplier >= 0, "veFXS mul must be >= 0");
        require(_vefxs_per_frax_for_max_boost > 0, "veFXS pct max must be >= 0");

        lock_max_multiplier = _lock_max_multiplier;
        vefxs_max_multiplier = _vefxs_max_multiplier;
        vefxs_per_frax_for_max_boost = _vefxs_per_frax_for_max_boost;

        emit MaxVeFXSMultiplier(vefxs_max_multiplier);
        emit LockedStakeMaxMultiplierUpdated(lock_max_multiplier);
        emit veFXSPerFraxForMaxBoostUpdated(vefxs_per_frax_for_max_boost);
    }

  function name:setLockedStakeTimeForMinAndMaxMultiplier
  event name:  LockedStakeTimeForMaxMultiplier
  variable:    lock_time_for_max_multiplier->_lock_time_for_max_multiplier

    function setLockedStakeTimeForMinAndMaxMultiplier(uint256 _lock_time_for_max_multiplier, uint256 _lock_time_min) external onlyByOwnGov {
        require(_lock_time_for_max_multiplier >= 1, "Mul max time must be >= 1");
        require(_lock_time_min >= 1, "Mul min time must be >= 1");

        lock_time_for_max_multiplier = _lock_time_for_max_multiplier;
        lock_time_min = _lock_time_min;

        emit LockedStakeTimeForMaxMultiplier(lock_time_for_max_multiplier);
        emit LockedStakeMinTime(_lock_time_min);
    }
  • StakingRewards.sol
    function name:setRewardsDuration
    event name:  RewardsDurationUpdated
    variable:    rewardsDuration->_rewardsDuration
    function setRewardsDuration(uint256 _rewardsDuration) external onlyByOwnGov {
        require(
            periodFinish == 0 || block.timestamp > periodFinish,
            "Reward period incomplete"
        );
        rewardsDuration = _rewardsDuration;
        emit RewardsDurationUpdated(rewardsDuration);
    }

  function name:setMultipliers
  event name:  MaxCRBoostMultiplier
          LockedStakeMaxMultiplierUpdated
  variable:    cr_boost_max_multiplier->_cr_boost_max_multiplier
          locked_stake_max_multiplier->_locked_stake_max_multiplier

    function setMultipliers(uint256 _locked_stake_max_multiplier, uint256 _cr_boost_max_multiplier) external onlyByOwnGov {
        require(_locked_stake_max_multiplier >= 1, "Multiplier must be greater than or equal to 1");
        require(_cr_boost_max_multiplier >= 1, "Max CR Boost must be greater than or equal to 1");

        locked_stake_max_multiplier = _locked_stake_max_multiplier;
        cr_boost_max_multiplier = _cr_boost_max_multiplier;
        
        emit MaxCRBoostMultiplier(cr_boost_max_multiplier);
        emit LockedStakeMaxMultiplierUpdated(locked_stake_max_multiplier);
    }

  function name:setLockedStakeTimeForMinAndMaxMultiplier
  event name:  LockedStakeTimeForMaxMultiplier
  variable:    locked_stake_time_for_max_multiplier->_locked_stake_time_for_max_multiplier

  • StakingRewardsDualV2.sol
    function name:setRewardsDuration
    event name:  RewardsDurationUpdated
    variable:    rewardsDuration->_rewardsDuration

  function name:setMultipliers
  event name:  MaxCRBoostMultiplier
          LockedStakeMaxMultiplierUpdated
  variable:    cr_boost_max_multiplier->_cr_boost_max_multiplier
          locked_stake_max_multiplier->_locked_stake_max_multiplier

  function name:setLockedStakeTimeForMinAndMaxMultiplier
  event name:  LockedStakeTimeForMaxMultiplier
  variable:    locked_stake_time_for_max_multiplier->_locked_stake_time_for_max_multiplier

  • veFXSYieldDistributorV3.sol
    function name:setYieldDuration
    event name:  YieldDurationUpdated
    variable:    yieldDuration->_yieldDuration

  • FraxUniV3Farm_Volatile.sol
    function name:setMultipliers
    event name:  MaxVeFXSMultiplier
            LockedNFTMaxMultiplierUpdated
            veFXSPctForMaxBoostUpdated
    variable:    vefxs_max_multiplier->_vefxs_max_multiplier
            lock_max_multiplier->_lock_max_multiplier
            vefxs_per_frax_for_max_boost->_vefxs_per_frax_for_max_boost

  function name:setLockedNFTTimeForMinAndMaxMultiplier
  event name:  LockedNFTTimeForMaxMultiplier
  variable:    lock_time_for_max_multiplier->_lock_time_for_max_multiplier

  • veFXSYieldDistributorV2.sol
    function name:setYieldDuration
    event name:  YieldDurationUpdated
    variable:    yieldDuration->_yieldDuration

  • StakingRewardsDualV4.sol
    function name:setRewardsDuration
    event name:  RewardsDurationUpdated
    variable:    rewardsDuration->_rewardsDuration

  function name:setMultipliers
  event name:  MaxVeFXSMultiplier
          LockedStakeMaxMultiplierUpdated
          veFXSPerFraxForMaxBoostUpdated
  variable:    vefxs_max_multiplier->_vefxs_max_multiplier
          lock_max_multiplier->_lock_max_multiplier
          vefxs_per_frax_for_max_boost->_vefxs_per_frax_for_max_boost

  function name:setLockedStakeTimeForMinAndMaxMultiplier
  event name:  LockedStakeTimeForMaxMultiplier
  variable:    lock_time_for_max_multiplier->_lock_time_for_max_multiplier

  • StakingRewardsDual.sol
    function name:setRewardsDuration
    event name:  RewardsDurationUpdated
    variable:    rewardsDuration->_rewardsDuration

  function name:setMultipliers
  event name:  MaxCRBoostMultiplier
          LockedStakeMaxMultiplierUpdated
  variable:    cr_boost_max_multiplier->_cr_boost_max_multiplier
          locked_stake_max_multiplier->_locked_stake_max_multiplier

  function name:setLockedStakeTimeForMinAndMaxMultiplier
  event name:  LockedStakeTimeForMaxMultiplier
  variable:    locked_stake_time_for_max_multiplier->_locked_stake_time_for_max_multiplier

  • StakingRewardsDualV3.sol
    function name:setRewardsDuration
    event name:  RewardsDurationUpdated
    variable:    rewardsDuration->_rewardsDuration

  function name:setMultipliers
  event name:  MaxVeFXSMultiplier
          LockedStakeMaxMultiplierUpdated
          veFXSPerFraxForMaxBoostUpdated
  variable:    vefxs_max_multiplier->_vefxs_max_multiplier
          lock_max_multiplier->_lock_max_multiplier
          vefxs_per_frax_for_max_boost->_vefxs_per_frax_for_max_boost

  function name:setLockedStakeTimeForMinAndMaxMultiplier
  event name:  LockedStakeTimeForMaxMultiplier
  variable:    lock_time_for_max_multiplier->_lock_time_for_max_multiplier

  • FraxFarmBSC_Dual_V5.sol
    function name:setRewardsDuration
    event name:  RewardsDurationUpdated
    variable:    rewardsDuration->_rewardsDuration

  function name:setMultipliers
  event name:  LockedStakeMaxMultiplierUpdated
  variable:    lock_max_multiplier->_lock_max_multiplier

  function name:setLockedStakeTimeForMinAndMaxMultiplier
  event name:  LockedStakeTimeForMaxMultiplier
  variable:    lock_time_for_max_multiplier->_lock_time_for_max_multiplier

  • MigratableFarmBSC.sol
    function name:setRewardsDuration
    event name:  RewardsDurationUpdated
    variable:    rewardsDuration->_rewardsDuration

  function name:setMultipliers
  event name:  LockedStakeMaxMultiplierUpdated
  variable:    locked_stake_max_multiplier->_locked_stake_max_multiplier

  function name:setLockedStakeTimeForMinAndMaxMultiplier
  event name:  LockedStakeTimeForMaxMultiplier
  variable:    locked_stake_time_for_max_multiplier->_locked_stake_time_for_max_multiplier

  • AnyswapV4ERC20.sol
    function name:changeVault
    event name:  LogChangeVault
    variable:    pendingVault->newVault

  function name:changeMPCOwner
  event name:  LogChangeMPCOwner
  variable:    pendingVault->newVault

  • FraxCrossChainFarm.sol
    function name:setMultipliers
    event name:  MaxVeFXSMultiplier
            LockedStakeMaxMultiplierUpdated
            veFXSPerFraxForMaxBoostUpdated
    variable:    vefxs_max_multiplier->_vefxs_max_multiplier
            lock_max_multiplier->_lock_max_multiplier
            vefxs_per_frax_for_max_boost->_vefxs_per_frax_for_max_boost

  function name:setLockedStakeTimeForMinAndMaxMultiplier
  event name:  LockedStakeTimeForMaxMultiplier
  variable:    lock_time_for_max_multiplier->_lock_time_for_max_multiplier

  • FraxCrossChainFarmV2.sol
    function name:setMultipliers
    event name:  MaxVeFXSMultiplier
            LockedStakeMaxMultiplierUpdated
            veFXSPerFraxForMaxBoostUpdated
    variable:    vefxs_max_multiplier->_vefxs_max_multiplier
            lock_max_multiplier->_lock_max_multiplier
            vefxs_per_frax_for_max_boost->_vefxs_per_frax_for_max_boost

  function name:setLockedStakeTimeForMinAndMaxMultiplier
  event name:  LockedStakeTimeForMaxMultiplier
  variable:    lock_time_for_max_multiplier->_lock_time_for_max_multiplier

  • veFXSYieldDistributorV4.sol
    function name:setYieldDuration
    event name:  YieldDurationUpdated
    variable:    yieldDuration->_yieldDuration

  • CommunalFarm.sol
    function name:setRewardsDuration
    event name:  RewardsDurationUpdated
    variable:    rewardsDuration->_rewardsDuration

  function name:setMultipliers
  event name:  LockedStakeMaxMultiplierUpdated
  variable:    lock_max_multiplier->_lock_max_multiplier

  function name:setLockedStakeTimeForMinAndMaxMultiplier
  event name:  LockedStakeTimeForMaxMultiplier
  variable:    lock_time_for_max_multiplier->_lock_time_for_max_multiplier

  • FraxUniV3Farm_Stable.sol
    function name:setMultipliers
    event name:  MaxVeFXSMultiplier
            LockedStakeMaxMultiplierUpdated
            veFXSPerFraxForMaxBoostUpdated
    variable:    vefxs_max_multiplier->_vefxs_max_multiplier
            lock_max_multiplier->_lock_max_multiplier
            vefxs_per_frax_for_max_boost->_vefxs_per_frax_for_max_boost

  function name:setLockedNFTTimeForMinAndMaxMultiplier
  event name:  LockedNFTTimeForMaxMultiplier
  variable:    lock_time_for_max_multiplier->_lock_time_for_max_multiplier

  • StakingRewardsDualV5.sol
    function name:setRewardsDuration
    event name:  RewardsDurationUpdated
    variable:    rewardsDuration->_rewardsDuration

  function name:setMultipliers
  event name:  MaxVeFXSMultiplier
          LockedStakeMaxMultiplierUpdated
          veFXSPerFraxForMaxBoostUpdated
  variable:    vefxs_max_multiplier->_vefxs_max_multiplier
          lock_max_multiplier->_lock_max_multiplier
          vefxs_per_frax_for_max_boost->_vefxs_per_frax_for_max_boost

  function name:setLockedStakeTimeForMinAndMaxMultiplier
  event name:  LockedStakeTimeForMaxMultiplier
  variable:    lock_time_for_max_multiplier->_lock_time_for_max_multiplier

  function name:setRewardsDuration
  event name:  RewardsDurationUpdated
  variable:    rewardsDuration->_rewardsDuration

  • StakingRewardsMultiGauge.sol
    function name:setRewardsDuration
    event name:  RewardsDurationUpdated
    variable:    rewardsDuration->_rewardsDuration

  function name:setMultipliers
  event name:  MaxVeFXSMultiplier
          LockedStakeMaxMultiplierUpdated
          veFXSPerFraxForMaxBoostUpdated
  variable:    vefxs_max_multiplier->_vefxs_max_multiplier
          lock_max_multiplier->_lock_max_multiplier
          vefxs_per_frax_for_max_boost->_vefxs_per_frax_for_max_boost

  function name:setLockedStakeTimeForMinAndMaxMultiplier
  event name:  LockedStakeTimeForMaxMultiplier
  variable:    lock_time_for_max_multiplier->_lock_time_for_max_multiplier

  • celrFRAX.sol
    function name:updateBridge
    event name:  BridgeUpdated
    variable:    bridge->_bridge

  • AnyswapV5ERC20.sol
    function name:changeVault
    event name:  LogChangeVault
    variable:    pendingVault->newVault

  function name:changeMPCOwner
  event name:  LogChangeMPCOwner
  variable:    pendingVault->newVault

  • celrFXS.sol
    function name:updateBridge
    event name:  BridgeUpdated
    variable:    bridge->_bridge

  • AnyswapV4ERC20.sol
    function name:changeVault
    event name:  LogChangeVault
    variable:    pendingVault->newVault

  function name:changeMPCOwner
  event name:  LogChangeMPCOwner
  variable:    pendingVault->newVault

Do you find our results useful? Your reply and invaluable suggestions would be greatly appreciated, and are vital for improving our tool. Thanks a lot for your time!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants