Files
hardhat-smartcontract-lottery/script/HelperConfig.s.sol
hoelee 1b85b327ab
Some checks are pending
CI / Foundry project (push) Waiting to run
bug
2024-08-16 08:43:04 +08:00

143 lines
6.0 KiB
Solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import {Script, console2} from "forge-std/Script.sol";
import {VRFCoordinatorV2_5Mock} from "@chainlink/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2_5Mock.sol";
import {LinkToken} from "../test/mocks/LinkToken.sol";
abstract contract CodeConstants {
uint96 public MOCK_BASE_FEE = 0.25 ether;
uint96 public MOCK_GAS_PRICE_LINK = 1e9;
// LINK / ETH price
int256 public MOCK_WEI_PER_UINT_LINK = 4e15;
address public FOUNDRY_DEFAULT_SENDER = 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38;
uint256 public constant ETH_SEPOLIA_CHAIN_ID = 11155111;
uint256 public constant ETH_MAINNET_CHAIN_ID = 1;
uint256 public constant LOCAL_CHAIN_ID = 31337;
}
contract HelperConfig is CodeConstants, Script {
/*//////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/
error HelperConfig__InvalidChainId();
/*//////////////////////////////////////////////////////////////
TYPES
//////////////////////////////////////////////////////////////*/
struct NetworkConfig {
uint256 subscriptionId;
bytes32 gasLane;
uint256 automationUpdateInterval;
uint256 raffleEntranceFee;
uint32 callbackGasLimit;
address vrfCoordinatorV2_5;
address link;
address myAccount;
}
/*//////////////////////////////////////////////////////////////
STATE VARIABLES
//////////////////////////////////////////////////////////////*/
// Local network state variables
NetworkConfig public localNetworkConfig;
mapping(uint256 chainId => NetworkConfig) public networkConfigs;
/*//////////////////////////////////////////////////////////////
FUNCTIONS
//////////////////////////////////////////////////////////////*/
constructor() {
networkConfigs[ETH_SEPOLIA_CHAIN_ID] = getSepoliaEthConfig();
networkConfigs[ETH_MAINNET_CHAIN_ID] = getMainnetEthConfig();
// Note: We skip doing the local config
}
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);
}
function setConfig(uint256 chainId, NetworkConfig memory networkConfig) public {
networkConfigs[chainId] = networkConfig;
}
function getConfigByChainId(uint256 chainId) public returns (NetworkConfig memory) {
if (networkConfigs[chainId].vrfCoordinatorV2_5 != address(0)) {
return networkConfigs[chainId];
} else if (chainId == LOCAL_CHAIN_ID) {
return getOrCreateAnvilEthConfig();
} else {
revert HelperConfig__InvalidChainId();
}
}
function getMainnetEthConfig() public pure returns (NetworkConfig memory mainnetNetworkConfig) {
mainnetNetworkConfig = NetworkConfig({
subscriptionId: 0, // If left as 0, our scripts will create one!
gasLane: 0x9fe0eebf5e446e3c998ec9bb19951541aee00bb90ea201ae456421a2ded86805,
automationUpdateInterval: 30, // 30 seconds
raffleEntranceFee: 0.01 ether,
callbackGasLimit: 500000, // 500,000 gas
vrfCoordinatorV2_5: 0x271682DEB8C4E0901D1a1550aD2e64D568E69909,
link: 0x514910771AF9Ca656af840dff83E8264EcF986CA,
myAccount: 0x643315C9Be056cDEA171F4e7b2222a4ddaB9F88D
});
}
function getSepoliaEthConfig() public pure returns (NetworkConfig memory sepoliaNetworkConfig) {
sepoliaNetworkConfig = NetworkConfig({
// If left as 0, our scripts will create one!
subscriptionId: 0, //54852953177758767717007928774683925681326589777506065303357242036079980899870
gasLane: 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae,
automationUpdateInterval: 30, // 30 seconds
raffleEntranceFee: 0.01 ether, // 1e16
callbackGasLimit: 500000, // 500,000 gas
vrfCoordinatorV2_5: 0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B,
link: 0x779877A7B0D9E8603169DdbD7836e478b4624789, // This is fixed LINK token address
myAccount: 0xc9445E993dAeA4bA3F1FE1080F0F6f8c46b4d967 // my address with supplied private key
});
}
function getOrCreateAnvilEthConfig() public returns (NetworkConfig memory) {
// Check to see if we set an active network config
if (localNetworkConfig.vrfCoordinatorV2_5 != address(0)) {
return localNetworkConfig;
}
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,
MOCK_GAS_PRICE_LINK,
MOCK_WEI_PER_UINT_LINK
);
LinkToken link = new LinkToken();
uint256 subscriptionId = vrfCoordinatorV2_5Mock.createSubscription();
vm.stopBroadcast();
localNetworkConfig = NetworkConfig({
subscriptionId: subscriptionId,
gasLane: 0x474e34a077df58807dbe9c96d3c009b23b3c6d0cce433e59bbf5b34f823bc56c, // doesn't really matter
automationUpdateInterval: 30, // 30 seconds
raffleEntranceFee: 0.01 ether,
callbackGasLimit: 500000, // 500,000 gas
vrfCoordinatorV2_5: address(vrfCoordinatorV2_5Mock),
link: address(link),
myAccount: FOUNDRY_DEFAULT_SENDER
});
vm.deal(localNetworkConfig.myAccount, 100 ether); //0xDB8cFf278adCCF9E9b5da745B44E754fC4EE3C76
return localNetworkConfig;
}
}