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

Open
code423n4 opened this issue Mar 19, 2022 · 1 comment
Open

Gas Optimizations #112

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

Comments

@code423n4
Copy link
Contributor

Gas Optimizations

Removing unused function parameter can save gas

Description

Removing unused function parameters can save gas. And it improves code clarity.

Findings

IHook.hook()
DepositHook.hook()
WithdrawHook.hook()

Recommended mitigation steps

Remove unused function parameter initialAmount.


An array's length should be cached to save gas in for-loops

Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.

Caching the array length in the stack saves around 3 gas per iteration.

Findings

AccountAccessController.allowAccounts()
AccountAccessController.blockAccounts()

Recommended mitigation steps

AccountAccessController.allowAccounts()

uint length = _accounts.length;

for (uint256 _i = 0; _i < length; _i++) {
    _allowedAccounts[_allowedAccountsIndex][_accounts[_i]] = true;
    emit AccountAllowed(_accounts[_i]);
}

AccountAccessController.blockAccounts()

uint length = _accounts.length;

for (uint256 _i = 0; _i < length; _i++) {
    _blockedAccounts[_blockedAccountsIndex][_accounts[_i]] = true;
    emit AccountBlocked(_accounts[_i]);
}

Use a temporary variable to cache repetitive storage reads

Reading the same variable from storage multiple times consumes unnecessary gas because SLOADs are expensive.

Findings

AccountAccessController.allowAccounts()

Repetitive storage access of variable: _accounts[_i]

1st access - L45
2nd access - L46

AccountAccessController.blockAccounts()

Repetitive storage access of variable: _accounts[_i]

1st access - L56
2nd access - L57

Recommended mitigation steps

Read the variable from storage once and store it in a temporary variable in memory for repetitive read access.


Unused named returns can be removed

Description

Removing unused named return variables can reduce gas usage and improve code clarity.

Recommended mitigation steps

Remove the unused named return variables or use them instead of creating additional variables.

Findings

PrePOMarketFactory.sol

L98 unnecessary named returns _newLongToken & _newShortToken

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Mar 19, 2022
code423n4 added a commit that referenced this issue Mar 19, 2022
@ramenforbreakfast
Copy link
Collaborator

Unused parameter - duplicate of #4
Caching recs duplicate of #5 and #18
Unused named returns is valid and we will take into consideration.

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