Users may lose valuable unopened rewards if they claim all rewards without opening their boxes first, leading to a total loss of funds.
User 2 should receive 1.5 ether (His total balance should be 2.2 ether), but instead gets 1 ether, and his rewardsOwned mapping gets deleted.
Add this to your test suite.
function testPlayerCanClaimRewards() public {
vm.startPrank(user1);
mysteryBox.buyBox{value: 0.1 ether}();
uint r = mysteryBox.openBox();
console2.log("REWARD:", r);
mysteryBox.claimSingleReward(0);
vm.startPrank(user1);
mysteryBox.buyBox{value: 0.1 ether}();
uint a = mysteryBox.openBox();
console2.log("REWARD:", a);
mysteryBox.claimSingleReward(1);
MysteryBox.Reward[] memory aa = mysteryBox.getRewards(user1);
vm.startPrank(user2);
mysteryBox.buyBox{value: 0.1 ether}();
uint b = mysteryBox.openBox();
console2.log("REWARD:", b);
mysteryBox.buyBox{value: 0.1 ether}();
uint c = mysteryBox.openBox();
console2.log("REWARD:", c);
mysteryBox.buyBox{value: 0.1 ether}();
mysteryBox.claimAllRewards();
MysteryBox.Reward[] memory rew = mysteryBox.getRewards(user2);
vm.startPrank(user3);
mysteryBox.buyBox{value: 0.1 ether}();
uint w = mysteryBox.openBox();
console2.log("REWARD:", w);
mysteryBox.claimSingleReward(0);
vm.startPrank(user3);
mysteryBox.buyBox{value: 0.1 ether}();
uint t = mysteryBox.openBox();
console2.log("REWARD:", t);
mysteryBox.claimSingleReward(1);
vm.startPrank(user4);
mysteryBox.buyBox{value: 0.1 ether}();
uint y = mysteryBox.openBox();
console2.log("REWARD:", y);
mysteryBox.claimSingleReward(0);
vm.startPrank(user4);
mysteryBox.buyBox{value: 0.1 ether}();
uint f = mysteryBox.openBox();
console2.log("REWARD:", f);
mysteryBox.claimSingleReward(1);
assertEq(user3.balance, 0.8 ether);
>>> assertEq(user2.balance, 1.7 ether);
assertEq(user1.balance, 0.8 ether);
assertEq(rew.length, 0);
assertEq(aa.length, 2);
} ```
## Recommendations
Invoke `openBox()` Automatically: Modify the `buyBox()` function to automatically call the `openBox()` function after purchasing a box. This ensures that users immediately open their rewards upon buying, reducing the likelihood of forgetting to do so later.