code-423n4 / 2022-06-canto-v2-findings

0 stars 0 forks source link

Gas Optimizations #165

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

[G01] - Consider variables packing for structs and storage variables to save gas

The EVM fit everything into storage slots sequentially. Each slot is 32 bytes, if two variables do not exceed the 32bytes, only one slot will be created and gas will be save. Hence, for gas optimization purposes, it is useful to pack variables according to their type and bytes consumption.

File : BaseV1-core.sol Contract : BaseV1Pair « Uint8 » and « bool » should be placed after « address ». Paste lines 46 & 48 after line 63. BaseV1-core.sol:46 BaseV1-core.sol:48 BaseV1-core.sol:63

[G02] - It costs more gas to initialize variables to zero than to let the default of zero be applied.

File : BaseV1-core.sol BaseV1-core.sol:210 BaseV1-core.sol:226 BaseV1-core.sol:340

File : BaseV1-pheriphery.sol BaseV1-pheriphery.sol:154 BaseV1-pheriphery.sol:380

File: GovernorBravoDelegate.sol GovernorBravoDelegate.sol:66 GovernorBravoDelegate.sol:89

File: Comptroller.sol Comptroller.sol:131 Comptroller.sol:211 Comptroller.sol:742

[G03] - Pre-increments (++i) cost less gas than post-increment (i++), especially inside for loops.

Saving 6 gas per loop

File : BaseV1-core.sol BaseV1-core.sol:210 BaseV1-core.sol:340

File : BaseV1-pheriphery.sol BaseV1-pheriphery.sol:154 BaseV1-pheriphery.sol:380

File: GovernorBravoDelegate.sol GovernorBravoDelegate.sol:66 GovernorBravoDelegate.sol:89

File: Comptroller.sol Comptroller.sol:131 Comptroller.sol:211 Comptroller.sol:742

[G04] ++i should be unchecked{++I} when it is not possible for them to overflow (ie. In a for loop).

File : BaseV1-core.sol BaseV1-core.sol:210 BaseV1-core.sol:235 BaseV1-core.sol:340

File : BaseV1-pheriphery.sol BaseV1-pheriphery.sol:154 BaseV1-pheriphery.sol:380

File: GovernorBravoDelegate.sol GovernorBravoDelegate.sol:66 GovernorBravoDelegate.sol:89

File: Comptroller.sol Comptroller.sol:131 Comptroller.sol:211 Comptroller.sol:742

[G05] Use custom errors rather than revert()/require() strings to save gas.

File : BaseV1-core.sol BaseV1-core.sol:256 BaseV1-core.sol:275 BaseV1-core.sol:289 BaseV1-core.sol:291 BaseV1-core.sol:297 BaseV1-core.sol:307 BaseV1-core.sol:312 BaseV1-core.sol:416 BaseV1-core.sol:434 BaseV1-core.sol:524 BaseV1-core.sol:526 BaseV1-core.sol:527

File : BaseV1-pheriphery.sol BaseV1-pheriphery.sol:104 BaseV1-pheriphery.sol:106 BaseV1-pheriphery.sol:122 BaseV1-pheriphery.sol:123 BaseV1-pheriphery.sol:151 BaseV1-pheriphery.sol:241 BaseV1-pheriphery.sol:246 BaseV1-pheriphery.sol:313 BaseV1-pheriphery.sol:314 BaseV1-pheriphery.sol:405 BaseV1-pheriphery.sol:420 BaseV1-pheriphery.sol:433 BaseV1-pheriphery.sol:435 BaseV1-pheriphery.sol:446 BaseV1-pheriphery.sol:448

File : WETH.sol WETH.sol:29 WETH.sol:69 WETH.sol:72 WETH.sol:89 WETH.sol:90

File: GovernorBravoDelegate.sol GovernorBravoDelegate.sol:25 GovernorBravoDelegate.sol:26 GovernorBravoDelegate.sol:27 GovernorBravoDelegate.sol:42 GovernorBravoDelegate.sol:46 GovernorBravoDelegate.sol:47 GovernorBravoDelegate.sol:76 GovernorBravoDelegate.sol:85 GovernorBravoDelegate.sol:128 GovernorBravoDelegate.sol:129 GovernorBravoDelegate.sol:140 GovernorBravoDelegate.sol:150

File: Comptroller.sol Comptroller.sol:242 Comptroller.sol:492 Comptroller.sol:857

File : AccountantDelegator.sol AccountantDelegator.sol:42 AccountantDelegator.sol:43 AccountantDelegator.sol:123

File : TreasuryDelegator.sol TreasuryDelegator.sol:31 TreasuryDelegator.sol:32 TreasuryDelegator.sol:121

File : NoteInterest.sol NoteInterest.sol:167 NoteInterest.sol:180 NoteInterest.sol:193

[G06] Consider splitting require instead of using « && »

File : BaseV1-core.sol BaseV1-core.sol:291 BaseV1-core.sol:297 BaseV1-core.sol:416 BaseV1-core.sol:434 BaseV1-core.sol:471

