# Gas ## ✅ G-X: Don't Initialize Variables with Default Value ### 📝 Description Uninitialized variables are assigned with the types default value. Explicitly initializing a variable with it's default value costs unnecesary gas. Not overwriting the default for [stack variables](https://gist.github.com/IllIllI000/e075d189c1b23dce256cd166e28f3397) saves 8 gas. Storage and memory variables have larger savings ### 💡 Recommendation Delete useless variable declarations to save gas. ### 🔍 Findings: `kanjyo-contract/contracts/TokenVesting.sol#L314` [for (uint256 i = 0; i < size; i++) {](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/TokenVesting.sol#L314) `kanjyo-contract/contracts/TokenVesting.sol#L329` [for (uint256 i = 0; i < size; i++) {](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/TokenVesting.sol#L329) ## ✅ G-X: Cache Array Length Outside of Loop ### 📝 Description Caching the array length outside a loop saves reading it on each iteration, as long as the array's length is not changed during the loop. You save 3 gas by not reading `array.length` - 3 gas per instance - 27 gas saved ### 💡 Recommendation ### 🔍 Findings: `kanjyo-contract/contracts/TokenVesting.sol#L311` [uint256 size = issuerVestingSchedulesIds[msg.sender].length;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/TokenVesting.sol#L311) `kanjyo-contract/contracts/TokenVesting.sol#L326` [uint256 size = contractorVestingScheduleIds[msg.sender].length;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/TokenVesting.sol#L326) ## ✅ G-X: Use != 0 instead of > 0 for Unsigned Integer Comparison ### 📝 Description Use != 0 when comparing uint variables to zero, which cannot hold values below zero ### 💡 Recommendation You should change from `> 0` to `!=0`. ### 🔍 Findings: `kanjyo-contract/contracts/Payroll.sol#L84` [require(amount > 0 ether, "INSUFFICIENT AMOUNT");](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/Payroll.sol#L84) `kanjyo-contract/contracts/TokenVesting.sol#L81` [\_vestingSchedule[\_id].confirmedAt > 0,](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/TokenVesting.sol#L81) ## ✅ G-X: Solidity Version should be the latest ### 📝 Description Use a solidity version of at least 0.8.0 to get overflow protection without SafeMath Use a solidity version of at least 0.8.2 to get simple compiler automatic inlining Use a solidity version of at least 0.8.3 to get better struct packing and cheaper multiple storage reads Use a solidity version of at least 0.8.4 to get custom errors, which are cheaper at deployment than revert()/require() strings Use a solidity version of at least 0.8.10 to have external calls skip contract existence checks if the external call has a return value ### 💡 Recommendation You should consider with your team members whether you should choose the latest version. The latest version has its advantages, but also it has disadvantages, such as the possibility of unknown bugs. ### 🔍 Findings: `kanjyo-contract/contracts/Ctoken.sol#L1` [pragma solidity ^0.8.0;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/Ctoken.sol#L1) `kanjyo-contract/contracts/Payroll.sol#L2` [pragma solidity ^0.8.0;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/Payroll.sol#L2) `kanjyo-contract/contracts/TokenVesting.sol#L2` [pragma solidity ^0.8.0;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/TokenVesting.sol#L2) `kanjyo-contract/contracts/mock/AnotherMockERC20.sol#L2` [pragma solidity ^0.8.0;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/mock/AnotherMockERC20.sol#L2) `kanjyo-contract/contracts/mock/MockERC20.sol#L2` [pragma solidity ^0.8.0;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/mock/MockERC20.sol#L2) ## ✅ G-X: Use `++i` instead of `i++` ### 📝 Description You can get cheaper for loops (at least 25 gas, however can be up to 80 gas under certain conditions), by rewriting The post-increment operation (i++) boils down to saving the original value of i, incrementing it and saving that to a temporary place in memory, and then returning the original value; only after that value is returned is the value of i actually updated (4 operations).On the other hand, in pre-increment doesn't need to store temporary(2 operations) so, the pre-increment operation uses less opcodes and is thus more gas efficient. ### 💡 Recommendation You should change from i++ to ++i. ### 🔍 Findings: `kanjyo-contract/contracts/TokenVesting.sol#L314` [for (uint256 i = 0; i < size; i++) {](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/TokenVesting.sol#L314) `kanjyo-contract/contracts/TokenVesting.sol#L329` [for (uint256 i = 0; i < size; i++) {](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/TokenVesting.sol#L329) ## ✅ G-X: Use x=x+y instad of x+=y ### 📝 Description You can save about 35 gas per instance if you change from `x+=y**`\*\* to `x=x+y` This works equally well for subtraction, multiplication and division. ### 💡 Recommendation You should change from `x+=y` to `x=x+y`. ### 🔍 Findings: `kanjyo-contract/contracts/Payroll.sol#L78` [\_depositedAmount[msg.sender][\_token] += amount;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/Payroll.sol#L78) `kanjyo-contract/contracts/Payroll.sol#L85` [\_depositedAmount[msg.sender][address(0x0)] += amount;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/Payroll.sol#L85) `kanjyo-contract/contracts/Payroll.sol#L109` [\_depositedAmount[msg.sender][\_token] -= amount;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/Payroll.sol#L109) `kanjyo-contract/contracts/Payroll.sol#L116` [recipientAgreementCounts[\_paymentKey] += 1;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/Payroll.sol#L116) `kanjyo-contract/contracts/TokenVesting.sol#L115` [\_depositedAmount[msg.sender][\_token] -= \_amount;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/TokenVesting.sol#L115) `kanjyo-contract/contracts/TokenVesting.sol#L128` [\_depositedAmount[msg.sender][address(0x0)] += msg.value;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/TokenVesting.sol#L128) `kanjyo-contract/contracts/TokenVesting.sol#L137` [\_depositedAmount[msg.sender][\_token] += amount;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/TokenVesting.sol#L137) `kanjyo-contract/contracts/TokenVesting.sol#L153` [\_depositedAmount[msg.sender][\_token] -= amount;](https://github.com/Kanjyo-Protocol/kanjyo-contract/blob/main/contracts/TokenVesting.sol#L153)