|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import {Script} from "forge-std/Script.sol"; |
| 5 | +import {console} from "forge-std/console.sol"; |
| 6 | +import {DepositFees} from "socket-protocol/script/PayFeesInArbitrumETH.s.sol"; |
| 7 | +import {Fees} from "socket-protocol/contracts/protocol/utils/common/Structs.sol"; |
| 8 | +import {FeesPlug} from "socket-protocol/contracts/protocol/payload-delivery/FeesPlug.sol"; |
| 9 | +import {ETH_ADDRESS, FAST} from "socket-protocol/contracts/protocol/utils/common/Constants.sol"; |
| 10 | +import {FeesManager} from "socket-protocol/contracts/protocol/payload-delivery/app-gateway/FeesManager.sol"; |
| 11 | + |
| 12 | +import {DeploymentMistakesAppGateway} from "../../src/deployment-mistakes/DeploymentMistakesAppGateway.sol"; |
| 13 | +import {DeploymentMistakesDeployer} from "../../src/deployment-mistakes/DeploymentMistakesDeployer.sol"; |
| 14 | +import { |
| 15 | + NoPlugNoInititialize, |
| 16 | + NoPlugInitialize, |
| 17 | + PlugNoInitialize, |
| 18 | + PlugInitialize, |
| 19 | + PlugInitializeTwice, |
| 20 | + PlugNoInitInitialize |
| 21 | +} from "../../src/deployment-mistakes/DeployOnchainMistakes.sol"; |
| 22 | + |
| 23 | +contract RunEVMxDeploy is Script { |
| 24 | + // ----- ENVIRONMENT VARIABLES ----- |
| 25 | + string rpcEVMx = vm.envString("EVMX_RPC"); |
| 26 | + string rpcArbSepolia = vm.envString("ARBITRUM_SEPOLIA_RPC"); |
| 27 | + address addressResolver = vm.envAddress("ADDRESS_RESOLVER"); |
| 28 | + address auctionManager = vm.envAddress("AUCTION_MANAGER"); |
| 29 | + address feesPlugArbSepolia = vm.envAddress("ARBITRUM_FEES_PLUG"); |
| 30 | + address feesManagerAddress = vm.envAddress("FEES_MANAGER"); |
| 31 | + uint256 privateKey = vm.envUint("PRIVATE_KEY"); |
| 32 | + address deployerAddress = vm.envAddress("DEPLOYER"); |
| 33 | + address appGatewayAddress = vm.envAddress("APP_GATEWAY"); |
| 34 | + |
| 35 | + // ----- SCRIPT VARIABLES ----- |
| 36 | + uint32 arbSepChainId = 411614; |
| 37 | + uint32 opSepChainId = 11155420; |
| 38 | + |
| 39 | + Fees fees = Fees({feePoolChain: arbSepChainId, feePoolToken: ETH_ADDRESS, amount: 0.001 ether}); |
| 40 | + FeesManager feesManager = FeesManager(payable(feesManagerAddress)); |
| 41 | + FeesPlug feesPlug = FeesPlug(payable(feesPlugArbSepolia)); |
| 42 | + |
| 43 | + DeploymentMistakesDeployer deployer = DeploymentMistakesDeployer(deployerAddress); |
| 44 | + DeploymentMistakesAppGateway appGateway = DeploymentMistakesAppGateway(appGatewayAddress); |
| 45 | + address noPlugNoInititializeForwarder; |
| 46 | + address noPlugInitializeForwarder; |
| 47 | + address plugNoInitializeForwarder; |
| 48 | + address plugInitializeForwarder; |
| 49 | + address plugInitializeTwiceForwarder; |
| 50 | + address plugNoInitInitializeForwarder; |
| 51 | + |
| 52 | + function checkDepositedFees(uint32 chainId) internal returns (uint256 availableFees) { |
| 53 | + vm.createSelectFork(rpcEVMx); |
| 54 | + |
| 55 | + (uint256 deposited, uint256 blocked) = |
| 56 | + feesManager.appGatewayFeeBalances(appGatewayAddress, chainId, ETH_ADDRESS); |
| 57 | + console.log("App Gateway:", appGatewayAddress); |
| 58 | + console.log("Deposited fees:", deposited); |
| 59 | + console.log("Blocked fees:", blocked); |
| 60 | + |
| 61 | + availableFees = feesManager.getAvailableFees(chainId, appGatewayAddress, ETH_ADDRESS); |
| 62 | + console.log("Available fees:", availableFees); |
| 63 | + } |
| 64 | + |
| 65 | + function withdrawAppFees(uint32 chainId) internal { |
| 66 | + // EVMX Check available fees |
| 67 | + vm.createSelectFork(rpcEVMx); |
| 68 | + |
| 69 | + uint256 availableFees = feesManager.getAvailableFees(chainId, appGatewayAddress, ETH_ADDRESS); |
| 70 | + console.log("Available fees:", availableFees); |
| 71 | + |
| 72 | + if (availableFees > 0) { |
| 73 | + // Switch to Arbitrum Sepolia to get gas price |
| 74 | + vm.createSelectFork(rpcArbSepolia); |
| 75 | + |
| 76 | + // Gas price from Arbitrum |
| 77 | + uint256 arbitrumGasPrice = block.basefee + 0.1 gwei; // With buffer |
| 78 | + uint256 gasLimit = 5_000_000; // Estimate |
| 79 | + uint256 estimatedGasCost = gasLimit * arbitrumGasPrice; |
| 80 | + |
| 81 | + console.log("Arbitrum gas price (wei):", arbitrumGasPrice); |
| 82 | + console.log("Gas limit:", gasLimit); |
| 83 | + console.log("Estimated gas cost:", estimatedGasCost); |
| 84 | + |
| 85 | + // Calculate amount to withdraw |
| 86 | + uint256 amountToWithdraw = availableFees > estimatedGasCost ? availableFees - estimatedGasCost : 0; |
| 87 | + |
| 88 | + if (amountToWithdraw > 0) { |
| 89 | + // Switch back to EVMX to perform withdrawal |
| 90 | + vm.createSelectFork(rpcEVMx); |
| 91 | + vm.startBroadcast(privateKey); |
| 92 | + address sender = vm.addr(privateKey); |
| 93 | + console.log("Withdrawing amount:", amountToWithdraw); |
| 94 | + appGateway.withdrawFeeTokens(chainId, ETH_ADDRESS, amountToWithdraw, sender); |
| 95 | + vm.stopBroadcast(); |
| 96 | + |
| 97 | + // Switch back to Arbitrum Sepolia to check final balance |
| 98 | + vm.createSelectFork(rpcArbSepolia); |
| 99 | + console.log("Final sender balance:", sender.balance); |
| 100 | + } else { |
| 101 | + console.log("Available fees less than estimated gas cost"); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + function deployOnchainContracts() internal { |
| 107 | + vm.createSelectFork(rpcEVMx); |
| 108 | + vm.startBroadcast(privateKey); |
| 109 | + deployer.deployContracts(arbSepChainId); |
| 110 | + vm.stopBroadcast(); |
| 111 | + |
| 112 | + console.log("Contracts deployed"); |
| 113 | + } |
| 114 | + |
| 115 | + function getForwarderAddresses() internal { |
| 116 | + vm.createSelectFork(rpcEVMx); |
| 117 | + |
| 118 | + noPlugNoInititializeForwarder = deployer.forwarderAddresses(deployer.noPlugNoInititialize(), arbSepChainId); |
| 119 | + console.log("No Plug No Init Forwarder:", noPlugNoInititializeForwarder); |
| 120 | + |
| 121 | + noPlugInitializeForwarder = deployer.forwarderAddresses(deployer.noPlugInitialize(), arbSepChainId); |
| 122 | + console.log("No Plug Init Forwarder:", noPlugInitializeForwarder); |
| 123 | + |
| 124 | + plugNoInitializeForwarder = deployer.forwarderAddresses(deployer.plugNoInitialize(), arbSepChainId); |
| 125 | + console.log("Plug No Init Forwarder:", plugNoInitializeForwarder); |
| 126 | + |
| 127 | + plugInitializeForwarder = deployer.forwarderAddresses(deployer.plugInitialize(), arbSepChainId); |
| 128 | + console.log("Plug Init Forwarder:", plugInitializeForwarder); |
| 129 | + |
| 130 | + plugInitializeTwiceForwarder = deployer.forwarderAddresses(deployer.plugInitializeTwice(), arbSepChainId); |
| 131 | + console.log("Plug Init Init Forwarder:", plugInitializeTwiceForwarder); |
| 132 | + |
| 133 | + plugNoInitInitializeForwarder = deployer.forwarderAddresses(deployer.plugNoInitInitialize(), arbSepChainId); |
| 134 | + console.log("Plug No Init Init Forwarder:", plugNoInitInitializeForwarder); |
| 135 | + } |
| 136 | + |
| 137 | + function validateMistakes() internal { |
| 138 | + NoPlugNoInititialize noPlugNoInititialize = NoPlugNoInititialize(noPlugNoInititializeForwarder); |
| 139 | + NoPlugInitialize noPlugInitialize = NoPlugInitialize(noPlugInitializeForwarder); |
| 140 | + PlugNoInitialize plugNoInitialize = PlugNoInitialize(plugNoInitializeForwarder); |
| 141 | + PlugInitialize plugInitialize = PlugInitialize(plugInitializeForwarder); |
| 142 | + PlugInitializeTwice plugInitializeTwice = PlugInitializeTwice(plugInitializeTwiceForwarder); |
| 143 | + PlugNoInitInitialize plugNoInitInitialize = PlugNoInitInitialize(plugNoInitInitializeForwarder); |
| 144 | + |
| 145 | + vm.createSelectFork(rpcArbSepolia); |
| 146 | + vm.startBroadcast(privateKey); |
| 147 | + |
| 148 | + // NoPlugNoInititialize checks |
| 149 | + require(noPlugNoInititialize.variable() == 0, "variable should be 0"); |
| 150 | + (bool success,) = noPlugNoInititializeForwarder.call(abi.encodeWithSignature("socket__()")); |
| 151 | + require(!success, "Should revert on socket__()"); |
| 152 | + console.log("NoPlugNoInititialize checks passed"); |
| 153 | + |
| 154 | + // NoPlugInitialize checks |
| 155 | + require(noPlugInitialize.variable() == 10, "variable should be 10"); |
| 156 | + (success,) = noPlugInitializeForwarder.call(abi.encodeWithSignature("socket__()")); |
| 157 | + require(!success, "Should revert on socket__()"); |
| 158 | + console.log("NoPlugInitialize checks passed"); |
| 159 | + |
| 160 | + // PlugNoInitialize checks |
| 161 | + require(plugNoInitialize.variable() == 0, "variable should be 0"); |
| 162 | + require(address(plugNoInitialize.socket__()) != address(0), "Should return socket address"); |
| 163 | + console.log("PlugNoInitialize checks passed"); |
| 164 | + |
| 165 | + // PlugInitialize checks |
| 166 | + require(plugInitialize.variable() == 10, "variable should be 10"); |
| 167 | + require(address(plugInitialize.socket__()) != address(0), "Should return socket address"); |
| 168 | + console.log("PlugInitialize checks passed"); |
| 169 | + |
| 170 | + // PlugInitializeTwice checks |
| 171 | + require(address(plugInitializeTwice.socket__()) != address(0), "Should return socket address"); |
| 172 | + require(plugInitializeTwice.variable() == 20, "variable should be 20"); |
| 173 | + console.log("PlugInitializeTwice checks passed"); |
| 174 | + |
| 175 | + // PlugNoInitInitialize checks |
| 176 | + require(plugNoInitInitialize.variable() == 10, "variable should be 10"); |
| 177 | + require(address(plugNoInitInitialize.socket__()) != address(0), "Should return socket address"); |
| 178 | + console.log("PlugNoInitInitialize checks passed"); |
| 179 | + |
| 180 | + vm.stopBroadcast(); |
| 181 | + } |
| 182 | + |
| 183 | + function run() external { |
| 184 | + uint256 availableFees = checkDepositedFees(arbSepChainId); |
| 185 | + |
| 186 | + if (availableFees > 0) { |
| 187 | + // Set up onchain deployments |
| 188 | + deployOnchainContracts(); |
| 189 | + getForwarderAddresses(); |
| 190 | + |
| 191 | + validateMistakes(); |
| 192 | + |
| 193 | + withdrawAppFees(arbSepChainId); |
| 194 | + } else { |
| 195 | + console.log("NO AVAILABLE FEES - Please deposit fees before running this script"); |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments