deposit() gas will be from Avg 112668 to 112664 with this change according to the hardhat gas reporter.
2 Use unchecked for user.amount -= _amount. The underflow for this calculation is already checked in require statement, so you can use unchecked to save gas.
For example set() gas will be from Avg 41715 to 41611 with this change according to the hardhat gas reporter.
5 use unchecked for collateralAmount -= amount in FungibleAssetVaultForDAO. Underflow will never happen because require statement checked if collateralAmount is greater or equal than amount.
6 code duplication. The functions closePosition, liquidate, repurchase and claimExpiredInsuranceNFT have code duplication to delete position. Create an new internal function to save gas cost for deployment.
For example,
function deletePosition(uint256 _nftIndex) internal {
positionOwner[_nftIndex] = address(0);
delete positions[_nftIndex];
positionIndexes.remove(_nftIndex);
}
2022-04-jpegd gas optimization
1 delete cache for pool in deposit. The cached pool is used only one time in deposit, so you can delete it and save a little bit of gas cost.
https://github.com/code-423n4/2022-04-jpegd/blob/main/contracts/farming/LPFarming.sol#L220
deposit() gas will be from Avg 112668 to 112664 with this change according to the hardhat gas reporter.
2 Use unchecked for user.amount -= _amount. The underflow for this calculation is already checked in require statement, so you can use unchecked to save gas.
https://github.com/code-423n4/2022-04-jpegd/blob/main/contracts/farming/LPFarming.sol#L248
unchecked { user.amount -= _amount; }
withdraw() gas will be from Avg 85044 to 84891 with this change according to the hardhat gas reporter.
3 delete cache for pool in withdraw. The cached pool is used only one time in withdraw, so you can delete it and save a little bit of gas cost.
https://github.com/code-423n4/2022-04-jpegd/blob/main/contracts/farming/LPFarming.sol#L220
withdraw() gas will be from Avg 85044 to 85042 with this change according to the hardhat gas reporter.
4 use initial value for i und unchecked for increment in the loop.
https://github.com/code-423n4/2022-04-jpegd/blob/main/contracts/farming/LPFarming.sol#L281 https://github.com/code-423n4/2022-04-jpegd/blob/main/contracts/farming/LPFarming.sol#L348
For example,
for (uint256 pid; pid < length;) { // something executed unchecked { ++pid; } }
For example set() gas will be from Avg 41715 to 41611 with this change according to the hardhat gas reporter.
5 use unchecked for collateralAmount -= amount in FungibleAssetVaultForDAO. Underflow will never happen because require statement checked if collateralAmount is greater or equal than amount.
https://github.com/code-423n4/2022-04-jpegd/blob/main/contracts/vaults/FungibleAssetVaultForDAO.sol#L199
unchecked { collateralAmount -= amount; }
6 code duplication. The functions closePosition, liquidate, repurchase and claimExpiredInsuranceNFT have code duplication to delete position. Create an new internal function to save gas cost for deployment.
positionOwner[_nftIndex] = address(0); delete positions[_nftIndex]; positionIndexes.remove(_nftIndex);
https://github.com/code-423n4/2022-04-jpegd/blob/main/contracts/vaults/NFTVault.sol#L808-L810 https://github.com/code-423n4/2022-04-jpegd/blob/main/contracts/vaults/NFTVault.sol#L865-L867 https://github.com/code-423n4/2022-04-jpegd/blob/main/contracts/vaults/NFTVault.sol#L906-L908 https://github.com/code-423n4/2022-04-jpegd/blob/main/contracts/vaults/NFTVault.sol#L934-L936
For example, function deletePosition(uint256 _nftIndex) internal { positionOwner[_nftIndex] = address(0); delete positions[_nftIndex]; positionIndexes.remove(_nftIndex);
}