"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 protocol_kit_1 = __importDefault(require("@safe-global/protocol-kit"));
const safeAccountConfig = {
    owners: [
        "0x868CF19464e17F76D6419ACC802B122c22D2FD34",
        "0xcc400c09ecBAC3e0033e4587BdFAABB26223e37d",
        "0x837219D7a9C666F5542c4559Bf17D7B804E5c5fe",
        "0x1d933Fd71FF07E69f066d50B39a7C34EB3b69F05",
        "0x996267d7d1B7f5046543feDe2c2Db473Ed4f65e9",
    ],
    threshold: 2,
};
const EXPECTED_SAFE_ADDRESS = "0x0Fc8E2BB9bEd4FDb51a0d36f2415c4C7F9e75F6e";
const predictedSafe = {
    safeAccountConfig,
    safeDeploymentConfig: {
        // Safe addresses are deterministic based on owners and salt nonce.
        saltNonce: "0x1234",
    },
};
/**
 * 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,
    });
    // Check if the safe already exists:
    const existingProtocolKit = await protocolKit.connect({
        safeAddress: 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 ${EXPECTED_SAFE_ADDRESS}:`, {
            safeAddress,
            safeOwners,
            safeThreshold,
        });
        return;
    }
    // Deploy a new safe:
    const safeAddress = await protocolKit.getAddress();
    if (safeAddress !== EXPECTED_SAFE_ADDRESS) {
        throw new Error(`Safe address ${safeAddress} does not match expected ${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;
});
