Summary
The provided audit concerns optimizing gas usage by replacing a require()
statement with Custom Errors.
Vulnerability Detail
The require()
statement in the UnripeFacet::chop
function is currently used to check if underlying amount is less or equal to zero. This check could potentially be optimized to use a Custom Error instead.
Impact
Gas optimization is the primary impact of this change. By using Custom Errors, gas costs could potentially be reduced.
Code Snippet
function chop(
address unripeToken,
uint256 amount,
LibTransfer.From fromMode,
LibTransfer.To toMode
) external payable nonReentrant returns (uint256) {
uint256 supply = IBean(unripeToken).totalSupply();
amount = LibTransfer.burnToken(IBean(unripeToken), amount, msg.sender, fromMode);
(address underlyingToken, uint256 underlyingAmount) = LibChop.chop(
unripeToken,
amount,
supply
);
require(underlyingAmount > 0, "Chop: no underlying");
IERC20(underlyingToken).sendToken(underlyingAmount, msg.sender, toMode);
emit Chop(msg.sender, unripeToken, amount, underlyingAmount);
return underlyingAmount;
}
Tool used
Manual Review
Recommendation
The function should use Custom Errors as shown below:
function chop(
address unripeToken,
uint256 amount,
LibTransfer.From fromMode,
LibTransfer.To toMode
) external payable nonReentrant returns (uint256) {
uint256 supply = IBean(unripeToken).totalSupply();
amount = LibTransfer.burnToken(IBean(unripeToken), amount, msg.sender, fromMode);
(address underlyingToken, uint256 underlyingAmount) = LibChop.chop(
unripeToken,
amount,
supply
);
if(underlyingAmount <= 0) {
revert("Chop: no underlying");
}
IERC20(underlyingToken).sendToken(underlyingAmount, msg.sender, toMode);
emit Chop(msg.sender, unripeToken, amount, underlyingAmount);
return underlyingAmount;
}