Omniscia Euler Finance Audit

SafePermit2Lib Code Style Findings

SafePermit2Lib Code Style Findings

SPL-01C: Optimization of Successful Permit Transfer Flow

Description:

The SafePermit2Lib::safePermitTransferFrom function is presently inefficient as it will evaluate the success variable in its negated form twice if the Permit2 transfer was successful.

Example:

src/lib/SafePermit2Lib.sol
18/// @dev Transfer from using Permit2. If `permit2 == address(0)` this will use the normale `ERC20.transferFrom()` call.
19function safePermitTransferFrom(IERC20 _token, address _from, address _to, uint256 _value, address _permit2)
20 internal
21{
22 bool success;
23 bytes memory permit2Data;
24 bytes memory transferData;
25
26 if (_permit2 != address(0) && _value <= type(uint160).max) {
27 // it's safe to down-cast value to uint160
28 (success, permit2Data) =
29 _permit2.call(abi.encodeCall(IPermit2.transferFrom, (_from, _to, uint160(_value), address(_token))));
30 }
31
32 if (!success) {
33 (success, transferData) = _trySafeTransferFrom(_token, _from, _to, _value);
34 }
35
36 if (!success) revert Errors.SafeTransferFromFailed(permit2Data, transferData);
37}

Recommendation:

We advise the referenced if clause to be relocated inside the preceding if clause, ensuring that the success variable is re-evaluated solely if it has changed and thus optimizing the code's successful Permit2 transfer scenario gas cost without affecting the other scenarios.

Alleviation:

The Euler Finance team evaluated this exhibit but opted to acknowledge it in the current iteration of the codebase.