File : BaseV1-pheriphery.sol BaseV1-pheriphery.sol:123

File: GovernorBravoDelegate.sol GovernorBravoDelegate.sol:42

[G07] For memory variable « x »  += « y » more gas efficient Than « x » = « x »+ « y »

File : BaseV1-core.sol BaseV1-core.sol:235 BaseV1-core.sol:345 BaseV1-core.sol:348

[G08] Consider factorizing mathematical operations to save gas

File : BaseV1-core.sol BaseV1-core.sol:254 —> _totalSupply can be place outside the min function to reduce the number of mathematical operations performed by the EVM by one. BaseV1-core.sol:332 —> could be transformed to get the same results in less operations: uint x0bis = x0/1e18; uint ybis = y/1e18; return x0 (ybis ybis ybis)+ y (x0bis x0bis x0bis); BaseV1-core.sol:336 —> could be transformed to get the same results in less operations: uint x0bis = x0/1e18; uint ybis = y/1e18; return x0 (3 (ybis ybis)+(x0bis x0bis));

File : BaseV1-pheriphery.sol BaseV1-pheriphery.sol:189 —> totalSupply can be place outside of the min function to reduce the number of mathematical operations performed by the EVM by one. BaseV1-pheriphery.sol:193 —> totalSupply can be place outside of the min function to reduce the number of mathematical operations performed by the EVM by one.

[G09] - Using >0 costs more gas than != 0 when used on a uint in a require() statement

File : BaseV1-core.sol BaseV1-core.sol:275 BaseV1-core.sol:289 BaseV1-core.sol:306 BaseV1-core.sol:468

File : BaseV1-pheriphery.sol BaseV1-pheriphery.sol:122 BaseV1-pheriphery.sol:123 BaseV1-pheriphery.sol:151

[G10] - consider revert()/require() message lower than 32 bytes to save additional gas

File : BaseV1-pheriphery.sol BaseV1-pheriphery.sol:104 BaseV1-pheriphery.sol:122 BaseV1-pheriphery.sol:123 BaseV1-pheriphery.sol:241 BaseV1-pheriphery.sol:246 BaseV1-pheriphery.sol:313 BaseV1-pheriphery.sol:314 BaseV1-pheriphery.sol:405 BaseV1-pheriphery.sol:420 BaseV1-pheriphery.sol:433 BaseV1-pheriphery.sol:435 BaseV1-pheriphery.sol:446 BaseV1-pheriphery.sol:448

File: GovernorBravoDelegate.sol GovernorBravoDelegate.sol:25 GovernorBravoDelegate.sol:26 GovernorBravoDelegate.sol:27 GovernorBravoDelegate.sol:42 GovernorBravoDelegate.sol:46 GovernorBravoDelegate.sol:47 GovernorBravoDelegate.sol:76 GovernorBravoDelegate.sol:85 GovernorBravoDelegate.sol:128 GovernorBravoDelegate.sol:129 GovernorBravoDelegate.sol:140 GovernorBravoDelegate.sol:150

File : WETH.sol WETH.sol:29 WETH.sol:72 WETH.sol:89 WETH.sol:90

File: Comptroller.sol Comptroller.sol:292

File : AccountantDelegator.sol AccountantDelegator.sol:42 AccountantDelegator.sol:43 AccountantDelegator.sol:123

File : AccountantDelegate.sol AccountantDelegate.sol:101

File : TreasuryDelegator.sol TreasuryDelegator.sol:31 TreasuryDelegator.sol:32 TreasuryDelegator.sol:121

File : NoteInterest.sol NoteInterest.sol:167 NoteInterest.sol:180 NoteInterest.sol:193

[G11] - Expressions for constant values such as a call to KECCAK256() should use immutable rather than constant

File: GovernorBravoDelegate.sol GovernorBravoDelegate.sol:15 GovernorBravoDelegate.sol:18

File: TreasureDelegate.sol TreasureDelegate.sol:12 TreasureDelegate.sol:13

[G12]- Duplicated Require()/Revert() checks should be refactored to a modifier or function

File: GovernorBravoDelegate.sol « Pending admin only » requirement is of application within different function : GovernorBravoDelegate.sol:26 GovernorBravoDelegate.sol:128 GovernorBravoDelegate.sol:140 GovernorBravoDelegate.sol:158

[G13]- Using safeMath is not needed for overflow check with solidity 0.8 Cf solidity note : https://docs.soliditylang.org/en/v0.8.9/080-breaking-changes.html - SafeMath is generally not needed starting with Solidity 0.8, since the compiler now has built in overflow checking.

File : NoteInterest.sol NoteInterest.sol:5 NoteInterest.sol:15

GalloDaSballo commented 2 years ago

Expressions for constant values such as a call to KECCAK256() should use immutable rather than constant

nooooooo -> https://twitter.com/GalloDaSballo/status/1543729080926871557

Poor formatting, max 200 gas saved