Cyfrin / foundry-smart-contract-lottery-cu

47 stars 42 forks source link

script failed: panic: arithmetic underflow or overflow (0x11) #59

Closed nayashi002 closed 3 months ago

nayashi002 commented 3 months ago

When deploying to both local and sepolia testnet i have been getting this error ``No files changed, compilation skipped EIP-3855 is not supported in one or more of the RPCs used. Unsupported Chain IDs: 31337. Contracts deployed with a Solidity version equal or higher than 0.8.20 might not work properly. For more information, please see https://eips.ethereum.org/EIPS/eip-3855 Traces: [39142691] → new DeployRaffle@0x5b73C5498c1E3b4dbA84de0F1833c4a029d90519 └─ ← [Return] 195055 bytes of code

[24056751] DeployRaffle::run() ├─ [8316981] → new HelperConfig@0xC7f2Cf4845C6db0e1a1e91ED41Bcd0FcC1b0E141 │ └─ ← [Return] 40639 bytes of code ├─ [6728151] HelperConfig::getConfig() │ ├─ [0] VM::startBroadcast() │ │ └─ ← [Return] │ ├─ [5093039] → new VRFCoordinatorV2_5Mock@0x5FbDB2315678afecb367f032d93F642f64180aa3 │ │ ├─ emit ConfigSet() │ │ └─ ← [Return] 25068 bytes of code │ ├─ [1418500] → new LinkToken@0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 │ │ ├─ emit Transfer(from: 0x0000000000000000000000000000000000000000, to: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266, amount: 1000000000000000000000000 [1e24]) │ │ └─ ← [Return] 6618 bytes of code │ ├─ [0] VM::stopBroadcast() │ │ └─ ← [Return] │ └─ ← [Return] NetworkConfig({ entranceFee: 10000000000000000 [1e16], interval: 30, vrfCoordinator: 0x5FbDB2315678afecb367f032d93F642f64180aa3, keyHash: 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae, subId: 0, callbackGasLimit: 500000 [5e5], link: 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512, account: 0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38 }) ├─ [8920437] → new CreateSubscription@0xdaE97900D4B184c5D2012dcdB658c008966466DD │ └─ ← [Return] 44429 bytes of code ├─ [7800] CreateSubscription::createSubscription(VRFCoordinatorV2_5Mock: [0x5FbDB2315678afecb367f032d93F642f64180aa3], DefaultSender: [0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38]) │ ├─ [0] console::log("creating subscription on chainid", 31337 [3.133e4]) [staticcall] │ │ └─ ← [Stop] │ ├─ [0] VM::startBroadcast(DefaultSender: [0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38]) │ │ └─ ← [Return] │ ├─ [2821] VRFCoordinatorV2_5Mock::createSubscription() │ │ └─ ← [Revert] panic: arithmetic underflow or overflow (0x11) │ └─ ← [Revert] panic: arithmetic underflow or overflow (0x11) └─ ← [Revert] panic: arithmetic underflow or overflow (0x11)

