code-423n4 / 2022-04-phuture-findings

0 stars 0 forks source link

Gas Optimizations #29

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Usage of != 0 instead of > 0 can reduce the gas cost

Target codebase

2022-04-phuture/contracts/IndexLogic.sol 76,44: require(lastAssetBalanceInBase > 0, "Index: INSUFFICIENT_AMOUNT"); 86,17: if (fee > 0) { 98,23: require(value > 0, "Index: INSUFFICIENT_AMOUNT"); 114,21: if (fee > 0) { 141,29: if (lastOrderId > 0) {

2022-04-phuture/contracts/ManagedIndexReweightingLogic.sol 56,19: if (i > 0) { 61,27: if (newWeight > 0) { 98,24: if (shares > 0) {

2022-04-phuture/contracts/PhutureIndex.sol 56,24: if (timePassed > 0) { 64,21: if (fee > 0) {

2022-04-phuture/contracts/TopNMarketCapIndex.sol 56,24: if (weight > 0) {

2022-04-phuture/contracts/TopNMarketCapReweightingLogic.sol 58,32: if (shares > 0) { 79,32: if (weight > 0) { 106,24: if (shares > 0) {

2022-04-phuture/contracts/vToken.sol 160,26: if (_totalSupply > 0) {

2022-04-phuture/contracts/libraries/FullMath.sol 35,37: require(denominator > 0); 122,43: if (mulmod(a, b, denominator) > 0) {

2022-04-phuture/contracts/libraries/IndexLibrary.sol 29,35: require(_assetPerBaseInUQ > 0, "IndexLibrary: ORACLE");

2022-04-phuture/contracts/libraries/NAV.sol 49,24: require(shares > 0, "NAV: INSUFFICIENT_AMOUNT"); 59,24: require(amount > 0, "NAV: INSUFFICIENT_SHARES_BURNED");

Potential workaround

Simply using != instead of > 0 can reduce the gas cost

Not defining 0 can reduce the gas cost

Target codebase

Following codebase sets 0 for uint in the for loop. But setting 0 is not needed, and the gas cost can be reduced by omitting it.

https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/UniswapV2PathPriceOracle.sol#L34

https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/UniswapV2PathPriceOracle.sol#L49

Potential workaround

Simply avoiding setting 0 can reduce the gas cost.

for (uint i; i < path.length - 1; i++) {