Skip to content

Commit

Permalink
complete test
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmojtaba committed Oct 5, 2024
1 parent 5865704 commit f4635ba
Show file tree
Hide file tree
Showing 11 changed files with 1,164 additions and 807 deletions.
106 changes: 106 additions & 0 deletions broadcast/DeployLottery.s.sol/11155111/run-1728123960.json

Large diffs are not rendered by default.

106 changes: 106 additions & 0 deletions broadcast/DeployLottery.s.sol/11155111/run-latest.json

Large diffs are not rendered by default.

1,541 changes: 768 additions & 773 deletions coverage.txt

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ fs_permissions = [
{ access = "read", path = "./broadcast" },
{ access = "read", path = "./reports" },
]

[fuzz]
runs = 1000

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
67 changes: 67 additions & 0 deletions json.json

Large diffs are not rendered by default.

16 changes: 14 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
-include .env

.PHONY: all test deploy

build:; forge build

test:; forge test

install:; forge install cyfrin/[email protected] --no-commit && forge install smartcontractkit/[email protected] --no-commit && forge install foundry-rs/[email protected] --no-commit && forge install transmissions11/solmate@v6 --no-commit

format:; forge fmt

sepoliabuild:; forge script script/Interactions.s.sol:FundSubscription --rpc-url $SEPOLIA_RPC_URL --account myaccount --broadcast
sepolia-fudsubscription-build:
@forge script script/Interactions.s.sol:FudSubscription --rpc-url $SEPOLIA_RPC_URL --account myaccount --broadcast

sepolia-deploy:
@forge script script/DeployLottery.s.sol:LotteryDeploy --rpc-url $(SEPOLIA_RPC_URL) --account myaccount --broadcast --verify --etherscan-api-key $(ETHERSCAN_API_KEY) -vvvv

test:; forge test

