From 175a52c8f5df44a02f7842f532b25c8692578732 Mon Sep 17 00:00:00 2001 From: Josh Levine Date: Tue, 7 May 2024 16:18:03 -0700 Subject: [PATCH] feat: add amount too small check to convertRewardsToPTokens fnc --- contracts/PointTokenVault.sol | 11 +++++++- contracts/test/PointTokenVault.t.sol | 40 +++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/contracts/PointTokenVault.sol b/contracts/PointTokenVault.sol index 1b2f547..7628b93 100644 --- a/contracts/PointTokenVault.sol +++ b/contracts/PointTokenVault.sol @@ -74,6 +74,7 @@ contract PointTokenVault is UUPSUpgradeable, AccessControlUpgradeable, Multicall error PTokenAlreadyDeployed(); error DepositExceedsCap(); error PTokenNotDeployed(); + error AmountTooSmall(); constructor() { _disableInitializers(); @@ -170,7 +171,15 @@ contract PointTokenVault is UUPSUpgradeable, AccessControlUpgradeable, Multicall } rewardToken.safeTransferFrom(msg.sender, address(this), _amountToConvert); - pTokens[_pointsId].mint(_receiver, FixedPointMathLib.divWadDown(_amountToConvert, rewardsPerPToken)); // Round down for mint. + + 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); } diff --git a/contracts/test/PointTokenVault.t.sol b/contracts/test/PointTokenVault.t.sol index ea8f429..7e7605b 100644 --- a/contracts/test/PointTokenVault.t.sol +++ b/contracts/test/PointTokenVault.t.sol @@ -94,7 +94,7 @@ contract PointTokenVaultTest is Test { // Can withdraw with a different receiver vm.prank(vitalik); vm.expectEmit(true, true, true, true); - emit Withdraw(vitalik, toly, address(pointEarningToken), 0.5e18); + emit Withdraw(vitalik, toly, address(pointEarningToken), 0.5e18); pointTokenVault.withdraw(pointEarningToken, 0.5e18, toly); assertEq(pointEarningToken.balanceOf(vitalik), 0.623e18); @@ -550,6 +550,44 @@ contract PointTokenVaultTest is Test { pointTokenVault.convertRewardsToPTokens(vitalik, eigenPointsId, 1e18); } + function test_CantMintPTokensForRewardsAmountTooSmall() public { + bytes32 root = 0x4e40a10ce33f33a4786960a8bb843fe0e170b651acd83da27abc97176c4bed3c; + + bytes32[] memory proof = new bytes32[](1); + proof[0] = 0x6d0fcb8de12b1f57f81e49fa18b641487b932cdba4f064409fde3b05d3824ca2; + + vm.prank(merkleUpdater); + pointTokenVault.updateRoot(root); + + vm.prank(vitalik); + pointTokenVault.claimPTokens(PointTokenVault.Claim(eigenPointsId, 1e18, 1e18, proof), vitalik); + + rewardToken.mint(address(pointTokenVault), 3e18); + + vm.prank(operator); + pointTokenVault.setRedemption(eigenPointsId, rewardToken, 2e18, false); + + bytes32[] memory empty = new bytes32[](0); + vm.prank(vitalik); + pointTokenVault.redeemRewards(PointTokenVault.Claim(eigenPointsId, 2e18, 2e18, empty), vitalik); + + assertEq(rewardToken.balanceOf(vitalik), 2e18); + assertEq(pointTokenVault.pTokens(eigenPointsId).balanceOf(vitalik), 0); + + // Can't mint ptokens if the amount is too small + vm.prank(vitalik); + rewardToken.approve(address(pointTokenVault), 1); + vm.prank(vitalik); + vm.expectRevert(PointTokenVault.AmountTooSmall.selector); + pointTokenVault.convertRewardsToPTokens(vitalik, eigenPointsId, 1); + + // Can mint anything above the absolute minimum + vm.prank(vitalik); + rewardToken.approve(address(pointTokenVault), 2); + vm.prank(vitalik); + pointTokenVault.convertRewardsToPTokens(vitalik, eigenPointsId, 2); + } + function test_ReceiveETH() public payable { // Amount of ETH to send uint256 amountToSend = 1 ether;