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

Open
code423n4 opened this issue Feb 23, 2022 · 1 comment
Open

Gas Optimizations #88

code423n4 opened this issue Feb 23, 2022 · 1 comment
Labels
bug Something isn't working duplicate This issue or pull request already exists G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

> 0 is less efficient than != 0 for uint in require condition

Ref: https://twitter.com/GalloDaSballo/status/1485430908165443590

contracts/MarginAccount.sol:150:        require(amount > 0, "Add non-zero margin");

Use custom errors

Solidity ^0.8.4 allow the use of custom errors to optimize gas usage.
https://blog.soliditylang.org/2021/04/21/custom-errors/

Cache array length

contracts/ClearingHouse.sol:122: for (uint i = 0; i < amms.length; i++) {
contracts/ClearingHouse.sol:130: for (uint i = 0; i < amms.length; i++) {
contracts/ClearingHouse.sol:170: for (uint i = 0; i < amms.length; i++) {
contracts/ClearingHouse.sol:194: for (uint i = 0; i < amms.length; i++) { // liquidate all positions
contracts/ClearingHouse.sol:251: for (uint i = 0; i < amms.length; i++) {
contracts/ClearingHouse.sol:263: for (uint i = 0; i < amms.length; i++) {
contracts/ClearingHouse.sol:277: for (uint i = 0; i < amms.length; i++) {

        uint namms = amms.length;
        for (uint i = 0; i < namms; i++) {

Functions can be mark as external

https://github.com/code-423n4/2022-02-hubble/blob/ed1d885d5dbc2eae24e43c3ecbf291a0f5a52765/contracts/ClearingHouse.sol#L148
function liquidateMaker(address maker) override public whenNotPaused {
https://github.com/code-423n4/2022-02-hubble/blob/ed1d885d5dbc2eae24e43c3ecbf291a0f5a52765/contracts/ClearingHouse.sol#L153
function liquidateTaker(address trader) override public whenNotPaused {

Safe math can be put in unchecked block

For example, ClearingHouse.sol#211-215 can be put in unchecked block
https://github.com/code-423n4/2022-02-hubble/blob/ed1d885d5dbc2eae24e43c3ecbf291a0f5a52765/contracts/ClearingHouse.sol#L210

        if (liquidationFee > 0) {
            uint toInsurance = liquidationFee / 2;
            marginAccount.transferOutVusd(address(insuranceFund), toInsurance);
            marginAccount.transferOutVusd(_msgSender(), liquidationFee - toInsurance);
        }

Unnecessary checks

There are implicit assert as commented in the code
https://github.com/code-423n4/2022-02-hubble/blob/ed1d885d5dbc2eae24e43c3ecbf291a0f5a52765/contracts/AMM.sol#L487
https://github.com/code-423n4/2022-02-hubble/blob/ed1d885d5dbc2eae24e43c3ecbf291a0f5a52765/contracts/AMM.sol#L511

Use stuct storage instead of memory

A struct in storage is call by ref which don't need to copy the contents to memory
https://github.com/code-423n4/2022-02-hubble/blob/ed1d885d5dbc2eae24e43c3ecbf291a0f5a52765/contracts/AMM.sol#L528

Unused library

https://github.com/code-423n4/2022-02-hubble/blob/ed1d885d5dbc2eae24e43c3ecbf291a0f5a52765/contracts/Oracle.sol#L13

    using SafeCast for int256;

Variables can be set immutable

https://github.com/code-423n4/2022-02-hubble/blob/ed1d885d5dbc2eae24e43c3ecbf291a0f5a52765/contracts/MarginAccountHelper.sol

    IMarginAccount marginAccount;
    VUSD vusd;
    IERC20 public reserveToken;
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Feb 23, 2022
code423n4 added a commit that referenced this issue Feb 23, 2022
@atvanguard
Copy link
Collaborator

Good but duplicate.

@atvanguard atvanguard added the duplicate This issue or pull request already exists label Feb 26, 2022
@CloudEllie CloudEllie reopened this Mar 30, 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

3 participants