"use strict";
// This script reclaims rent_fund debt to the depositor of the 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 yargs_1 = __importDefault(require("yargs"));
const helpers_1 = require("yargs/helpers");
const web3_v1_1 = require("../../../src/svm/web3-v1");
const web3_js_1 = require("@solana/web3.js");
// Set up the provider
const provider = anchor_1.AnchorProvider.env();
anchor.setProvider(provider);
const program = (0, web3_v1_1.getSponsoredCctpSrcPeripheryProgram)(provider);
const programId = program.programId;
// Parse arguments
const argvPromise = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv)).option("recipient", {
    type: "string",
    demandOption: false,
    describe: "Depositor address to repay rent_fund debt, defaults to signer",
}).argv;
async function repayRentFundDebt() {
    const argv = await argvPromise;
    const txPayer = provider.wallet.payer;
    if (!txPayer) {
        throw new Error("Provider wallet does not have a keypair");
    }
    const recipient = new web3_js_1.PublicKey(argv.recipient || txPayer.publicKey.toString());
    const [rentFund] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("rent_fund")], programId);
    const [rentClaim] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("rent_claim"), recipient.toBuffer()], programId);
    console.table([
        { Property: "programId", Value: programId.toString() },
        { Property: "recipient", Value: recipient.toString() },
        { Property: "rentFund", Value: rentFund.toString() },
        { Property: "rentClaim", Value: rentClaim.toString() },
        { Property: "txPayer", Value: txPayer.publicKey.toString() },
    ]);
    const ix = await program.methods.repayRentFundDebt().accounts({ recipient, program: programId }).instruction();
    console.log("Reclaiming rent_fund debt to the depositor...");
    const tx = new web3_js_1.Transaction().add(ix);
    const txSignature = await provider.sendAndConfirm(tx, [txPayer], { commitment: "confirmed" });
    console.log("Repaid debt from rent fund successfully, transaction signature:", txSignature);
}
repayRentFundDebt();
