code-423n4 / 2022-03-lifinance-findings

6 stars 4 forks source link

Gas Optimizations #146

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

1 - Short require strings save gas

Impact

Strings in solidity are handled in 32 byte chunks. A require string longer than 32 bytes uses more gas. Shortening these strings will save gas.

Proof of Concept

Several cases of this gas optimization were found. These are a few examples, but more may exist

  1. https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L84
  2. https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L95
  3. https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L102
  4. https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L104
  5. https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L113
  6. https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L121
  7. https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L124
  8. https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Libraries/LibDiamond.sol#L187

Tools Used

Manual analysis

Recommended Mitigation Steps

Shorten require strings

2 - Redundant assert

Impact

There are two asserts in WithdrawFacet that are not necessary. The transfer that happens immediately after the assert will revert if there is insufficient balance, so the assert does not provide any value.

Proof of Concept

The redundant assert calls are found at WithdrawFacet https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/WithdrawFacet.sol#L30 https://github.com/code-423n4/2022-03-lifinance/blob/main/src/Facets/WithdrawFacet.sol#L34

Tools Used

Manual analysis

Recommended Mitigation Steps

Remove the two asserts to save gas

3 - Public functions can be external

Impact

Declaring a function as external instead of public saves gas

Proof of Concept

Some functions can be declared external instead of public

Tools Used

Slither

Recommended Mitigation Steps

Change function from public to external to save gas

H3xept commented 2 years ago

Re Public functions can be external

Duplicate of #197

H3xept commented 2 years ago

Re Short require strings save gas

Duplicate of #100