"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const utils_2 = require("../utils/utils");
const utils_hre_1 = require("../utils/utils.hre");
const consts_1 = require("../deploy/consts");
const protocol_kit_1 = __importDefault(require("@safe-global/protocol-kit"));
/**
 * Script to deploy a new Safe Multisig contract via the Safe SDK. Run via:
 * ```
 * yarn hardhat run ./scripts/deployMultisig.ts \
 * --network hyperevm \
 * ```
 */
async function main() {
    const chainId = parseInt(await utils_hre_1.hre.getChainId());
    const nodeUrl = (0, utils_1.getNodeUrl)(chainId);
    const wallet = utils_2.ethers.Wallet.fromMnemonic(utils_hre_1.hre.network.config.accounts.mnemonic);
    const privateKey = wallet._signingKey().privateKey;
    console.log(`Connected to node ${nodeUrl} for chain ${chainId}`);
    const signer = wallet.connect(new utils_2.ethers.providers.JsonRpcProvider(nodeUrl));
    const protocolKit = await protocol_kit_1.default.init({
        provider: nodeUrl,
        signer: privateKey,
        predictedSafe: utils_2.predictedSafe,
    });
    // Check if the safe already exists:
    const existingProtocolKit = await protocolKit.connect({
        safeAddress: consts_1.EXPECTED_SAFE_ADDRESS,
    });
    const isDeployed = await existingProtocolKit.isSafeDeployed();
    if (isDeployed) {
        const safeAddress = await existingProtocolKit.getAddress();
        const safeOwners = await existingProtocolKit.getOwners();
        const safeThreshold = await existingProtocolKit.getThreshold();
        console.log(`Safe already exists at ${consts_1.EXPECTED_SAFE_ADDRESS}:`, {
            safeAddress,
            safeOwners,
            safeThreshold,
        });
        return;
    }
    // Deploy a new safe:
    const safeAddress = await protocolKit.getAddress();
    if (safeAddress !== consts_1.EXPECTED_SAFE_ADDRESS) {
        throw new Error(`Safe address ${safeAddress} does not match expected ${consts_1.EXPECTED_SAFE_ADDRESS}`);
    }
    console.log(`Deploying a new safe with determinstic address: ${safeAddress}`);
    const deploymentTransaction = await protocolKit.createSafeDeploymentTransaction();
    console.log(`Deployment txn data`, deploymentTransaction);
    const client = await protocolKit.getSafeProvider().getExternalSigner();
    if (!client) {
        throw new Error("Unable to get external signer from safe provider");
    }
    const deployerAccount = client.account.address;
    console.log(`Deployer account: ${deployerAccount}`);
    const clientConnectedChain = client.chain;
    if (client.chain?.id !== chainId) {
        throw new Error(`Client connected to chain ${clientConnectedChain?.id}, but expected ${chainId}`);
    }
    if (deploymentTransaction.value.toString() !== "0") {
        throw new Error(`Deployment transaction value should be 0, but is ${deploymentTransaction.value}`);
    }
    console.log(`Sending deployment transaction...`);
    const txnHash = await signer.sendTransaction({
        to: deploymentTransaction.to,
        value: 0,
        data: deploymentTransaction.data,
    });
    const txnReceipt = await txnHash.wait();
    console.log(`Success! Deployment transaction receipt:`, txnReceipt);
}
main().catch((error) => {
    console.error(error);
    process.exitCode = 1;
});
