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 saving optimizing storage #116

Open
code423n4 opened this issue Feb 1, 2022 · 0 comments
Open

Gas saving optimizing storage #116

code423n4 opened this issue Feb 1, 2022 · 0 comments
Labels
bug Something isn't working G (Gas Optimization) sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons

Comments

@code423n4
Copy link
Contributor

Handle

0x1f8b

Vulnerability details

Impact

Gas saving.

Proof of Concept

  • contracts\Types.sol: [TradeVars]

Moving the uint32 dexDetail close to address depositErc20 it's possible to save one slot:

struct TradeVars {
        uint depositValue;  
        IERC20 depositErc20;
        uint32 dexDetail;  // moved
        uint fees;
        uint depositAfterFees;      
        uint tradeSize;
        uint newHeld; 
        uint borrowValue;
        uint token0Price;
        uint totalHeld;
    }
  • contracts\Types.sol: [CloseTradeVars]

Moving the uint32 dexDetail; close to bool isPartialClose it's possible to save one slot:

struct CloseTradeVars {
        uint16 marketId;
        bool longToken;
        bool depositToken;
        uint closeRatio;          
        bool isPartialClose;        
        uint32 dexDetail; // moved
        uint closeAmountAfterFees;
        uint borrowed;
        uint repayAmount;     
        uint depositDecrease;
        uint depositReturn;  
        uint sellAmount;
        uint receiveAmount;
        uint token0Price;
        uint fees;
    }
  • contracts\periphery\QueryHelper.sol [LiqVars]

Moving marginLimit close to LiqStatus it's possible to save one storage slot.

struct LiqVars {
        LiqStatus status;
        uint32 marginLimit;  // moved
        uint lastUpdateTime;
        uint currentMarginRatio;
        uint cAvgMarginRatio;
        uint hAvgMarginRatio;
    }
  • contracts\periphery\QueryHelper.sol [LiquidateVars]

Moving isSellAllHeld and dexDetail it's possible to save 2 slots

struct LiquidateVars {// A variable holder for liquidation process
        uint16 marketId;
        bool longToken;
        bool isSellAllHeld; // moved
        uint32 dexDetail; // moved
        uint borrowed; 
        uint fees;      
        uint penalty; 
        uint remainAmountAfterFees;
        uint depositDecrease;   
        uint depositReturn;       
        uint sellAmount;
        uint receiveAmount;
        uint token0Price;
        uint outstandingAmount;
        uint finalRepayAmount;
    }
  • contracts\periphery\QueryHelper.sol [MarginRatioVars]

Moving multiplier and multiplier it's possible to save 2 slots

struct MarginRatioVars {
        address heldToken;
        address sellToken;
        address owner;
        uint16 multiplier; // moved
        uint8 decimals; // moved
        uint held;
        bytes dexData;
        uint price;
        uint cAvgPrice;
        uint hAvgPrice; 
        uint lastUpdateTime;
    }

Tools Used

Manual review.

Recommended Mitigation Steps

Apply the mentioned changes

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) sponsor acknowledged Technically the issue is correct, but we're not going to resolve it for XYZ reasons
Projects
None yet
Development

No branches or pull requests

2 participants