const { ethers, upgrades } = require("hardhat"); const fs = require("fs"); const path = require("path"); // File to store deployment addresses const DEPLOYMENT_FILE = path.join(__dirname, "deployments.json"); // Load existing deployments function loadDeployments() { if (fs.existsSync(DEPLOYMENT_FILE)) { return JSON.parse(fs.readFileSync(DEPLOYMENT_FILE, "utf8")); } return {}; } // Save deployment info function saveDeployment(network, contractName, proxyAddress, implementationAddress) { const deployments = loadDeployments(); if (!deployments[network]) { deployments[network] = {}; } deployments[network][contractName] = { proxy: proxyAddress, implementation: implementationAddress, deployedAt: new Date().toISOString() }; fs.writeFileSync(DEPLOYMENT_FILE, JSON.stringify(deployments, null, 2)); } async function main() { const network = hre.network.name; console.log(`\nšŸš€ Deploying to network: ${network}\n`); const [deployer] = await ethers.getSigners(); console.log(`šŸ“ Deploying with account: ${deployer.address}`); console.log(`šŸ’° Account balance: ${ethers.formatEther(await deployer.provider.getBalance(deployer.address))} ETH\n`); const contractName = "CunaFinanceBsc"; const deployments = loadDeployments(); const existingDeployment = deployments[network]?.[contractName]; try { if (existingDeployment?.proxy) { console.log(`šŸ”„ Existing proxy found at: ${existingDeployment.proxy}`); console.log("šŸ“¦ Upgrading contract...\n"); // Get the contract factory const CunaFinanceBsc = await ethers.getContractFactory(contractName); // Upgrade the proxy const upgraded = await upgrades.upgradeProxy(existingDeployment.proxy, CunaFinanceBsc); await upgraded.waitForDeployment(); const newImplementationAddress = await upgrades.erc1967.getImplementationAddress(existingDeployment.proxy); console.log(`āœ… Contract upgraded successfully!`); console.log(`šŸ“ Proxy address: ${existingDeployment.proxy}`); console.log(`šŸ“ New implementation: ${newImplementationAddress}\n`); // Save the new implementation address saveDeployment(network, contractName, existingDeployment.proxy, newImplementationAddress); // Verify the new implementation if (network !== "hardhat" && network !== "localhost") { console.log("šŸ” Verifying new implementation..."); try { await hre.run("verify:verify", { address: newImplementationAddress, constructorArguments: [], }); console.log("āœ… Implementation verified successfully!\n"); } catch (error) { console.log(`āš ļø Verification failed: ${error.message}\n`); } } } else { console.log("šŸ†• No existing deployment found. Deploying fresh proxy...\n"); // Get the contract factory const CunaFinanceBsc = await ethers.getContractFactory(contractName); // Deploy the proxy const proxy = await upgrades.deployProxy(CunaFinanceBsc, [], { initializer: "initialize", kind: "transparent" }); await proxy.waitForDeployment(); const proxyAddress = await proxy.getAddress(); const implementationAddress = await upgrades.erc1967.getImplementationAddress(proxyAddress); console.log(`āœ… Contract deployed successfully!`); console.log(`šŸ“ Proxy address: ${proxyAddress}`); console.log(`šŸ“ Implementation: ${implementationAddress}\n`); // Save deployment info saveDeployment(network, contractName, proxyAddress, implementationAddress); // Verify contracts if (network !== "hardhat" && network !== "localhost") { console.log("šŸ” Verifying contracts...\n"); // Verify implementation try { await hre.run("verify:verify", { address: implementationAddress, constructorArguments: [], }); console.log("āœ… Implementation verified successfully!"); } catch (error) { console.log(`āš ļø Implementation verification failed: ${error.message}`); } // Get proxy admin address for verification try { const adminAddress = await upgrades.erc1967.getAdminAddress(proxyAddress); console.log(`šŸ“ Proxy Admin: ${adminAddress}`); // Verify proxy admin await hre.run("verify:verify", { address: adminAddress, constructorArguments: [], }); console.log("āœ… Proxy Admin verified successfully!"); } catch (error) { console.log(`āš ļø Proxy Admin verification failed: ${error.message}`); } } } console.log("\nšŸŽ‰ Deployment/Upgrade completed successfully!"); console.log(`šŸ“„ Deployment info saved to: ${DEPLOYMENT_FILE}`); } catch (error) { console.error("\nāŒ Deployment failed:", error.message); process.exit(1); } } // Execute the script main() .then(() => process.exit(0)) .catch((error) => { console.error("\nāŒ Script execution failed:", error); process.exit(1); });