"use strict";
// This script sets new minimum deposit amount for a supported burnToken on a SVM Sponsored CCTP bridge.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const anchor = __importStar(require("@coral-xyz/anchor"));
const anchor_1 = require("@coral-xyz/anchor");
const web3_js_1 = require("@solana/web3.js");
const bs58_1 = __importDefault(require("bs58"));
const yargs_1 = __importDefault(require("yargs"));
const helpers_1 = require("yargs/helpers");
const web3_v1_1 = require("../../../src/svm/web3-v1");
// Set up the provider and program
const provider = anchor_1.AnchorProvider.env();
anchor.setProvider(provider);
const program = (0, web3_v1_1.getSponsoredCctpSrcPeripheryProgram)(provider);
const programId = program.programId;
// Parse arguments
const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
    .option("burnToken", {
    type: "string",
    demandOption: true,
    describe: "Burn token mint address to set minimum deposit for",
})
    .option("amount", {
    type: "number",
    demandOption: false,
    default: 0,
    describe: "Minimum deposit amount (raw value)",
}).argv;
async function setMinimumDeposit() {
    const resolvedArgv = await argv;
    const burnToken = new web3_js_1.PublicKey(resolvedArgv.burnToken);
    const amount = new anchor.BN(resolvedArgv.amount);
    const txSigner = provider.wallet.publicKey;
    const txPayer = provider.wallet.payer;
    if (!txPayer) {
        throw new Error("Provider wallet does not have a keypair");
    }
    const [programData] = web3_js_1.PublicKey.findProgramAddressSync([program.programId.toBuffer()], new web3_js_1.PublicKey("BPFLoaderUpgradeab1e11111111111111111111111"));
    const upgradeAuthority = await (async () => {
        const { value } = await provider.connection.getParsedAccountInfo(programData);
        const authority = value?.data && !Buffer.isBuffer(value?.data) ? value?.data.parsed?.info?.authority : undefined;
        if (authority)
            return new web3_js_1.PublicKey(authority);
        throw new Error("Could not get parsed program data account");
    })();
    const [state] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("state")], program.programId);
    console.log("Setting new minimum deposit amount...");
    console.table([
        { Property: "programId", Value: programId.toString() },
        { Property: "burnToken", Value: burnToken.toString() },
        { Property: "amount", Value: amount.toString() },
        { Property: "txPayer", Value: txPayer.publicKey.toString() },
        { Property: "txSigner", Value: txSigner.toString() },
        { Property: "upgradeAuthority", Value: upgradeAuthority.toString() },
        { Property: "programData", Value: programData.toString() },
        { Property: "state", Value: state.toString() },
    ]);
    const ix = await program.methods
        .setMinimumDepositAmount({ amount })
        .accounts({
        signer: upgradeAuthority,
        burnToken,
        programData,
    })
        .instruction();
    if (txSigner.equals(upgradeAuthority)) {
        console.log("Signer is the upgrade authority, setting new quote signer...");
        const tx = new web3_js_1.Transaction().add(ix);
        const txSignature = await provider.sendAndConfirm(tx, [txPayer], { commitment: "confirmed" });
        console.log("Minimum deposit amount set successfully, transaction signature:", txSignature);
    }
    else {
        console.log("Signer is not the upgrade authority, can only simulate the transaction or import it into the multi-sig transaction builder.");
        const tx = new web3_js_1.Transaction({
            feePayer: txSigner,
            blockhash: web3_js_1.PublicKey.default.toString(),
            lastValidBlockHeight: 0,
        }).add(ix);
        // Should throw upon simulation error
        await provider.simulate(tx);
        const encodedTx = bs58_1.default.encode(Buffer.from(tx.serialize({ requireAllSignatures: false, verifySignatures: false })));
        console.log("Simulation successful, import the base58 encoded transaction:\n", encodedTx);
    }
}
setMinimumDeposit();
