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 #359

Open
code423n4 opened this issue May 25, 2022 · 0 comments
Open

Gas Optimizations #359

code423n4 opened this issue May 25, 2022 · 0 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Title: Using != is more gas efficient

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/Aura.sol#L68
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L139
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L210

Recommended Mitigation Steps:

    require(_amount != 0, "Must mint something");

========================================================================

Title: unnecessary variable set. the default value of uint is 0

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L35
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L38-L39

Recommended Mitigation Steps:
remove 0 value

========================================================================

Title: Using delete statement to empty rewards[msg.sender] can save gas

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L179
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraClaimZap.sol#L191-L192

Recommended Mitigation Steps:

    delete rewards[msg.sender];

========================================================================

Title: Using unchecked and prefix increment

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraClaimZap.sol#L143-L153
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L773

Recommended Mitigation Steps:

for (uint256 i = 0; i < rewardContracts.length;) {
            IBasicRewards(rewardContracts[i]).getReward(msg.sender, true);
        }
    unchecked{
        ++i; //@audit-info: Place here with unchecked
        }

========================================================================

Title: Unnecessary variable set of bool

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L114
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraVestedEscrow.sol#L33

Recommended Mitigation Steps:
the default value of bool is false. remove it for gas opt

========================================================================

Title: Use reward that already been cache

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L177-L179

Recommended Mitigation Steps:

if (reward > 0) {
            reward = 0;

========================================================================

Title: Using > is cheaper than >=

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L216-L217
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L829

Recommended Mitigation Steps:
just use > can save gas

	require(_delay > 1, "min delay"); //minimum 2 epochs of grace

========================================================================

Title: Gas opt to substract

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L484

Recommended Mitigation Steps:

	uint256 i = --len;

========================================================================

Title: Using calldata to store struct data type can save gas

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L583
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L627
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L769

Recommended Mitigation Steps:

	function checkpoints(address account, uint32 pos) external view virtual returns (DelegateeCheckpoint calldata) {

========================================================================

Title: Using storage to declare Struct variable inside function

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L521
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L600
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L794

Recommended Mitigation Steps:

    DelegateeCheckpoint storage ckpt = _checkpointsLookup(_checkpointedVotes[account], epoch);

========================================================================

Title: Using SafeMath for solidity >0.8

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraBalRewardPool.sol#L24
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraStakingProxy.sol#L35

Recommended Mitigation Steps:
it's better to remove using SafeMath for uint256 for solidity >0.8
reference: OpenZeppelin/openzeppelin-contracts#2465

========================================================================

Title: Cheaper to use ++ instead + 1

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraLocker.sol#L636

Recommended Mitigation Steps:

	low = ++mid;

========================================================================

Title: set as immutable

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraMerkleDrop.sol#L26

Recommended Mitigation Steps:
add immutable

========================================================================

Title: Using multiple require instead && can save gas

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraStakingProxy.sol#L159
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraStakingProxy.sol#L203
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/BalLiquidityProvider.sol#L48
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/BalLiquidityProvider.sol#L57

Recommended Mitigation Steps:

	require(_token != crv, "not allowed");
	require(_token != cvx, "not allowed");
	require(_token != cvxCrv, "not allowed");

========================================================================

Title: Using unchecked can save gas

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraVestedEscrow.sol#L162

Recommended Mitigation Steps:

unchecked{
	uint256 elapsed = _time - startTime;
}

========================================================================

Title: better increment

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/AuraVestedEscrow.sol#L100

Recommended Mitigation Steps:
Change i++ to ++i

========================================================================

Title: Gas improvement on calling SafeERC20.function

Proof of Concept:
https://github.com/code-423n4/2022-05-aura/blob/main/contracts/ExtraRewardsDistributor.sol#L15

Recommended Mitigation Steps:
by removing L#15 and directly call SafeERC20

Example L#93:

    SafeERC20.safeTransferFrom(_token, msg.sender, address(this), _amount);

========================================================================

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 25, 2022
code423n4 added a commit that referenced this issue May 25, 2022
@0xMaharishi 0xMaharishi added the duplicate This issue or pull request already exists label May 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

2 participants