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

Gas Optimizations #271

Open
code423n4 opened this issue Jun 2, 2022 · 1 comment
Open

Gas Optimizations #271

code423n4 opened this issue Jun 2, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

  • uint is always >= 0, can't be negative:
  function setFees(uint256 _lockIncentive)
    ...
    if (_lockIncentive >= 0 && _lockIncentive <= 30)
  • No need for safe math where overflow/underflow is impossible, e.g.:
  if (_balance < _amount) {
  _amount = _withdrawSome(_gauge, _amount.sub(_balance));
  • When you declared the named return value, then you do not need to return afterward explicitly:
  function withdraw(IERC20 _asset) external returns (uint256 balance) {
   ...
   return balance;
  • No need to initialize to default values, e.g.:
  uint256 public periodFinish = 0;
  uint256 public rewardRate = 0;
  uint256 public queuedRewards = 0;
  uint256 public currentRewards = 0;
  uint256 public historicalRewards = 0;
  • Repeated storage access should be cached, e.g. veAsset is accessed 3 times here:
        uint256 veAssetBalance = IERC20(veAsset).balanceOf(address(this));
        if (veAssetBalance > 0) {
            IERC20(veAsset).safeTransfer(staker, veAssetBalance);
        }
        //increase ammount
        uint256 veAssetBalanceStaker = IERC20(veAsset).balanceOf(staker);
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jun 2, 2022
code423n4 added a commit that referenced this issue Jun 2, 2022
@GalloDaSballo
Copy link
Collaborator

## No need for safe math where overflow/underflow is impossible, e.g.:
Saves 20 gas

Repeated storage access should be cached, e.g. veAsset is accessed 3 times here:

Saves 97 + 94 gas

Saves less than 500 gas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

2 participants