code-423n4 / 2022-07-fractional-findings

0 stars 0 forks source link

Gas Optimizations #645

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

VaultRegistry.totalSupply(vault) to token.totalSupply(id)

When the total supply of fractional tokens of a vault is needed, we can make a call VaultRegistry.totalSupply(vault), that gets the token/id and then calls FERC1155(info.token).totalSupply(info.id).

However, sometimes we already have these values, so with this method we have a staticall and two warm SLOAD extra. In the following lines, consider using directly FERC1155(token).totalSupply(id).

Buyout.sol: L71, L210, L267, L288 Migration.sol: L95, L200, L470

VaultInfo.id can be uint96

token and id can be packed in a single storage slot if instead of using uint256 we use uint96. The number of vaults won't realistically reach 2**96.

for-loop i++

In general, a for-loop like this can be made more gas efficient.

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

Consider rewriting it like this:

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

Lines

grep -r 'i++'

Migration.sol buyout can be immutable

SInce this variable doesn't change, it can be set immutable. It saves a SLOAD in every function.

nonReentrant modifier in join isn't needed

There's a nonReentrant modifier in Migration.sol#join. I don't a way to reenter from this function, so I suggest to remove it (to save gas of course).