wise-foundation / lending-audit

5 stars 4 forks source link

[WSY-03C] Inefficient `mapping` Lookups #47

Open vm06007 opened 1 year ago

vm06007 commented 1 year ago

WSY-03C: Inefficient mapping Lookups

Type Severity Location
Gas Optimization WiseSecurity.sol:L160, L163, L178, L209, L219, L226, L236

Description:

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

Example:

curveSwapInfoData[_poolToken] = _curveSwapStructData;
curveSwapInfoToken[_poolToken] = _curveSwapStructToken;

address curvePool = curveSwapInfoData[_poolToken].curvePool;
uint256 tokenIndexForApprove = _curveSwapStructToken.curvePoolTokenIndexFrom;

_safeApprove(
    ICurve(curvePool).coins(tokenIndexForApprove),
    curvePool,
    0
);

_safeApprove(
    ICurve(curvePool).coins(tokenIndexForApprove),
    curvePool,
    UINT256_MAX
);

address curveMetaPool = curveSwapInfoData[_poolToken].curveMetaPool;

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.

vm06007 commented 1 year ago

Team will prefer to keep this code unchanged. prepareCurvePools receives both parameters as memory marked and due to that it would not work when converting to strorage