You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
3.0 KiB
Solidity
94 lines
3.0 KiB
Solidity
// SPDX-License-Identifier: UNLICENSED
|
|
pragma solidity ^0.8.13;
|
|
|
|
import "forge-std/Test.sol";
|
|
import "../src/Lottery.sol";
|
|
|
|
contract LotteryTest is Test {
|
|
Lottery public lotto;
|
|
uint256 mp;
|
|
|
|
function setUp() public {
|
|
lotto = new Lottery();
|
|
mp = lotto.price();
|
|
}
|
|
|
|
// 2023-01-16 winning tokens are 1,2,3,4,5,6,7,8,9,10
|
|
|
|
function testOnlyOwner() public {
|
|
startHoax(lotto.owner());
|
|
lotto.setWinningPhrase("24fad66ea96693aaa369548305e1df9e769b32840d454493dbf14dae53ac2d39");
|
|
vm.stopPrank();
|
|
hoax(address(1));
|
|
lotto.mint{value: mp * 20}(20);
|
|
startHoax(lotto.owner());
|
|
uint256[] memory winners = new uint256[](10);
|
|
for (uint256 i; i < 10; i++) {
|
|
winners[i] = i;
|
|
}
|
|
lotto.setWinningTokens(winners);
|
|
assertEq(lotto.winningTokens(0), true);
|
|
assertEq(lotto.winningTokens(9), true);
|
|
assertEq(lotto.winningTokens(10), false);
|
|
uint256 preBalC = address(lotto).balance;
|
|
uint256 preBalU = address(lotto.owner()).balance;
|
|
lotto.withdraw(address(lotto).balance);
|
|
uint256 postBalC = address(lotto).balance;
|
|
uint256 postBalU = address(lotto.owner()).balance;
|
|
assertEq(postBalC, 0);
|
|
assertEq(postBalU, preBalC + preBalU);
|
|
}
|
|
|
|
function testHashRequired() public {
|
|
vm.expectRevert("phrase not set");
|
|
lotto.mint{value: mp * 10}(10);
|
|
assertEq(lotto.totalSupply(), 0);
|
|
lotto.setWinningPhrase("24fad66ea96693aaa369548305e1df9e769b32840d454493dbf14dae53ac2d39");
|
|
hoax(address(5));
|
|
lotto.mint{value: mp * 10}(10);
|
|
assertEq(lotto.totalSupply(), 10);
|
|
}
|
|
|
|
function testClaimingWorks() public {
|
|
address winner = address(10);
|
|
address loser = address(55);
|
|
lotto.setWinningPhrase("24fad66ea96693aaa369548305e1df9e769b32840d454493dbf14dae53ac2d39");
|
|
hoax(winner);
|
|
lotto.mint{value: mp * 200}(200);
|
|
hoax(loser);
|
|
lotto.mint{value: mp * 200}(200);
|
|
uint256[] memory winners = new uint256[](10);
|
|
for (uint256 i; i < 10; i++) {
|
|
winners[i] = i;
|
|
}
|
|
lotto.setWinningTokens(winners);
|
|
startHoax(loser);
|
|
vm.expectRevert("not owner of token");
|
|
lotto.claimToken(0);
|
|
vm.expectRevert("not a winning token");
|
|
lotto.claimToken(200);
|
|
vm.stopPrank();
|
|
startHoax(winner);
|
|
uint256 winnerPreBal = winner.balance;
|
|
lotto.claimToken(0);
|
|
uint256 winnerPostBal = winner.balance;
|
|
assertEq(winnerPostBal, winnerPreBal + 1 ether);
|
|
vm.expectRevert("token already claimed");
|
|
lotto.claimToken(0);
|
|
}
|
|
|
|
// require amounts
|
|
// only winners can claim
|
|
// winners can claim once
|
|
|
|
|
|
|
|
// halt minting
|
|
|
|
// Function to receive Ether. msg.data must be empty
|
|
receive() external payable {}
|
|
|
|
// Fallback function is called when msg.data is not empty
|
|
fallback() external payable {}
|
|
}
|