const fs = require("fs"); const path = require("path"); const hre = require("hardhat"); async function main() { let contractAddressInput = process.env.ASTRAL_ACCESS_ADDRESS || process.argv[2]; if (!contractAddressInput) { const [deployer] = await hre.ethers.getSigners(); const AstralAccess = await hre.ethers.getContractFactory("AstralAccess", deployer); const contract = await AstralAccess.deploy(); await contract.waitForDeployment(); contractAddressInput = await contract.getAddress(); console.log(`No contract address provided. Deployed AstralAccess at ${contractAddressInput}`); } if (!hre.ethers.isAddress(contractAddressInput)) { throw new Error("Invalid address. Use ASTRAL_ACCESS_ADDRESS or pass a valid CLI argument."); } const normalizedAddress = hre.ethers.getAddress(contractAddressInput); const artifactPath = path.join( __dirname, "..", "artifacts", "contracts", "AstralAccess.sol", "AstralAccess.json" ); if (!fs.existsSync(artifactPath)) { throw new Error(`Artifact not found: ${artifactPath}. Run 'npx hardhat compile' first.`); } const artifact = JSON.parse(fs.readFileSync(artifactPath, "utf8")); const outputPath = path.join(__dirname, "..", "..", "middleware", "blockchain_config.json"); fs.mkdirSync(path.dirname(outputPath), { recursive: true }); fs.writeFileSync( outputPath, JSON.stringify( { contractAddress: normalizedAddress, abi: artifact.abi, }, null, 2 ) ); console.log(`Exported blockchain config to ${outputPath}`); } main().catch((error) => { console.error(error); process.exitCode = 1; });