Platform: Code4rena
Start Date: 07/01/2022
Pot Size: $80,000 USDC
Total HM: 21
Participants: 37
Period: 7 days
Judge: 0xean
Total Solo HM: 14
Id: 71
League: ETH
Rank: 24/37
Findings: 1
Award: $277.76
🌟 Selected for report: 5
🚀 Solo Findings: 0
🌟 Selected for report: Tomio
62.2711 INSURE - $21.79
32.6923 USDC - $32.69
Tomio
by using 'unchecked' you can save +-182 gas
before: https://github.com/code-423n4/2022-01-insure/blob/main/contracts/PoolTemplate.sol#L938 //22378 before
after:
function _sub(uint256 a, uint256 b) public pure returns (uint256) { if (a < b) { return 0; } else { unchecked {return a - b;} } }
//22196 after
Remix
used 'unchecked' in function _sub
🌟 Selected for report: Tomio
62.2711 INSURE - $21.79
32.6923 USDC - $32.69
Tomio
by changing the code from if (_credit == 0) {
to if (_credit != 0) {
and remove the else we can save gas when contract is deploy and we can save gas when _credit
is equal to 0. because if _credit
equal to 0 the original function will return 0 which a default value for uint256
Before: https://github.com/code-423n4/2022-01-insure/blob/main/contracts/PoolTemplate.sol#L776 // gas 24307
After:
function pendingPremium(address _index) external view returns (uint256) { uint256 _credit = indicies[_index].credit; if (_credit != 0) { return _sub( (_credit * rewardPerCredit) / MAGIC_SCALE_1E6, indicies[_index].rewardDebt ); } }
// gas 24286
Remix
🌟 Selected for report: Tomio
62.2711 INSURE - $21.79
32.6923 USDC - $32.69
Tomio
by saving totalLiquidity()
to memory can save more gas instead of doing double function call
Before: https://github.com/code-423n4/2022-01-insure/blob/main/contracts/PoolTemplate.sol#L829 // gas cost 23862
After:
function totalLiquidity() public view returns (uint256){ return 10; } function availableBalance()public view returns (uint256 _balance) { uint256 saveTotalLiquidity = totalLiquidity(); if (saveTotalLiquidity > 0) { return saveTotalLiquidity - lockedAmount; } else { return 0; } }
// gas cost 23840
Remix
🌟 Selected for report: Tomio
62.2711 INSURE - $21.79
32.6923 USDC - $32.69
Tomio
expensive gas
Before: https://github.com/code-423n4/2022-01-insure/blob/main/contracts/IndexTemplate.sol#L164 // gas 21512
Remix
function deposit(uint256 _amount) public returns (uint256 _mintAmount ) { uint256 _supply = 0; uint256 _totalLiquidity = 10; _mintAmount = _amount; if (_supply > 0 ){ if (_totalLiquidity == 0){ _mintAmount = _amount * _supply; }else{ _mintAmount = (_amount * _supply) / _totalLiquidity; } } }
/// 21444
#0 - 0xHaku
2022-01-26T10:23:23Z
@oishun1112 is this already fixed by another way?
#1 - oishun1112
2022-01-27T06:55:18Z
yes,
_mintAmount = _amount;
will only be done for the first time deposit(). this reduce gas for the first time, but increase after all.
3.723 INSURE - $1.30
1.9546 USDC - $1.95
Tomio
gas cost more by using true check in require templates[address(_template)].approval == true
change to templates[address(_template)].approval
because the require check expected a true value from templates[address(_template)].approval
https://github.com/code-423n4/2022-01-insure/blob/main/contracts/Factory.sol#L158 //gas 28518 before
function createMarket( IUniversalMarket _template, string memory _metaData, uint256[] memory _conditions, address[] memory _references ) public returns (address) { //check eligibility require( templates[address(_template)].approval, "ERROR: UNAUTHORIZED_TEMPLATE" ); } } // gas 28500 after
Remix
#0 - oishun1112
2022-01-13T17:26:12Z
🌟 Selected for report: Tomio
62.2711 INSURE - $21.79
32.6923 USDC - $32.69
Tomio
using double require instead of operator &&
can save more gas
https://github.com/code-423n4/2022-01-insure/blob/main/contracts/Vault.sol#L154
example:
using &&:
function check(uint x)public view{ require(x == 0 && x < 1 ); }
// gas cost 21630
using double require:
require(x == 0 ); require( x < 1); } } // gas cost 21622
2.4125 INSURE - $0.84
1.2666 USDC - $1.27
Tomio
The default value is already 0, so by not redeclare 0 value can save a little bit more gas
https://github.com/code-423n4/2022-01-insure/blob/main/contracts/IndexTemplate.sol#L280 https://github.com/code-423n4/2022-01-insure/blob/main/contracts/IndexTemplate.sol#L348 https://github.com/code-423n4/2022-01-insure/blob/main/contracts/IndexTemplate.sol#L381 https://github.com/code-423n4/2022-01-insure/blob/main/contracts/IndexTemplate.sol#L462 https://github.com/code-423n4/2022-01-insure/blob/main/contracts/IndexTemplate.sol#L655
Manual review
remove 0 value from: for (uint256 i = 0; to: for (uint256 i;
#0 - oishun1112
2022-01-13T14:52:44Z