const { ethers } = require("hardhat");

const IDENTITY_REGISTRY = "0xa7BD20bf5De63df949cA5Be2F20835978eCba81A";

async function deployOne(name, args, deployer) {
  await new Promise(r => setTimeout(r, 5000));
  const nonce = await ethers.provider.getTransactionCount(deployer.address, "pending");
  console.log(`   Deploying with nonce ${nonce}...`);
  const Factory = await ethers.getContractFactory(name);
  const contract = await Factory.deploy(...args, { nonce });
  await contract.waitForDeployment();
  const addr = await contract.getAddress();
  console.log(`   ${name}: ${addr}`);
  return addr;
}

async function main() {
  const [deployer] = await ethers.getSigners();
  console.log(`\n=== Deploying remaining 4 contracts on Base ===`);
  console.log(`Deployer: ${deployer.address}`);
  console.log(`Balance: ${ethers.formatEther(await ethers.provider.getBalance(deployer.address))} ETH\n`);

  const results = {
    reputation_staking: "0xd42BBfD3061A38D4B5540317A31D90212380069b" // Already deployed
  };

  console.log("2/5 VaultfireTrustOracle");
  results.trust_oracle = await deployOne("VaultfireTrustOracle", [IDENTITY_REGISTRY], deployer);

  console.log("3/5 VaultfireBondInsurancePool");
  results.bond_insurance_pool = await deployOne("VaultfireBondInsurancePool", [IDENTITY_REGISTRY], deployer);

  console.log("4/5 VaultfireCapabilityCredentials");
  results.capability_credentials = await deployOne("VaultfireCapabilityCredentials", [IDENTITY_REGISTRY], deployer);

  console.log("5/5 VaultfireDisputeResolution");
  results.dispute_resolution = await deployOne("VaultfireDisputeResolution", [IDENTITY_REGISTRY], deployer);

  console.log(`\n=== All Base contracts ===`);
  console.log(JSON.stringify(results, null, 2));
  console.log(`\nRemaining balance: ${ethers.formatEther(await ethers.provider.getBalance(deployer.address))} ETH`);
}

main().then(() => process.exit(0)).catch(e => { console.error(e); process.exit(1); });
