Skip to content

Commit

Permalink
lottFund bid tests done
Browse files Browse the repository at this point in the history
  • Loading branch information
drikssy committed Oct 29, 2024
1 parent afc731a commit 21d07c9
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 286 deletions.
11 changes: 7 additions & 4 deletions contracts/LottFund.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ contract LottFund is VRFConsumerBaseV2Plus, ILottFund, AddressProviderResolver,
error LottFund__BiddingNotFinished();
error LottFund__TokenBidAmountDepleted();
error LottFund__TokenCannotBeBidded();
error LottFund__AddressHasBiddedTooManyTimes();
error LottFund__AddressHasBiddedTooManyTimes(address caller);

constructor(
address addressProvider,
Expand Down Expand Up @@ -121,6 +121,9 @@ contract LottFund is VRFConsumerBaseV2Plus, ILottFund, AddressProviderResolver,
}

function bid(uint256 tokenId) public whenNotPaused nonReentrant {
if (bidCountPerRound[currentRound][msg.sender] >= maxBidsPerAddress) {
revert LottFund__AddressHasBiddedTooManyTimes(msg.sender);
}
ITraitForgeNft traitForgeNft = _getTraitForgeNft();
if (traitForgeNft.ownerOf(tokenId) != msg.sender) revert LottFund__CallerNotTokenOwner();
if (
Expand Down Expand Up @@ -150,7 +153,7 @@ contract LottFund is VRFConsumerBaseV2Plus, ILottFund, AddressProviderResolver,
uint256 tokenId = tokenIds[i];

if (bidCountPerRound[currentRound][sender] >= maxBidsPerAddress) {
revert LottFund__AddressHasBiddedTooManyTimes();
revert LottFund__AddressHasBiddedTooManyTimes(sender);
}
if (traitForgeNft.ownerOf(tokenId) != sender) {
revert LottFund__CallerNotTokenOwner();
Expand All @@ -163,7 +166,7 @@ contract LottFund is VRFConsumerBaseV2Plus, ILottFund, AddressProviderResolver,
) {
revert LottFund__ContractNotApproved();
}
canTokenBeBidded(tokenId);
if (!canTokenBeBidded(tokenId)) revert LottFund__TokenCannotBeBidded();
bidCountPerRound[currentRound][sender]++;
bidsAmount++;
tokenIdsBidded.push(tokenId);
Expand Down Expand Up @@ -355,7 +358,7 @@ contract LottFund is VRFConsumerBaseV2Plus, ILottFund, AddressProviderResolver,
revert LottFund__BiddingNotFinished();
}
requestRandomWords(nativePayment);
// uint256[] memory tokensToWin = new uint256[](quantityToWin); //memory to stre the tokens to be burnt
// uint256[] memory tokensToWin = new uint256[](quantityToWin); //memory to stre the tokens to be burnt
for (uint256 i = 1; i <= quantityToWin; i++) {
// A for loop incase we want to add multiple winners later
uint256 winnerIndex = _randomWords[0] % tokenIdsBidded.length; // get the index of the array of tokenIds
Expand Down
156 changes: 0 additions & 156 deletions test/integration/concrete/lottFund/BasicTests.t.sol

This file was deleted.

13 changes: 13 additions & 0 deletions test/integration/concrete/lottFund/LottFundTest.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import { Deploys } from "test/shared/Deploys.sol";

contract LottFundTest is Deploys {
address public user = makeAddr("user");

function setUp() public virtual override {
super.setUp();
deal(user, 1_000_000 ether);
}
}
94 changes: 94 additions & 0 deletions test/integration/concrete/lottFund/LottFundTest_Bid.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import { LottFundTest } from "test/integration/concrete/lottFund/LottFundTest.t.sol";
import { LottFund } from "contracts/LottFund.sol";

contract LottFundTest_Bid is LottFundTest {
function testRevert_lottFund_bid_whenPaused() public {
vm.prank(_protocolMaintainer);
_lottFund.pause();

vm.expectRevert(bytes("Pausable: paused"));
vm.prank(_randomUser);
_lottFund.bid(1);
}

function testRevert_lottFund_bid_whenCallerAddressHasBiddedTwoManyTimes() public {
uint256 maxBidsPerAddress = _lottFund.maxBidsPerAddress();
_mintTraitForgeNft(user, 1000);
uint256 tokenIdWithMaxBidPotential;
for (uint256 i = 0; i < maxBidsPerAddress; i++) {
tokenIdWithMaxBidPotential = _getTheNthMaxBidPotentialNotZeroId(0, 1000, i + 1);

vm.startPrank(user);
_traitForgeNft.approve(address(_lottFund), tokenIdWithMaxBidPotential);
_lottFund.bid(tokenIdWithMaxBidPotential);
vm.stopPrank();
}

tokenIdWithMaxBidPotential = _getTheNthMaxBidPotentialNotZeroId(0, 1000, maxBidsPerAddress + 1);

vm.startPrank(user);
_traitForgeNft.approve(address(_lottFund), tokenIdWithMaxBidPotential);
vm.expectRevert(abi.encodeWithSelector(LottFund.LottFund__AddressHasBiddedTooManyTimes.selector, user));
_lottFund.bid(tokenIdWithMaxBidPotential);
vm.stopPrank();
}

function testRevert_lottFund_bid_whenCallerNotTokenOwner() public {
address otherUser = makeAddr("otherUser");
_mintTraitForgeNft(otherUser, 1);

vm.expectRevert(LottFund.LottFund__CallerNotTokenOwner.selector);
vm.prank(user);
_lottFund.bid(1);
}

function testRevert_lottFund_bid_whenContractNotApproved() public {
_mintTraitForgeNft(user, 1);

vm.expectRevert(LottFund.LottFund__ContractNotApproved.selector);
vm.prank(user);
_lottFund.bid(1);
}

function testRevert_lottFund_bid_whenMaxBidPotentialIsZero() public {
_mintTraitForgeNft(user, 100);
uint256 tokenIdWithMaxBidPotentialZero = _getTheNthMaxBidPotentialIsZeroId(0, 100, 1);

vm.startPrank(user);
_traitForgeNft.approve(address(_lottFund), tokenIdWithMaxBidPotentialZero);
vm.expectRevert(LottFund.LottFund__TokenCannotBeBidded.selector);
_lottFund.bid(tokenIdWithMaxBidPotentialZero);
}

function testRevert_lottFund_bid_whenTokenBidCountHigherThanPotential() public {
_mintTraitForgeNft(user, 100);
uint256 tokenIdWithMaxBidPotential = _getTheNthMaxBidPotentialNotZeroId(0, 100, 1);
uint256 tokenMaxBidPotential = _lottFund.getMaxBidPotential(tokenIdWithMaxBidPotential);

vm.startPrank(user);
_traitForgeNft.approve(address(_lottFund), tokenIdWithMaxBidPotential);
for (uint256 i = 0; i < tokenMaxBidPotential; i++) {
_lottFund.bid(tokenIdWithMaxBidPotential);
}

vm.expectRevert(LottFund.LottFund__TokenCannotBeBidded.selector);
_lottFund.bid(tokenIdWithMaxBidPotential);
}

function test_lottFund_bid() public {
_mintTraitForgeNft(user, 100);
uint256 tokenIdWithMaxBidPotential = _getTheNthMaxBidPotentialNotZeroId(0, 100, 1);

vm.startPrank(user);
_traitForgeNft.approve(address(_lottFund), tokenIdWithMaxBidPotential);
_lottFund.bid(tokenIdWithMaxBidPotential);

assertEq(_lottFund.bidCountPerRound(_lottFund.currentRound(), user), 1);
assertEq(_lottFund.bidsAmount(), 1);
assertEq(_lottFund.tokenIdsBidded(0), tokenIdWithMaxBidPotential);
assertEq(_lottFund.tokenBidCount(tokenIdWithMaxBidPotential), 1);
}
}
Loading

0 comments on commit 21d07c9

Please sign in to comment.