Omniscia Euler Finance Audit

BalanceUtils Code Style Findings

BalanceUtils Code Style Findings

BUL-01C: Inefficient mapping Lookups

Description:

The linked statements perform key-based lookup operations on mapping declarations from storage multiple times for the same key redundantly.

Example:

src/EVault/shared/BalanceUtils.sol
18function increaseBalance(
19 VaultCache memory vaultCache,
20 address account,
21 address sender,
22 Shares amount,
23 Assets assets
24) internal {
25 if (account == address(0)) revert E_BadSharesReceiver();
26
27 (Shares origBalance, bool balanceForwarderEnabled) = vaultStorage.users[account].getBalanceAndBalanceForwarder();
28 Shares newBalance = origBalance + amount;
29
30 vaultStorage.users[account].setBalance(newBalance);
31 vaultStorage.totalShares = vaultCache.totalShares = vaultCache.totalShares + amount;
32
33 if (balanceForwarderEnabled) {
34 tryBalanceTrackerHook(account, newBalance.toUint(), false);
35 }
36
37 emit Transfer(address(0), account, amount.toUint());
38 emit Deposit(sender, account, assets.toUint(), amount.toUint());
39}

Recommendation:

As the lookups internally perform an expensive keccak256 operation, we advise the lookups to be cached wherever possible to a single local declaration that either holds the value of the mapping in case of primitive types or holds a storage pointer to the struct contained.

As the compiler's optimizations may take care of these caching operations automatically at-times, we advise the optimization to be selectively applied, tested, and then fully adopted to ensure that the proposed caching model indeed leads to a reduction in gas costs.

Alleviation (fb2dd77a6f):

The optimization has been partially applied as only the example referenced in the exhibit has been optimized. We advise all referenced instances by the exhibit (i.e. the Location lines) to be addressed, optimizing the codebase further.

Alleviation (0f2192ac81):

The optimization has been properly applied to all remaining instances rendering it addressed in full.

BUL-02C: Inexistent Propagation of Optimized Arithmetics

Description:

The referenced unchecked code blocks will not properly propagate to the overridden - functions thereby causing the syntax to be ineffective.

Example:

src/EVault/shared/BalanceUtils.sol
52Shares newBalance;
53unchecked {
54 newBalance = origBalance - amount;
55}

Recommendation:

We advise the syntax to be omitted from the BalanceUtils contract, and the unchecked code block to be introduced to the subtraction itself if needed.

Alleviation (fb2dd77a6ff9b7f710edb48e7eb5437e0db4fc1a):

The referenced unchecked code blocks have been omitted, and the calculations are now performed utilizing the newly introduced SharesLib::subUnchecked function that will properly perform the calculations in an unchecked code block.