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

Open
code423n4 opened this issue Apr 20, 2022 · 0 comments
Open

Gas Optimizations #132

code423n4 opened this issue Apr 20, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

  1. Saving gas by removing = 0

If a variable was not set/initialized, it is assumed to have default value to 0
this implementation was used for saving more gas by removing = 0

##POC
https://blog.polymath.network/solidity-tips-and-tricks-to-save-gas-and-reduce-bytecode-size-c44580b218e6

##TOOLS USED
Visual Studio Code, Manual Review

##Mitigation Step
Remove = 0

##Occurances
https://github.com/code-423n4/2022-04-badger-citadel/blob/18f8c392b6fc303fe95602eba6303725023e53da/src/CitadelMinter.sol#L180-L182

        uint256 lockingAmount = 0;
        uint256 stakingAmount = 0;
        uint256 fundingAmount = 0;

Another Occurance

main/src/CitadelMinter.sol#L366
  1. Change uint256 i = 0 into uint i for saving more gas

this implementation can saving more gas for each loops.

##Tool Used
Manual Review

##Recommended Mitigation
Change it

Occurances

main/src/CitadelMinter.sol#L152
main/src/SupplySchedule.sol#L103
main/src/SupplySchedule.sol#L192
  1. using ++i than i++ for saving more gas

Using i++ instead ++i for all the loops, the variable i is incremented using i++. It is known that implementation by using ++i costs less gas per iteration than i++.

Tools Used

Manual Review

Occurances

main/src/CitadelMinter.sol#L152

  1. Reorder Struct can saving gas

Since address was 20 bytes and uint256 was 32 bytes on struct, this implementation below can be used for gas opt

##Tool Used
Manual Review

##Recommended Mitigation

https://github.com/code-423n4/2022-04-badger-citadel/blob/18f8c392b6fc303fe95602eba6303725023e53da/src/Funding.sol#L46-L53

    struct FundingParams {
        address discountManager;
        uint256 discount;
        uint256 minDiscount;
        uint256 maxDiscount;
        uint256 assetCumulativeFunded; /// persistent sum of asset amount in over lifetime of contract.
        uint256 assetCap; /// Max asset token that can be taken in by the contract (defines the cap for citadel sold)
    }
  1. Using short reason string can be used for saving more gas

Every reason string takes at least 32 bytes. Use short reason strings that fits in 32 bytes or it will become more expensive.

##Tool Used
Manual Review

##Occurance :

main/src/CitadelMinter.sol#L301
main/src/CitadelMinter.sol#L321
main/src/CitadelMinter.sol#L328
main/src/CitadelMinter.sol#L370
main/src/CitadelMinter.sol#L377
main/src/Funding.sol#L148
main/src/Funding.sol#L298
main/src/Funding.sol#L325
main/src/StakedCitadelVester.sol#L137
main/src/StakedCitadelVester.sol#L138

  1. Set value of constant to saving more gas

This implementation below can be saving more gas

##Tool Used
Manual Review

##Recommended Mitigation

    uint256 public constant INITIAL_VESTING_DURATION = 86400 * 21; // 21 days of vesting

changed to

    uint256 public constant INITIAL_VESTING_DURATION = 1814400; // 21 days of vesting
  1. vesting[msg.sender].claimedAmounts can be shorter

This implementation can be used for saving more gas, instead cache vesting[msg.sender].claimedAmounts it can be changed by using += instead.

##POC
https://www.tutorialspoint.com/solidity/solidity_operators.htm

##Tool Used
Manual Review

##Recommended Mitigation

            vesting[msg.sender].claimedAmounts += amount;
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Apr 20, 2022
code423n4 added a commit that referenced this issue Apr 20, 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

1 participant