function convertToShares(uint256 assets) public view override returns (uint256 shares) {
uint256 supply = totalSupply();
if (supply == 0) {
// Scales assets by the value of a single unit of fCash
uint256 unitfCashValue = _getPresentValue(uint256(Constants.INTERNAL_TOKEN_PRECISION));
return (assets * uint256(Constants.INTERNAL_TOKEN_PRECISION)) / unitfCashValue;
}
return (assets * totalSupply()) / totalAssets();
}
Noticed that totalSupply() is called twice. Can be optimized to
function convertToShares(uint256 assets) public view override returns (uint256 shares) {
uint256 supply = totalSupply();
if (supply == 0) {
// Scales assets by the value of a single unit of fCash
uint256 unitfCashValue = _getPresentValue(uint256(Constants.INTERNAL_TOKEN_PRECISION));
return (assets * uint256(Constants.INTERNAL_TOKEN_PRECISION)) / unitfCashValue;
}
return (assets * supply) / totalAssets();
}
Using Clone instead of CREATE2
CREATE2 is consuming a lot of gas while Clone consume 10 times less gas than CREATE2
The WrappedFCash contract already used initialize pattern, so it is already compatible with Clone.
Clone is smaller than Beacon.
Read more: https://blog.logrocket.com/cloning-solidity-smart-contracts-factory-pattern/
https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/notional-wrapped-fcash/contracts/proxy/WrappedfCashFactory.sol#L26-L37
This part use Create2 to deploy Beacon, can be optimized to use Clone.
You may use clone with any kind of upgradable proxy to achieve upgradability.
Use solidity custom error instead of revert message
It not only provide better gas optimization but also reduce contract size
https://blog.soliditylang.org/2021/04/21/custom-errors/
Applied to all area with require(...)
Can be written like this
Duplicated function call on convertToShares
https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/notional-wrapped-fcash/contracts/wfCashERC4626.sol#L52-L61
Noticed that totalSupply() is called twice. Can be optimized to