test:; forge test
coverage:; forge coverage --report debug>coverage.txt
8 changes: 4 additions & 4 deletions script/DeployLottery.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ contract LotteryDeploy is Script {
if (config._subscriptionId == 0) {
CreateSubscription createSubscription = new CreateSubscription();
(config._subscriptionId, config._vrfCoordinator) =
createSubscription.createSubscription(config._vrfCoordinator);
createSubscription.createSubscription(config._vrfCoordinator, config.account);
FundSubscription fundSubscription = new FundSubscription();
fundSubscription.fundSubscription(config._vrfCoordinator, config._subscriptionId, config.link);
fundSubscription.fundSubscription(config._vrfCoordinator, config._subscriptionId, config.link, config.account);
}

vm.startBroadcast();
vm.startBroadcast(config.account);

Lottery lottery = new Lottery(
config._enteranceFee,
Expand All @@ -36,7 +36,7 @@ contract LotteryDeploy is Script {

vm.stopBroadcast();
AddConsumer addConsumer = new AddConsumer();
addConsumer.addConsumer(address(lottery), config._vrfCoordinator, config._subscriptionId);
addConsumer.addConsumer(address(lottery), config._vrfCoordinator, config._subscriptionId, config.account);

return (lottery, helperConfig);
}
Expand Down
7 changes: 5 additions & 2 deletions script/HelperConfig.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ contract HelperConfig is CodeConstants, Script {
uint32 _callbackGasLimit;
address _vrfCoordinator;
address link;
address account;
}

networkConfig public localNetworkConfig;
Expand Down Expand Up @@ -69,7 +70,8 @@ contract HelperConfig is CodeConstants, Script {
0, // the subscriptionId
100000, // the gasLimit
address(vrfCoordinatorV2_5Mock), // vrf coordinator address
address(linkToken)
address(linkToken),
0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38 // Default address for tx.origin and msg.sender, 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38. Base.sol
);
return localNetworkConfig;
}
Expand All @@ -82,7 +84,8 @@ contract HelperConfig is CodeConstants, Script {
18158373438440650950665589907136733397195481284246089286377304796610796020611, // the subscriptionId
100000, // the gasLimit
0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B, // vrf coordinator address
0x779877A7B0D9E8603169DdbD7836e478b4624789
0x779877A7B0D9E8603169DdbD7836e478b4624789,
0x918b0DB5d32b963977a18bD14f1004be80C2D71F
);
}
}
44 changes: 28 additions & 16 deletions script/Interactions.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ contract CreateSubscription is Script {
function createSubscriptionUsingconfig() public returns (uint256, address) {
HelperConfig helperConfig = new HelperConfig();
address vrfCoordinator = helperConfig.getConfig()._vrfCoordinator;
(uint256 subId,) = createSubscription(vrfCoordinator);
address account = helperConfig.getConfig().account;
(uint256 subId,) = createSubscription(vrfCoordinator ,account);

return (subId, vrfCoordinator);
}

function createSubscription(address vrfCoordinator) public returns (uint256, address) {
function createSubscription(address vrfCoordinator , address account) public returns (uint256, address) {
console.log("creating subId");

vm.startBroadcast();
vm.startBroadcast(account);
uint256 subId = VRFCoordinatorV2_5Mock(vrfCoordinator).createSubscription();
vm.stopBroadcast();

Expand All @@ -42,21 +43,30 @@ contract FundSubscription is Script, CodeConstants {
address vrfCoordinator = helperConfig.getConfig()._vrfCoordinator;
uint256 subscriptionId = helperConfig.getConfig()._subscriptionId;
address linkToken = helperConfig.getConfig().link;
address account = helperConfig.getConfig().account;

fundSubscription(vrfCoordinator, subscriptionId, linkToken);
fundSubscription(vrfCoordinator, subscriptionId, linkToken, account);
}

function fundSubscription(address vrfCoordinator, uint256 subscriptionId, address linkToken) public {
function fundSubscription
(
address vrfCoordinator,
uint256 subscriptionId,
address linkToken,
address account
) public {
console.log("funding subscriptionId ", subscriptionId);
console.log("with vrfCoordinator", vrfCoordinator);
console.log("on chain :", block.chainid);

if (block.chainid == ANVIL_CHAINID) {
vm.startBroadcast();
VRFCoordinatorV2_5Mock(vrfCoordinator).fundSubscription(subscriptionId, AMOUNT);
VRFCoordinatorV2_5Mock(vrfCoordinator).fundSubscription(
subscriptionId, AMOUNT * 1000 /* for Mock testing */
);
vm.stopBroadcast();
} else {
vm.startBroadcast();
vm.startBroadcast(account);
LinkToken(linkToken).transferAndCall(vrfCoordinator, AMOUNT, abi.encode(subscriptionId));
vm.stopBroadcast();
}
Expand All @@ -72,24 +82,26 @@ contract AddConsumer is Script {
HelperConfig helperConfig = new HelperConfig();
uint256 subscriptionId = helperConfig.getConfig()._subscriptionId;
address vrfCoordinator = helperConfig.getConfig()._vrfCoordinator;
addConsumer(contractDeployed, vrfCoordinator, subscriptionId);
address account = helperConfig.getConfig().account;
addConsumer(contractDeployed, vrfCoordinator, subscriptionId, account);
}

function addConsumer(address contractToVrf, address vrfCoordinator, uint256 subscriptionId) public {
function addConsumer
(
address contractToVrf,
address vrfCoordinator,
uint256 subscriptionId,
address account
) public {
console.log("adding consumer ", contractToVrf);
console.log("to vrfCoordinator", vrfCoordinator);
console.log("with subscriptionId", subscriptionId);
console.log("on chain :", block.chainid);

// if (block.chainid == ANVIL_CHAINID) {
vm.startBroadcast();
vm.startBroadcast(account);
VRFCoordinatorV2_5Mock(vrfCoordinator).addConsumer(subscriptionId, contractToVrf);
vm.stopBroadcast();
// } else {
// vm.startBroadcast();
vm.stopBroadcast();

// vm.stopBroadcast();
// }
}

function run() external {
Expand Down
4 changes: 4 additions & 0 deletions src/Lottery.sol
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,8 @@ contract Lottery is VRFConsumerBaseV2Plus {
function getPlayer(uint256 index) public view returns (address) {
return s_players[index];
}

function getLastTimestamp() public view returns (uint256) {
return s_lastTimeStamp;
}
}
68 changes: 58 additions & 10 deletions test/uint/TestLottery.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
pragma solidity ^0.8.19;

import {Test, console} from "forge-std/Test.sol";
import {VRFCoordinatorV2_5Mock} from "@chainlink/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2_5Mock.sol";
import {Lottery} from "src/Lottery.sol";
import {HelperConfig} from "script/HelperConfig.s.sol";
import {HelperConfig, CodeConstants} from "script/HelperConfig.s.sol";
import {LotteryDeploy} from "script/DeployLottery.s.sol";
import {Vm} from "forge-std/Vm.sol";

contract LotteryTest is Test {
contract LotteryTest is CodeConstants, Test {
Lottery public lottery;
HelperConfig public helperConfig;

Expand Down Expand Up @@ -47,19 +48,25 @@ contract LotteryTest is Test {
assert(lottery.getLotteryStatus() == Lottery.LotteryStatus.Open);
}


/*
Modifires
*/

modifier lotteryEntered(){
modifier lotteryEntered() {
vm.prank(player);
lottery.enter{value: _enteranceFee}();
vm.warp(block.timestamp + _interval + 10); // cheating => changing the time stamp to the time we needed
vm.roll(block.number + 1);
_;
}

modifier notLocal(){
if(block.chainid != ANVIL_CHAINID){
return;
}
_;
}

/*///////////////////////////////////////////////////////////
test enter
////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -96,7 +103,7 @@ contract LotteryTest is Test {
lottery.enter{value: _enteranceFee}();
}

function testEnterLotteryByPlayerWhileLotteryIsNotOpen() public lotteryEntered{
function testEnterLotteryByPlayerWhileLotteryIsNotOpen() public lotteryEntered {
// arrange
lottery.performUpkeep("");

Expand Down Expand Up @@ -144,8 +151,7 @@ contract LotteryTest is Test {
assert(!upkeepNeeded);
}

function testCheckupkeepPassWhenEverythingIsOk() public lotteryEntered{

function testCheckupkeepPassWhenEverythingIsOk() public lotteryEntered {
// act
(bool upkeepNeeded,) = lottery.checkUpkeep("");

Expand Down Expand Up @@ -174,12 +180,10 @@ contract LotteryTest is Test {
}

function testPerformeUpkeepRunsWell() public lotteryEntered {

lottery.performUpkeep("");
}

function testPerformeUpkeepEventRequestId() public lotteryEntered {

function testPerformeUpkeepEventRequestId() public lotteryEntered {
// act
vm.recordLogs();
lottery.performUpkeep("");
Expand All @@ -191,4 +195,48 @@ contract LotteryTest is Test {
assert(uint256(requestid) > 0);
assert(uint256(lStatus) == 1);
}

/*///////////////////////////////////////////////////////////
fulfillRandomWords
////////////////////////////////////////////////////////////*/

function testFulfillRandomWordsOnlyCallAfterPerformUpkeep(uint256 reqId) public lotteryEntered notLocal{
vm.expectRevert(VRFCoordinatorV2_5Mock.InvalidRequest.selector);
VRFCoordinatorV2_5Mock(_vrfCoordinator).fulfillRandomWords(reqId, address(lottery));
}

function testFulfillRandomWords() public lotteryEntered notLocal{
// arrange
uint256 extraPlayers = 3;
uint256 startingIndex = 1;
address expectedWinner = address(1);
for (uint256 i = startingIndex; i < extraPlayers + startingIndex; i++) {
address newPlayer = address(uint160(i));
hoax(newPlayer, 1 ether);
lottery.enter{value: _enteranceFee}();
}

uint256 startingTimestamp = lottery.getLastTimestamp();
uint256 winnerStatingBallance = expectedWinner.balance;

// act
vm.recordLogs();
lottery.performUpkeep("");
Vm.Log[] memory logs = vm.getRecordedLogs();
bytes32 requId = logs[1].topics[1];

VRFCoordinatorV2_5Mock(_vrfCoordinator).fulfillRandomWords(uint256(requId), address(lottery));

// assert
address winner = lottery.getWinner();
uint256 winnerBalance = winner.balance;
Lottery.LotteryStatus lStatus = lottery.getLotteryStatus();
uint256 endingTimestamp = lottery.getLastTimestamp();
uint256 prize = _enteranceFee * (extraPlayers + 1);

assert(winner == expectedWinner);
assert(uint256(lStatus) == 0);
assert(winnerBalance == prize + winnerStatingBallance);
assert(endingTimestamp > startingTimestamp);
}
}

0 comments on commit f4635ba

Please sign in to comment.