Platform: Code4rena
Start Date: 17/03/2022
Pot Size: $30,000 USDC
Total HM: 8
Participants: 43
Period: 3 days
Judge: gzeon
Total Solo HM: 5
Id: 100
League: ETH
Rank: 41/43
Findings: 1
Award: $32.46
🌟 Selected for report: 0
🚀 Solo Findings: 0
32.4612 USDC - $32.46
Source file : https://github.com/code-423n4/2022-03-prepo/blob/main/contracts/core/AccountAccessController.sol
Finding :
In this loops at line 44-47
value is set 0. If a variable is not set/initialized, it is assumed to have the default value (0, false, 0x0 etc depending on the data type). So we can save gas.
for (uint256 _i = 0; _i < _accounts.length; _i++) { _allowedAccounts[_allowedAccountsIndex][_accounts[_i]] = true; emit AccountAllowed(_accounts[_i]); }
Change to
for (uint256 _i; _i < _accounts.length; _i++) { _allowedAccounts[_allowedAccountsIndex][_accounts[_i]] = true; emit AccountAllowed(_accounts[_i]); }
Other line at 55-58.
Finding :
Using i++ in loop is more expensive than ++i. For example, using remix to declare variable i
and using i++
we need 43446
and when we use ++i
we need 43440
. If we use this in loops, we will need more gas each loop. Change to this:
for (uint256 _i; _i < _accounts.length; ++_i) { _allowedAccounts[_allowedAccountsIndex][_accounts[_i]] = true; emit AccountAllowed(_accounts[_i]); }
Other line at 55-58
Finding :
If we want to save more line of code, readability and gas, we can use a ternary operator. In line 46-55
we have this 2 if-else
conditional statement.
if (_globalDepositAmount > _amount) { _globalDepositAmount -= _amount; } else { _globalDepositAmount = 0; } if (_accountToNetDeposit[_sender] > _amount) { _accountToNetDeposit[_sender] -= _amount; } else { _accountToNetDeposit[_sender] = 0; }
///gas cost is 89098
Change to :
_globalDepositAmount = _globalDepositAmount > _amount ? _globalDepositAmount -= _amount : 0; _accountToNetDeposit[_sender] = _accountToNetDeposit[_sender] > _amount ? _accountToNetDeposit[_sender] -= _amount : 0;
/// gas cost is 70632
#0 - ramenforbreakfast
2022-03-24T03:31:13Z