Skip to content

Commit

Permalink
feat: add amount too small check to convertRewardsToPTokens fnc
Browse files Browse the repository at this point in the history
  • Loading branch information
jparklev committed May 7, 2024
1 parent bb3d3e7 commit 175a52c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
11 changes: 10 additions & 1 deletion contracts/PointTokenVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ contract PointTokenVault is UUPSUpgradeable, AccessControlUpgradeable, Multicall
error PTokenAlreadyDeployed();
error DepositExceedsCap();
error PTokenNotDeployed();
error AmountTooSmall();

constructor() {
_disableInitializers();
Expand Down Expand Up @@ -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);
}
Expand Down
40 changes: 39 additions & 1 deletion contracts/test/PointTokenVault.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 175a52c

Please sign in to comment.