const { ethers } = require("hardhat"); const fs = require("fs"); const path = require("path"); require('dotenv').config({ path: path.join(__dirname, '..', '.env') }); const deploymentFile = path.join(__dirname, "deployedAddresses.json"); async function main() { const privateKey = process.env.PRIVATE_KEY; const network = await hre.network.name; let deployer; if (network === "hardhat" || network === "localhost") { [deployer] = await ethers.getSigners(); console.log(`Using default hardhat account: ${deployer.address}`); } else { const wallet = new ethers.Wallet(privateKey, ethers.provider); deployer = wallet.connect(ethers.provider); console.log(`Using private key for account: ${deployer.address}`); } console.log("Deploying contracts with the account:", deployer.address); // Select contract based on network let contractName; if (network === "base") { contractName = "PacaFinanceWithBoostAndScheduleBase"; } else if (network === "sonic") { contractName = "PacaFinanceWithBoostAndScheduleSonic"; } else if (network === "mainnet") { contractName = "PacaFinanceWithBoostAndScheduleBsc"; } else { contractName = "PacaFinanceWithBoostAndScheduleBsc"; } console.log(`Deploying ${contractName} for network: ${network}`); // Read deployment data let deploymentData = {}; if (fs.existsSync(deploymentFile)) { deploymentData = JSON.parse(fs.readFileSync(deploymentFile, "utf8")); } if (!deploymentData.proxyAddress) { console.error("No proxy address found in deployment file!"); process.exit(1); } const proxyAddress = deploymentData.proxyAddress; console.log("Proxy address:", proxyAddress); // Deploy new implementation contract console.log("Deploying new implementation contract..."); const PacaFactory = await ethers.getContractFactory(contractName, deployer); const newImplementation = await PacaFactory.deploy(); await newImplementation.waitForDeployment(); const newImplementationAddress = await newImplementation.getAddress(); console.log("New implementation deployed to:", newImplementationAddress); // ProxyAdmin contract address const proxyAdminAddress = "0x3459Fe72D4274d40d449aE1806F8E2149302F28B"; // ProxyAdmin ABI with upgradeAndCall function const proxyAdminABI = [ "function upgradeAndCall(address proxy, address implementation, bytes data) external payable" ]; // Connect to ProxyAdmin contract const proxyAdmin = new ethers.Contract(proxyAdminAddress, proxyAdminABI, deployer); // Perform upgrade with empty call data console.log("Performing upgrade via ProxyAdmin..."); console.log("ProxyAdmin address:", proxyAdminAddress); const upgradeTx = await proxyAdmin.upgradeAndCall(proxyAddress, newImplementationAddress, "0x"); console.log("Upgrade transaction hash:", upgradeTx.hash); // Wait for transaction confirmation await upgradeTx.wait(); console.log("Upgrade completed successfully!"); // Update deployment data deploymentData.implementationAddress = newImplementationAddress; fs.writeFileSync(deploymentFile, JSON.stringify(deploymentData, null, 2)); console.log("Deployment data updated"); // Verify the new implementation console.log("Waiting 10 seconds before verification..."); await new Promise(resolve => setTimeout(resolve, 10000)); try { await hre.run("verify:verify", { address: newImplementationAddress, constructorArguments: [] }); console.log("Contract verified successfully."); } catch (err) { if (err.message.includes("already been verified")) { console.log("Contract is already verified."); } else { console.log("Verification failed:", err.message); console.log(`\nTo verify manually, run:`); console.log(`npx hardhat verify --network ${network} ${newImplementationAddress}`); } } } main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); });