98 lines
2.8 KiB
JavaScript
98 lines
2.8 KiB
JavaScript
const hre = require("hardhat");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
|
|
function requiredEnv(name) {
|
|
const value = process.env[name];
|
|
if (!value || !value.trim()) {
|
|
throw new Error(`Missing required env: ${name}`);
|
|
}
|
|
return value.trim();
|
|
}
|
|
|
|
function requireAddress(name, value) {
|
|
if (!hre.ethers.utils.isAddress(value)) {
|
|
throw new Error(`Invalid address for ${name}: ${value}`);
|
|
}
|
|
if (value === hre.ethers.constants.AddressZero) {
|
|
throw new Error(`${name} cannot be zero address`);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function parseUint(name, value, { min = 0, max = Number.MAX_SAFE_INTEGER } = {}) {
|
|
const parsed = Number.parseInt(String(value).trim(), 10);
|
|
if (!Number.isFinite(parsed) || parsed < min || parsed > max) {
|
|
throw new Error(`Invalid integer for ${name}: ${value}`);
|
|
}
|
|
return parsed;
|
|
}
|
|
|
|
function parseGuardianAddresses(raw) {
|
|
const values = raw
|
|
.split(",")
|
|
.map((v) => v.trim())
|
|
.filter(Boolean);
|
|
if (values.length === 0) {
|
|
throw new Error("LASTLIGHT_GUARDIANS must include at least one address");
|
|
}
|
|
|
|
const dedupe = new Set();
|
|
const out = [];
|
|
for (const value of values) {
|
|
const checksum = requireAddress("LASTLIGHT_GUARDIANS", value);
|
|
const key = checksum.toLowerCase();
|
|
if (dedupe.has(key)) {
|
|
throw new Error(`Duplicate guardian address: ${checksum}`);
|
|
}
|
|
dedupe.add(key);
|
|
out.push(checksum);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
async function main() {
|
|
const guardians = parseGuardianAddresses(requiredEnv("LASTLIGHT_GUARDIANS"));
|
|
const threshold = parseUint(
|
|
"LASTLIGHT_THRESHOLD",
|
|
process.env.LASTLIGHT_THRESHOLD || "3",
|
|
{ min: 1, max: guardians.length },
|
|
);
|
|
|
|
const [deployer] = await hre.ethers.getSigners();
|
|
console.log("deployer:", deployer.address);
|
|
console.log("network:", hre.network.name);
|
|
console.log("guardian_count:", guardians.length);
|
|
console.log("threshold:", threshold);
|
|
|
|
const factory = await hre.ethers.getContractFactory("LastLightController");
|
|
const contract = await factory.deploy(guardians, threshold);
|
|
await contract.deployed();
|
|
|
|
console.log("lastlight_contract:", contract.address);
|
|
|
|
const output = {
|
|
network: hre.network.name,
|
|
chain_id: hre.network.config.chainId || null,
|
|
deployer: deployer.address,
|
|
lastlight_contract: contract.address,
|
|
guardian_count: guardians.length,
|
|
threshold,
|
|
deployment_tx_hash: contract.deployTransaction.hash,
|
|
guardians,
|
|
};
|
|
|
|
const outputPath = (process.env.LASTLIGHT_DEPLOY_OUTPUT_PATH || "").trim();
|
|
if (outputPath) {
|
|
const absolute = path.resolve(outputPath);
|
|
fs.mkdirSync(path.dirname(absolute), { recursive: true });
|
|
fs.writeFileSync(absolute, JSON.stringify(output, null, 2));
|
|
console.log("deployment_output:", absolute);
|
|
}
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(err);
|
|
process.exitCode = 1;
|
|
});
|