Omniscia Euler Finance Audit
BalanceForwarder Code Style Findings
BalanceForwarder Code Style Findings
BFR-01C: Inefficient mapping
Lookups
Type | Severity | Location |
---|---|---|
Gas Optimization | BalanceForwarder.sol:L27, L29, L30, L40, L42 |
Description:
The linked statements perform key-based lookup operations on mapping
declarations from storage multiple times for the same key redundantly.
Example:
22/// @inheritdoc IBalanceForwarder23function enableBalanceForwarder() public virtual reentrantOK {24 if (address(balanceTracker) == address(0)) revert E_BalanceForwarderUnsupported();25
26 address account = EVCAuthenticate();27 bool wasBalanceForwarderEnabled = vaultStorage.users[account].isBalanceForwarderEnabled();28
29 vaultStorage.users[account].setBalanceForwarder(true);30 balanceTracker.balanceTrackerHook(account, vaultStorage.users[account].getBalance().toUint(), false);31
32 if (!wasBalanceForwarderEnabled) emit BalanceForwarderStatus(account, true);33}34
35/// @inheritdoc IBalanceForwarder36function disableBalanceForwarder() public virtual reentrantOK {37 if (address(balanceTracker) == address(0)) revert E_BalanceForwarderUnsupported();38
39 address account = EVCAuthenticate();40 bool wasBalanceForwarderEnabled = vaultStorage.users[account].isBalanceForwarderEnabled();41
42 vaultStorage.users[account].setBalanceForwarder(false);43 balanceTracker.balanceTrackerHook(account, 0, false);44
45 if (wasBalanceForwarderEnabled) emit BalanceForwarderStatus(account, false);46}
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 (fb2dd77a6ff9b7f710edb48e7eb5437e0db4fc1a):
All referenced inefficient mapping
lookups have been optimized to the greatest extent possible, significantly reducing the gas cost of the functions the statements were located in.