bug
Some checks are pending
CI / Foundry project (push) Waiting to run

This commit is contained in:
hoelee 2024-08-16 08:43:04 +08:00
parent 4ece5acece
commit 1b85b327ab
5 changed files with 138 additions and 103 deletions

View File

@ -26,7 +26,7 @@ pragma solidity ^0.8.19;
///// UPDATE IMPORTS TO V2.5 /////
import {VRFConsumerBaseV2Plus} from "@chainlink/contracts/src/v0.8/vrf/dev/VRFConsumerBaseV2Plus.sol";
import {VRFV2PlusClient} from "@chainlink/contracts/src/v0.8/vrf/dev/libraries/VRFV2PlusClient.sol";
import "@chainlink/contracts/src/v0.8/vrf/dev/interfaces/IVRFCoordinatorV2Plus.sol";
//import "@chainlink/contracts/src/v0.8/vrf/dev/interfaces/IVRFCoordinatorV2Plus.sol";
import "@chainlink/contracts/src/v0.8/automation/interfaces/AutomationCompatibleInterface.sol";
//import "hardhat/console.sol";
@ -55,21 +55,19 @@ contract Raffle is VRFConsumerBaseV2Plus, AutomationCompatibleInterface {
} // uint256 0 = OPEN, 1 = CALCULATING
/* State Variables */
// Chainlink VRF Variable
IVRFCoordinatorV2Plus private immutable i_vrfCoordinator;
bytes32 private immutable i_gasLane;
uint256 private immutable i_subscriptionId;
bytes32 private immutable i_gasLane;
uint32 private immutable i_callbackGasLimit;
uint16 private constant REQUEST_CONFIRMATIONS = 3;
uint32 private constant NUM_WORDS = 1;
// Lottery Variables
address payable[] private s_players;
RaffleState private s_raffleState;
address private s_recentWinner;
uint256 private s_lastTimeStamp;
uint256 private immutable i_interval;
uint256 private immutable i_entranceFee;
uint256 private s_lastTimeStamp;
address private s_recentWinner;
address payable[] private s_players;
RaffleState private s_raffleState;
/* Event */
event RaffleEnter(address indexed player);
@ -85,7 +83,6 @@ contract Raffle is VRFConsumerBaseV2Plus, AutomationCompatibleInterface {
uint256 entranceFee,
uint32 callbackGasLimit
) VRFConsumerBaseV2Plus(vrfCoordinatorV2_5) {
i_vrfCoordinator = IVRFCoordinatorV2Plus(vrfCoordinatorV2_5);
i_gasLane = gasLane;
i_interval = interval;
i_subscriptionId = subscriptionId;
@ -111,65 +108,6 @@ contract Raffle is VRFConsumerBaseV2Plus, AutomationCompatibleInterface {
emit RaffleEnter(msg.sender);
}
function requestRandomWinner() external returns (uint256 requestId) {
// Use Chainlink VRF V2 & Chainlink keeper
// https://docs.chain.link/vrf/v2/subscription/examples/get-a-random-number
// 0xb91d9cab0193d3cda599df11c4889f89c4a9fef9ec403de158ea67ce1f3a9e26
/**
* Create subscription at https://vrf.chain.link/ (MetaMask #1)
* Add Fund To The Created Subsciption Contract (MetaMask #2)
* Add Consumer
* Get Subscription ID
* Open Remix To Prepare Deploy Consumer Contract https://docs.chain.link/vrf/v2-5/migration-from-v2
* Change To Correct gwei limit hash address at https://docs.chain.link/vrf/v2/subscription/supported-networks
* Adjust Setting - random words count, confirmation blocks etc.
* Insert Subscription ID - v2 is uint64 BUT **v2.5 is uint256**
* Deploy And Get hash address (MetaMask #3)
* Insert in chainlink consumer (Metamask #4)
* Ready to use, record down the consumer contract address
* *
* v2.5 uint256 Subscription ID - 54852953177758767717007928774683925681326589777506065303357242036079980899870
* Admin Contract Creator address - 0xc9445e993daea4ba3f1fe1080f0f6f8c46b4d967
* Consumer address - 0x22eec58ce2cee446051337d71d59c89cb004d1c7
* Admin Approval Contract address - 0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B
*/
/*
// Deprecated, Now V2.5 insteads of V2
i_vrfCoordinator.requestRandomWords(
i_gasLane,
i_subscriptionId,
REQUEST_CONFIRMATIONS,
i_callbackGasLimit,
NUM_WORDS
);
*/
if ((block.timestamp - s_lastTimeStamp) <= i_interval) {
revert Raffle__IntervalNotPassed();
}
s_raffleState = RaffleState.CALCULATING;
// Prepare the ExtraArgs structure
VRFV2PlusClient.ExtraArgsV1 memory extraArgsV1 = VRFV2PlusClient.ExtraArgsV1({
nativePayment: true // Set to true or false depending on your use case
});
// Encode ExtraArgs to bytes
bytes memory extraArgs = VRFV2PlusClient._argsToBytes(extraArgsV1);
// Request random words using the prepared structure
requestId = i_vrfCoordinator.requestRandomWords(
VRFV2PlusClient.RandomWordsRequest({
keyHash: i_gasLane,
subId: i_subscriptionId,
requestConfirmations: REQUEST_CONFIRMATIONS,
callbackGasLimit: i_callbackGasLimit,
numWords: NUM_WORDS,
extraArgs: extraArgs
})
);
emit RequestedRaffleWinner(requestId); // Redundant, because i_vrfCoordinator do it same
}
/**
* @dev This is the function that the Chainlink Keeper nodes call
* they look for `upkeepNeeded` to return True.
@ -225,13 +163,73 @@ contract Raffle is VRFConsumerBaseV2Plus, AutomationCompatibleInterface {
extraArgs: extraArgs
});
// The s_vrfCoordinator is from override VRFConsumerBaseV2Plus
// Request random words using the prepared structure
uint256 requestId = i_vrfCoordinator.requestRandomWords(req);
uint256 requestId = s_vrfCoordinator.requestRandomWords(req);
// Quiz... is this redundant?
emit RequestedRaffleWinner(requestId);
}
function requestRandomWinner() external returns (uint256 requestId) {
// Use Chainlink VRF V2 & Chainlink keeper
// https://docs.chain.link/vrf/v2/subscription/examples/get-a-random-number
// 0xb91d9cab0193d3cda599df11c4889f89c4a9fef9ec403de158ea67ce1f3a9e26
/**
* Create subscription at https://vrf.chain.link/ (MetaMask #1)
* Add Fund To The Created Subsciption Contract (MetaMask #2)
* Add Consumer
* Get Subscription ID
* Open Remix To Prepare Deploy Consumer Contract https://docs.chain.link/vrf/v2-5/migration-from-v2
* Change To Correct gwei limit hash address at https://docs.chain.link/vrf/v2/subscription/supported-networks
* Adjust Setting - random words count, confirmation blocks etc.
* Insert Subscription ID - v2 is uint64 BUT **v2.5 is uint256**
* Deploy And Get hash address (MetaMask #3)
* Insert in chainlink consumer (Metamask #4)
* Ready to use, record down the consumer contract address
* *
* v2.5 uint256 Subscription ID - 54852953177758767717007928774683925681326589777506065303357242036079980899870
* Admin Contract Creator address - 0xc9445e993daea4ba3f1fe1080f0f6f8c46b4d967
* Consumer address - 0x22eec58ce2cee446051337d71d59c89cb004d1c7
* Admin Approval Contract address - 0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B
*/
/*
// Deprecated, Now V2.5 insteads of V2
i_vrfCoordinator.requestRandomWords(
i_gasLane,
i_subscriptionId,
REQUEST_CONFIRMATIONS,
i_callbackGasLimit,
NUM_WORDS
);
*/
if ((block.timestamp - s_lastTimeStamp) <= i_interval) {
revert Raffle__IntervalNotPassed();
}
s_raffleState = RaffleState.CALCULATING;
// Prepare the ExtraArgs structure
VRFV2PlusClient.ExtraArgsV1 memory extraArgsV1 = VRFV2PlusClient.ExtraArgsV1({
nativePayment: true // Set to true or false depending on your use case
});
// Encode ExtraArgs to bytes
bytes memory extraArgs = VRFV2PlusClient._argsToBytes(extraArgsV1);
// Request random words using the prepared structure
requestId = s_vrfCoordinator.requestRandomWords(
VRFV2PlusClient.RandomWordsRequest({
keyHash: i_gasLane,
subId: i_subscriptionId,
requestConfirmations: REQUEST_CONFIRMATIONS,
callbackGasLimit: i_callbackGasLimit,
numWords: NUM_WORDS,
extraArgs: extraArgs
})
);
emit RequestedRaffleWinner(requestId); // Redundant, because i_vrfCoordinator do it same
}
/*
* CEI: Check, Effects, Interactions Pattern
Check : Conditional - eg: Ensure is the raffle state is open

View File

@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import {Script} from "forge-std/Script.sol";
import {Script, console2} from "forge-std/Script.sol";
import {HelperConfig} from "./HelperConfig.s.sol";
import {Raffle} from "contracts/Raffle.sol";
import {AddConsumer, CreateSubscription, FundSubscription} from "./Interactions.s.sol";
@ -12,6 +12,11 @@ contract DeployRaffle is Script {
AddConsumer addConsumer = new AddConsumer();
HelperConfig.NetworkConfig memory config = helperConfig.getConfig();
console2.log(unicode"✨ ✨DeployRaffle:run");
console2.log(" >>> subscriptionId - ", config.subscriptionId);
console2.log(" >>> link - ", config.link);
console2.log(" >>> myAccount - ", config.myAccount);
if (config.subscriptionId == 0) {
CreateSubscription createSubscription = new CreateSubscription();
(config.subscriptionId, config.vrfCoordinatorV2_5) = createSubscription

View File

@ -55,6 +55,12 @@ contract HelperConfig is CodeConstants, Script {
}
function getConfig() public returns (NetworkConfig memory) {
console2.log(unicode"✨ HelperConfig:getConfig");
console2.log(" >>> chainId: ", block.chainid);
console2.log(
" >>> localNetworkConfig.subscriptionId: ",
localNetworkConfig.subscriptionId
);
return getConfigByChainId(block.chainid);
}
@ -88,7 +94,7 @@ contract HelperConfig is CodeConstants, Script {
function getSepoliaEthConfig() public pure returns (NetworkConfig memory sepoliaNetworkConfig) {
sepoliaNetworkConfig = NetworkConfig({
// If left as 0, our scripts will create one!
subscriptionId: 54852953177758767717007928774683925681326589777506065303357242036079980899870,
subscriptionId: 0, //54852953177758767717007928774683925681326589777506065303357242036079980899870
gasLane: 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae,
automationUpdateInterval: 30, // 30 seconds
raffleEntranceFee: 0.01 ether, // 1e16
@ -105,8 +111,10 @@ contract HelperConfig is CodeConstants, Script {
return localNetworkConfig;
}
console2.log(unicode"⚠️ You have deployed a mock conract!");
console2.log("Make sure this was intentional");
console2.log(
unicode"⚠️ HelperConfig: getOrCreateAnvilEthConfig - Deployed mock contract! ⚠️"
);
console2.log(" >>> Make sure this was intentional");
vm.startBroadcast();
VRFCoordinatorV2_5Mock vrfCoordinatorV2_5Mock = new VRFCoordinatorV2_5Mock(
MOCK_BASE_FEE,
@ -128,7 +136,7 @@ contract HelperConfig is CodeConstants, Script {
link: address(link),
myAccount: FOUNDRY_DEFAULT_SENDER
});
vm.deal(localNetworkConfig.myAccount, 100 ether);
vm.deal(localNetworkConfig.myAccount, 100 ether); //0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76
return localNetworkConfig;
}
}

View File

@ -1,10 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import {Script, console} from "forge-std/Script.sol";
import {Script, console2} from "forge-std/Script.sol";
import {HelperConfig} from "./HelperConfig.s.sol";
import {Raffle} from "../contracts/Raffle.sol";
// import {DevOpsTools} from "foundry-devops/src/DevOpsTools.sol"; // Deprecated
import {DevOpsTools} from "foundry-devops/DevOpsTools.sol";
import {VRFCoordinatorV2_5Mock} from "@chainlink/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2_5Mock.sol";
import {LinkToken} from "../test/mocks/LinkToken.sol";
@ -24,16 +23,18 @@ contract CreateSubscription is Script {
address vrfCoordinatorV2_5,
address myAccount
) public returns (uint256, address) {
console.log("Creating subscription on chainId: ", block.chainid);
console2.log(unicode"✨✨ CreateSubscription:createSubscription");
console2.log(" >>> chainId: ", block.chainid);
vm.startBroadcast(myAccount);
uint256 subId = VRFCoordinatorV2_5Mock(vrfCoordinatorV2_5).createSubscription();
vm.stopBroadcast();
console.log("Your subscription Id is: ", subId);
console.log("Please update the subscriptionId in HelperConfig.s.sol");
console2.log(" >>> subscription Id is - ", subId);
console2.log(" >>> Please update the subscriptionId in HelperConfig.s.sol");
return (subId, vrfCoordinatorV2_5);
}
function run() external returns (uint256, address) {
console2.log(unicode"✨✨ CreateSubscription:run");
return createSubscriptionUsingConfig();
}
}
@ -45,6 +46,7 @@ contract CreateSubscription is Script {
*/
contract FundSubscription is CodeConstants, Script {
uint96 public constant FUND_AMOUNT = 3 ether;
uint96 public constant FUND_AMOUNT_CUSTOM = 2198 ether;
function fundSubscriptionUsingConfig() public {
HelperConfig helperConfig = new HelperConfig();
@ -58,7 +60,11 @@ contract FundSubscription is CodeConstants, Script {
(uint256 updatedSubId, address updatedVRFv2) = createSub.run();
subId = updatedSubId;
vrfCoordinatorV2_5 = updatedVRFv2;
console.log("New SubId Created! ", subId, "VRF Address: ", vrfCoordinatorV2_5);
console2.log(
unicode"✨FundSubscription:fundSubscriptionUsingConfig - New SubId Created! "
);
console2.log(" >>> subscription id: ", subId);
console2.log(" >>> VRF Address: ", vrfCoordinatorV2_5);
}
fundSubscription(vrfCoordinatorV2_5, subId, link, myAccount);
@ -70,20 +76,21 @@ contract FundSubscription is CodeConstants, Script {
address linkToken,
address myAccount
) public {
console.log("Funding subscription:", subId);
console.log("Using vrfCoordinator:", vrfCoordinatorV2_5);
console.log("On ChainID:", block.chainid);
console2.log(unicode"✨ FundSubscription:fundSubscription");
if (block.chainid == LOCAL_CHAIN_ID) {
vm.startBroadcast(myAccount);
VRFCoordinatorV2_5Mock(vrfCoordinatorV2_5).fundSubscription(subId, FUND_AMOUNT);
vm.stopBroadcast();
} else {
console.log("Sender balance:", LinkToken(linkToken).balanceOf(msg.sender));
console.log("Sender address:", msg.sender);
console.log("LINK: ", linkToken);
console.log("myAccount: ", myAccount);
console.log("Subscription myAccount:", LinkToken(linkToken).balanceOf(address(this)));
console.log("Subscription address", address(this));
console2.log(" >>> Sender balance - ", LinkToken(linkToken).balanceOf(msg.sender));
console2.log(" >>> Sender address:", msg.sender);
console2.log(" >>> LINK: ", linkToken);
console2.log(" >>> myAccount: ", myAccount);
console2.log(
" >>> Subscription myAccount:",
LinkToken(linkToken).balanceOf(address(this))
);
console2.log(" >>> Subscription address", address(this));
vm.startBroadcast(myAccount);
LinkToken(linkToken).transferAndCall(
vrfCoordinatorV2_5,
@ -95,6 +102,7 @@ contract FundSubscription is CodeConstants, Script {
}
function run() external {
console2.log(unicode"✨✨ FundSubscription:run");
fundSubscriptionUsingConfig();
}
}
@ -115,15 +123,16 @@ contract AddConsumer is Script {
uint256 subId,
address myAccount
) public {
console.log("Adding consumer contract: ", contractToAddToVrf);
console.log("Using vrfCoordinator: ", vrfCoordinator);
console.log("On ChainID: ", block.chainid);
console2.log(unicode"✨ Interactions:AddConsumer contract: ", contractToAddToVrf);
console2.log(" >>> Using vrfCoordinator: ", vrfCoordinator);
console2.log(" >>> On ChainID: ", block.chainid);
vm.startBroadcast(myAccount);
VRFCoordinatorV2_5Mock(vrfCoordinator).addConsumer(subId, contractToAddToVrf);
vm.stopBroadcast();
}
function run() external {
console2.log(unicode"✨✨ AddConsumer:run");
address mostRecentlyDeployed = DevOpsTools.get_most_recent_deployment(
"Raffle",
block.chainid

View File

@ -30,8 +30,7 @@ contract RaffleTest is Test, CodeConstants {
LinkToken link;
address public PLAYER = makeAddr("player");
uint256 public constant STARTING_USER_BALANCE = 100 ether;
uint256 public constant STARTING_PLAYER_BALANCE = 10 ether;
uint256 public constant STARTING_USER_BALANCE = 10 ether;
uint256 public constant LINK_BALANCE = 100 ether;
function setUp() external {
@ -48,6 +47,17 @@ contract RaffleTest is Test, CodeConstants {
entranceFee = config.raffleEntranceFee;
callbackGasLimit = config.callbackGasLimit;
link = LinkToken(config.link);
vm.startPrank(msg.sender);
if (block.chainid == LOCAL_CHAIN_ID) {
link.mint(msg.sender, LINK_BALANCE);
VRFCoordinatorV2_5Mock(vrfCoordinatorV2_5).fundSubscription(
subscriptionId,
LINK_BALANCE
);
}
link.approve(vrfCoordinatorV2_5, LINK_BALANCE);
vm.stopPrank();
}
modifier raffleEntered() {
@ -69,6 +79,13 @@ contract RaffleTest is Test, CodeConstants {
}
}
modifier skipFork() {
if (block.chainid != 31337) {
return;
}
_;
}
function testRaffleInitializesInOpenState() public view {
assert(raffle.getRaffleState() == Raffle.RaffleState.OPEN);
}
@ -257,7 +274,7 @@ contract RaffleTest is Test, CodeConstants {
function testFulfillRandomWordsCanOnlyBeCalledAfterPerformUpkeep(
uint256 randomRequestId
) public raffleEntered onlyOnDeployedContracts {
) public raffleEntered skipFork {
// Arrange
// Act / Assert
vm.expectRevert(VRFCoordinatorV2_5Mock.InvalidRequest.selector);
@ -271,11 +288,7 @@ contract RaffleTest is Test, CodeConstants {
//..
}
function testFulfillRandomWordsPicksAWinnerResetsAndSendsMoney()
public
raffleEntered
onlyOnDeployedContracts
{
function testFulfillRandomWordsPicksAWinnerResetsAndSendsMoney() public raffleEntered skipFork {
address expectedWinner = address(1);
// Arrange
@ -307,7 +320,8 @@ contract RaffleTest is Test, CodeConstants {
helperConfig.getConfig().link,
helperConfig.getConfig().myAccount
);
*/
*/
VRFCoordinatorV2_5Mock(vrfCoordinatorV2_5).fulfillRandomWords(
uint256(requestId),
address(raffle)
@ -326,7 +340,7 @@ contract RaffleTest is Test, CodeConstants {
assert(endingTimeStamp > startingTimeStamp);
}
function testHoelee() public raffleEntered onlyOnDeployedContracts {
function testHoelee() public raffleEntered {
address expectedWinner = address(1);
// Arrange
@ -336,6 +350,7 @@ contract RaffleTest is Test, CodeConstants {
for (uint256 i = startingIndex; i < startingIndex + additionalEntrances; i++) {
address player = address(uint160(i));
hoax(player, 1 ether); // deal 1 eth to the player
//vm.deal(player, 1 ether); // same
raffle.enterRaffle{value: entranceFee}();
}
@ -352,7 +367,7 @@ contract RaffleTest is Test, CodeConstants {
VRFCoordinatorV2_5Mock(vrfCoordinatorV2_5).fulfillRandomWords(
uint256(requestId),
address(raffle)
);
); // InsuficientBalance()
// Assert
address recentWinner = raffle.getRecentWinner();