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

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

Gas Optimizations #94

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

Comments

@code423n4
Copy link
Contributor

Unused Import

Impact

Importing ReentrancyGuard.sol is unnecessary in LongShortToken.sol because the import is never used

Proof of Concept

https://github.com/code-423n4/2022-03-prepo/blob/main/contracts/core/LongShortToken.sol

Tools Used

Manual analysis

Recommended Mitigation Steps

Remove this line from LongShortToken
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";

Save gas with unchecked

Impact

If arithmetic will not overflow, use unchecked to save gas. There are several locations where safeMath is not needed and unchecked can lower gas consumption.

Proof of Concept

In this code from Collateral.sol, _balanceAfter ≥ _balanceBefore so unchecked can be used

uint256 _balanceBefore = _baseToken.balanceOf(address(this));
_strategyController.withdraw(address(this), _owed);
uint256 _balanceAfter = _baseToken.balanceOf(address(this));

uint256 _amountWithdrawn = _balanceAfter - _balanceBefore;

Instead, replace the last line with

unchecked { uint256 _amountWithdrawn = _balanceAfter - _balanceBefore; }

Tools Used

Manual analysis

Recommended Mitigation Steps

Use unchecked for arithmetic gas optimizations

Redundant zero initialization

Impact

Solidity does not recognize null as a value, so uint variables are initialized to zero. Setting a uint variable to zero is redundant and can waste gas.

Proof of Concept

https://github.com/code-423n4/2022-03-prepo/blob/main/contracts/core/Collateral.sol#L81

uint256 _shares = 0;

Tools Used

Manual analysis

Recommended Mitigation Steps

Remove the redundant zero initialization
uint256 _shares;

@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

duplicate of #5 and #18

@ramenforbreakfast ramenforbreakfast added the duplicate This issue or pull request already exists label Mar 24, 2022
@gzeoneth gzeoneth removed the duplicate This issue or pull request already exists label Apr 3, 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 G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

3 participants