sherlock-audit / 2024-07-sense-points-marketplace-judging

2 stars 0 forks source link

Howling Marigold Elk - Gas Optimization of convertRewardsToPTokens Function #195

Closed sherlock-admin4 closed 2 weeks ago

sherlock-admin4 commented 2 weeks ago

Howling Marigold Elk

Low/Info

Gas Optimization of convertRewardsToPTokens Function

Summary

The optimized version introduces a conditional check before performing the division operation. By checking if _amountToConvert < rewardsPerPToken, the function can revert early without performing unnecessary operations. This saves gas in cases where the amount is too small to convert. By moving the if (_amountToConvert < rewardsPerPToken) check before the transfer, the function potentially avoids an expensive transfer and division operation, resulting in a more efficient execution.(https://github.com/sherlock-audit/2024-07-sense-points-marketplace/blob/main/point-tokenization-vault/contracts/PointTokenVault.sol#L229-L254)

Tool used

Manual Review

Recommendation

    function convertRewardsToPTokens(address _receiver, bytes32 _pointsId, uint256 _amountToConvert) public {
        RedemptionParams memory params = redemptions[_pointsId];
        (ERC20 rewardToken, uint256 rewardsPerPToken, bool isMerkleBased) =
            (params.rewardToken, params.rewardsPerPToken, params.isMerkleBased);

        if (address(rewardToken) == address(0)) {
            revert RewardsNotReleased();
        }

        if (isMerkleBased) {
            revert CantConvertMerkleRedemption();
        }

+       if (_amountToConvert < rewardsPerPToken) {
+           revert AmountTooSmall();
+       }

        rewardToken.safeTransferFrom(msg.sender, address(this), _amountToConvert);

        uint256 pTokensToMint = FixedPointMathLib.divWadDown(_amountToConvert, rewardsPerPToken); // Round down for mint.

-       // Dust guard.
-       if (pTokensToMint == 0) {
-           revert AmountTooSmall();
-       }

        pTokens[_pointsId].mint(_receiver, pTokensToMint);

        emit RewardsConverted(msg.sender, _receiver, _pointsId, _amountToConvert);
    }