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

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

Gas Optimizations #80

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

Comments

@code423n4
Copy link
Contributor

Sublime-v1 gas optimization

1 code duplication. The following functions contain the same code. To save deployment gas costs you can use internal function for duplication code. If you can use the same error message you can save a little bit more gas. Please check the following example.

https://github.com/sublime-finance/sublime-v1/blob/46536a6d25df4264c1b217bd3232af30355dcb95/contracts/Verification/twitterVerifier.sol#L154-L178

function unregisterSelf() external {
string memory _userdata = userData[msg.sender].twitterId;
unregister(_userdata, msg.sender);
}

/**

  • @notice used to unregister user
  • @dev owners can unregister users
    */
    function unregisterUser(address _user) external onlyOwner {
    string memory _userdata = userData[_user].twitterId;
    unregister(_userdata, _user);
    }

function unregister(string memory _userdata, address _user) internal {
require(bytes(_userdata).length != 0, 'UR1');
delete twitterIdMap[_userdata];
delete userData[_user];
verification.unregisterMasterAddress(_user, address(this));
emit UserUnregistered(_user);
}

2 _minBorrowLimitInUSD is used only one time in the _limitBorrowedInUSD, so you can delete _minBorrowLimitInUSD to save gas.
https://github.com/sublime-finance/sublime-v1/blob/46536a6d25df4264c1b217bd3232af30355dcb95/contracts/PooledCreditLine/PooledCreditLine.sol#L395-L396

require(_minBorrowAmount.mul(_ratioOfPrices).div(10**_decimals >= _borrowLimitMin, 'ILB3');

3 use cache for pooledCLConstants[_id] in _accept. In _accept pooledCLConstants[_id] will be called many times. To save gas you can cache pooledCLConstants[_id].

https://github.com/sublime-finance/sublime-v1/blob/46536a6d25df4264c1b217bd3232af30355dcb95/contracts/PooledCreditLine/LenderPool.sol#L332-L343

function _accept(uint256 _id, uint256 _amount) internal {
LenderPoolConstants storage pooledCLConstant = pooledCLConstants[_id];
address _borrowAsset = pooledCLConstant.borrowAsset;
address _strategy = pooledCLConstant.borrowAssetStrategy;
IERC20(_borrowAsset).safeApprove(_strategy, _amount);
pooledCLVariables[_id].sharesHeld = SAVINGS_ACCOUNT.deposit(_borrowAsset, _strategy, address(this), _amount);

    POOLED_CREDIT_LINE.accept(_id, _amount);

    pooledCLConstant.borrowLimit = _amount;
    delete pooledCLConstant.startTime;
    delete pooledCLConstant.minBorrowAmount;
}
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Mar 31, 2022
code423n4 added a commit that referenced this issue Mar 31, 2022
@ritik99
Copy link
Collaborator

ritik99 commented Apr 12, 2022

Suggestions 1 and 3 are valid, for suggestion 2, we might stick to the current implementation since it improves readability

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