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

Open
code423n4 opened this issue May 28, 2022 · 0 comments
Open

Gas Optimizations #195

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

Comments

@code423n4
Copy link
Contributor

Gas report

Contest: Rubicon

Autor: Rotcivegaf

Scope:

Core:

Peripheral:

= Areas of concern
✔️ = Audited Contract
= Semi-Audited Contract(< 100%)

Issues

[G-01] != 0 to > 0 for uint in require() statements

  • RubiconMarket.sol, lines: 840, 879, 921, 945

Gas save: 6 for each require

* For the pragma versions minor than 0.8.13

[G-01] Repeated logged parameters

In BathToken.sol:

  • L575-L584, The events LogDeposit and Deposit log repeated the assets, shares and msg.sender
  • L607-L623, The events LogWithdraw and Withdraw log repeated the amountWithdrawn, _shares and msg.sender

Can remove from this parameters from LogDeposit and LogWithdraw events to save gas and clarify code

[G-02] Unnecessary type casting

In BathToken.sol L205: Remove the bytes cast

[G-03] Use function parameter

In BathToken.sol L214: Use market instead of RubiconMarketAddress to save the SLOAD

[G-04] <var>++ or <var> += 1 to ++<var>

  • In BathPair.sol, lines 311, 427, 480, 582
  • In BathToken.sol, line 635
  • In RubiconRouter.sol, line 169, 227

Use ++index instead of index++ and unchecked the increment:

for (...; i++) {
    ...
}

to

unchecked {
for (...;) {
    ...
    unchecked {
        ++i;
    }
}

[G-05] Caching length on for

  • In BathPair.sol, lines 311, 582
  • In BathToken.sol, line 635
  • In RubiconRouter.sol, line 169, 227

Caching the array length is more gas efficient:

for (uint256 i = 0; i < array.length; i++) {
  ...

to

uint256 arrayLength = array.length;

for (uint256 i = 0; i < arrayLength; i++) {
  ...

[G-06] Save the rewardsVestingWallet, feeBPS and bonusTokens.length in vars and directly bonusTokens[index] in release function call

In BathToken.sol, L629 distributeBonusTokenRewards function:

function distributeBonusTokenRewards(
    address receiver,
    uint256 sharesWithdrawn,
    uint256 initialTotalSupply
) internal {
    uint256 bonusTokensLength = bonusTokens.length;
    IBathBuddy _rewardsVestingWallet = rewardsVestingWallet;
    uint256 _feeBPS = feeBPS;

    if (bonusTokensLength > 0) {
        for (uint256 index = 0; index < bonusTokensLength; index++) {
            // Note: Shares already burned in Bath Token _withdraw

            // Pair each bonus token with a lightly adapted OZ Vesting wallet. Each time a user withdraws, they
            //  are released their relative share of this pool, of vested BathBuddy rewards
            // The BathBuddy pool should accrue ERC-20 rewards just like OZ VestingWallet and simply just release the withdrawer's relative share of releaseable() tokens
            if (_rewardsVestingWallet != IBathBuddy(0)) {
                _rewardsVestingWallet.release(
                    IERC20(bonusTokens[index]),
                    receiver,
                    sharesWithdrawn,
                    initialTotalSupply,
                    _feeBPS
                );
            }
        }
    }
}

[G-07] Save allowance[from][msg.sender] in var to save SLOAD gas cost

In BathToken.sol, L698 transferFrom function:

function transferFrom(
    address from,
    address to,
    uint256 value
) external returns (bool) {
    uint256 currentAllowance = allowance[from][msg.sender];
    if (currentAllowance != uint256(-1)) {
        allowance[from][msg.sender] = currentAllowance.sub(
            value
        );
    }
    _transfer(from, to, value);
    return true;
}

[G-08] Save IBathHouse(_bathHouse).getMarket() in a var

In BathToken.sol, L115: In initialize function save in a var and use msg.sender instead of _bathHouse to save SLOAD gas

function initialize(uint256 _maxOrderSizeBPS, int128 _shapeCoefNum)
    external
{
    require(!initialized);
    address _bathHouse = msg.sender; //Assume the initializer is BathHouse
    _rubiconMarket = IBathHouse(msg.sender).getMarket();

    require(
        _rubiconMarket !=
            address(0x0000000000000000000000000000000000000000) &&
            IBathHouse(msg.sender).initialized(),
        "BathHouse not initialized"
    );
    bathHouse = msg.sender;

    RubiconMarketAddress = _rubiconMarket;

...
@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels May 28, 2022
code423n4 added a commit that referenced this issue May 28, 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