|
| 1 | +// ...existing code... |
| 2 | +// SPDX-License-Identifier: UNLICENSED |
| 3 | +pragma solidity ^0.8.13; |
| 4 | + |
| 5 | +import {Test, console} from "forge-std/Test.sol"; |
| 6 | +import {ChainwebConfig, Chainweb, ChainwebConfigReader} from "../src/Chainweb.sol"; |
| 7 | + |
| 8 | +/// Environment variables: |
| 9 | +/// -- CHAINWEB: "anvil" or "sandbox" (default: "anvil") |
| 10 | +/// -- INIT_IN_CONSTRUCTOR: Whether setupChainsForTest() is called in the constructor or in setUp() (default: "false") |
| 11 | +/// -- DEPLOYER_KEY: Private key to use for broadcasting transactions (default is to use msg.sender) |
| 12 | +contract ChainwebInitTest is Test { |
| 13 | + Chainweb chainweb; |
| 14 | + ChainwebConfigReader private configReader; |
| 15 | + |
| 16 | + uint256 constant ALICE_KEY = 0xb332ddc4e0801582e154d10cad8b672665656cbf0097f2b47483c0cfe3261299; |
| 17 | + address constant ALICE = address(0xFB8Fb7f9bdc8951040a6D195764905138F7462Ed); |
| 18 | + address constant BOB = address(0x28f2d8ef4e0fe6B2E945cF5C33a0118a30a62354); |
| 19 | + |
| 20 | + bool _initializeInConstructor; |
| 21 | + string _environment; |
| 22 | + uint256 _deployerKey; |
| 23 | + address _deployer; |
| 24 | + |
| 25 | + constructor () { |
| 26 | + _initializeInConstructor = vm.envOr("INIT_IN_CONSTRUCTOR", false); |
| 27 | + vm.label(ALICE, "Alice"); |
| 28 | + vm.label(BOB, "Bob"); |
| 29 | + |
| 30 | + _environment = vm.envExists("CHAINWEB") ? vm.envString("CHAINWEB") : "anvil"; |
| 31 | + configReader = new ChainwebConfigReader(); |
| 32 | + ChainwebConfig memory config = configReader.readChainwebConfig(_environment); |
| 33 | + chainweb = new Chainweb( |
| 34 | + uint24(config.numberOfChains), block.chainid, uint24(config.chainwebChainIdOffset), config.externalHostUrl |
| 35 | + ); |
| 36 | + |
| 37 | + // initializing the deployer here causes the --sender flag to be |
| 38 | + // ignored. |
| 39 | + // |
| 40 | + // _deployerKey = vm.envOr("DEPLOYER_KEY", uint256(0x0)); |
| 41 | + // if (_deployerKey != 0) { |
| 42 | + // _deployer = vm.rememberKey(_deployerKey); |
| 43 | + // vm.label(_deployer, "EnvDeployer"); |
| 44 | + // } else { |
| 45 | + // _deployer = msg.sender; |
| 46 | + // } |
| 47 | + |
| 48 | + if (_initializeInConstructor) { |
| 49 | + chainweb.setupChainsForTest(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + function setUp() public { |
| 54 | + if (!_initializeInConstructor) { |
| 55 | + chainweb.setupChainsForTest(); |
| 56 | + } |
| 57 | + |
| 58 | + _deployerKey = vm.envOr("DEPLOYER_KEY", uint256(0x0)); |
| 59 | + if (_deployerKey != 0) { |
| 60 | + _deployer = vm.rememberKey(_deployerKey); |
| 61 | + vm.label(_deployer, "EnvDeployer"); |
| 62 | + } else { |
| 63 | + _deployer = msg.sender; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + function test_SwitchChainNonce() public { |
| 68 | + console.log("=========================================================="); |
| 69 | + console.log("network:", _environment); |
| 70 | + console.log("Initialized in constructor:", _initializeInConstructor); |
| 71 | + console.log("deployer:", vm.getLabel(_deployer)); |
| 72 | + console.log("deployer is msg.sender:", msg.sender == _deployer); |
| 73 | + uint256[] memory chainIds = chainweb.getChainIds(); |
| 74 | + for (uint256 i = 0; i < chainIds.length; i++) { |
| 75 | + chainweb.switchChain(chainIds[i]); |
| 76 | + deal(_deployer, 100 gwei); |
| 77 | + console.log("deployer nonce: %d", vm.getNonce(_deployer)); |
| 78 | + vm.startBroadcast(_deployer); |
| 79 | + payable(BOB).transfer(1 gwei); |
| 80 | + vm.stopBroadcast(); |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | +} |
0 commit comments