== Logs == creating subscription on chainid 31337 Error: script failed: panic: arithmetic underflow or overflow (0x11) make: *** [Makefile:43: deploy] Error 1```

it seems to be coming from my create subscription below are my scripts
N:B new to github

DEPLOY RAFFLE // SPDX-License-Identifier: MIT pragma solidity ^0.8.19;

import{Script,console} from "forge-std/Script.sol"; import {Raffle} from "src/Raffle.sol"; import {HelperConfig} from "script/HelperConfig.s.sol"; import {CreateSubscription,FundSubscription,AddConsumer} from "script/interactions.s.sol";

contract DeployRaffle is Script{ function run() public{ deployContract(); } function deployContract() public returns(Raffle,HelperConfig){ HelperConfig helperConfig = new HelperConfig();

    HelperConfig.NetworkConfig memory config = helperConfig.getConfig();
   if(config.subId == 0){
    // creating subscription
    CreateSubscription createSubscription = new CreateSubscription();
    (config.subId,config.vrfCoordinator) = createSubscription.createSubscription(config.vrfCoordinator,config.account);
    //funding it
    FundSubscription fundSubscription = new FundSubscription();
    fundSubscription.fundSubscription(config.vrfCoordinator, config.subId, config.link,config.account);
   }
    vm.startBroadcast(config.account);

    Raffle raffle = new Raffle(
       config.entranceFee,
  config.interval,
 config.vrfCoordinator,
 config.keyHash,
 config.subId,
config.callbackGasLimit
    );
    vm.stopBroadcast();
    AddConsumer addConsumer =new AddConsumer();
    addConsumer.addConsumer(address(raffle), config.vrfCoordinator, config.subId,config.account);
    return(raffle,helperConfig);
}

} HELPERCONFIG

// SPDX-License-Identifier: MIT pragma solidity ^0.8.19;

import {Script} 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";

error HelperConfig__InvalidChainId(); abstract contract CodeConstants { / VRF MOCK VALUE/ uint96 public constant MOCK_BASE_FEE = 0.25 ether; uint96 public constant MOCK_GAS_PRICE_LINK = 1e19; int256 public constant MOCK_WEI_PER_UNIT_LINK = 4e15; uint256 public constant ETH_SEPOLIA_CHAIN_ID = 11155111; uint256 public constant LOCAL_CHAIN_ID = 31337; }

contract HelperConfig is CodeConstants,Script{ struct NetworkConfig{ uint256 entranceFee; uint256 interval; address vrfCoordinator; bytes32 keyHash; uint256 subId; uint32 callbackGasLimit; address link; address account; } NetworkConfig public localNetworkConfig; mapping(uint256 chainid => NetworkConfig) public networkConfig; constructor(){ networkConfig[ETH_SEPOLIA_CHAIN_ID] = getSepoliaEthConfig(); } function getConfigByChainId(uint256 chainid) public returns(NetworkConfig memory){ if(networkConfig[chainid].vrfCoordinator != address(0)){ return networkConfig[chainid]; } else if(chainid == LOCAL_CHAIN_ID){ // getOrCreateAnvilChainId return getOrCreateAnvilEthConfig(); } else{ revert HelperConfig__InvalidChainId(); } } function getConfig() public returns(NetworkConfig memory){ return getConfigByChainId(block.chainid); } function getSepoliaEthConfig() public pure returns(NetworkConfig memory){ return NetworkConfig({ entranceFee : 0.01 ether, interval : 30, // 30 seconds vrfCoordinator : 0x9DdfaCa8183c41ad55329BdeeD9F6A8d53168B1B, keyHash : 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae, subId : 6956171124383976846514163274968361531529488885617738713351408796354057774303, callbackGasLimit: 500000, link:0x779877A7B0D9E8603169DdbD7836e478b4624789, account:0x78f891f23ad2F7b398D6f0191a4675C71e4E4cD6 }); } function getOrCreateAnvilEthConfig() public returns(NetworkConfig memory){ if(localNetworkConfig.vrfCoordinator != address(0)){ return localNetworkConfig; } // Deploy mocks vm.startBroadcast(); VRFCoordinatorV2_5Mock vrfCoordinatorMock = new VRFCoordinatorV2_5Mock(MOCK_BASE_FEE,MOCK_GAS_PRICE_LINK,MOCK_WEI_PER_UNIT_LINK); LinkToken linkToken = new LinkToken(); vm.stopBroadcast(); localNetworkConfig = NetworkConfig({ entranceFee : 0.01 ether, interval : 30, // 30 seconds vrfCoordinator : address(vrfCoordinatorMock), keyHash : 0x787d74caea10b2b357790d5b5247c2f63d1d91572a9846f780606e4d953677ae, subId : 0, callbackGasLimit: 500000, link: address(linkToken), account:0x1804c8AB1F12E6bbf3894d4083f33e07309d1f38 }); return localNetworkConfig; } }``

INTERACTIONS // SPDX-License-Identifier: MIT pragma solidity ^0.8.19; import {Script,console} from "forge-std/Script.sol"; import {HelperConfig,CodeConstants} from "script/HelperConfig.s.sol"; import {VRFCoordinatorV2_5Mock} from "@chainlink/contracts/src/v0.8/vrf/mocks/VRFCoordinatorV2_5Mock.sol"; import {LinkToken} from "test/mocks/LinkToken.sol"; import {DevOpsTools} from "lib/foundry-devops/src/DevOpsTools.sol";

contract CreateSubscription is Script{ function createSubscriptionUsingConfig() public returns(uint256,address){ HelperConfig helperConfig = new HelperConfig(); address vrfCoordinator = helperConfig.getConfig().vrfCoordinator; address account = helperConfig.getConfig().account; (uint256 subId,) = createSubscription(vrfCoordinator,account); return(subId,vrfCoordinator); } function createSubscription(address vrfCoordinator,address account) public returns(uint256,address){ console.log("creating subscription on chainid",block.chainid); vm.startBroadcast(account); uint256 subId = VRFCoordinatorV2_5Mock(vrfCoordinator).createSubscription(); console.log("with vrfCoordinator",vrfCoordinator); vm.stopBroadcast(); return(subId,vrfCoordinator);

}
function run() public{
   createSubscriptionUsingConfig();
}

} contract FundSubscription is Script,CodeConstants{ uint256 public constant FUND_AMOUNT = 3 ether; function fundSubscriptionUsingConfig() public{ HelperConfig helperConfig = new HelperConfig(); address vrfCoordinator = helperConfig.getConfig().vrfCoordinator; uint256 subId = helperConfig.getConfig().subId; address account = helperConfig.getConfig().account; address linkToken = helperConfig.getConfig().link; fundSubscription(vrfCoordinator, subId, linkToken,account); } function fundSubscription(address vrfCoordinator,uint256 subId,address linkToken,address account) public{ console.log("Funding Subscription with",subId); console.log("Using vrfCoordinator",vrfCoordinator); console.log("Using chainId",block.chainid); if(block.chainid == LOCAL_CHAIN_ID){ vm.startBroadcast(); VRFCoordinatorV2_5Mock(vrfCoordinator).fundSubscription(subId, FUND_AMOUNT * 1e9); vm.stopBroadcast(); } else{ vm.startBroadcast(account); LinkToken(linkToken).transferAndCall(vrfCoordinator,FUND_AMOUNT,abi.encode(subId)); vm.stopBroadcast(); } } function run() public { fundSubscriptionUsingConfig(); } }

contract AddConsumer is Script{ function addConsumerUsingConfig(address mostRecentlyDeployed) public{ HelperConfig helperConfig = new HelperConfig(); address vrfCoordinator = helperConfig.getConfig().vrfCoordinator; uint256 subId = helperConfig.getConfig().subId; address account = helperConfig.getConfig().account; addConsumer(mostRecentlyDeployed, vrfCoordinator, subId, account); } function addConsumer(address contractToAddVrf,address vrfCoordinator,uint256 subId,address account ) public{ console.log("Adding consumer contract",contractToAddVrf); console.log("Adding consumer to vrfCoordinator",vrfCoordinator); console.log("On chainId",block.chainid); vm.startBroadcast(account); VRFCoordinatorV2_5Mock(vrfCoordinator).addConsumer(subId, contractToAddVrf); vm.stopBroadcast(); }

function run() external{ address mostRecentlyDeployed = DevOpsTools.get_most_recent_deployment("Raffle", block.chainid); addConsumerUsingConfig(mostRecentlyDeployed); }

}

cromewar commented 3 months ago

Hello @nayashi002, can you please make this question in the discussion section of the main repo?

https://github.com/Cyfrin/foundry-full-course-cu/discussions