"use strict";
// This script is used by a chain admin to enable or disable a route for a token on the Solana Spoke Pool.
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 spl_token_1 = require("@solana/spl-token");
const yargs_1 = __importDefault(require("yargs"));
const helpers_1 = require("yargs/helpers");
// Set up the provider
const provider = anchor_1.AnchorProvider.env();
anchor.setProvider(provider);
const idl = require("../../target/idl/svm_spoke.json");
const program = new anchor_1.Program(idl, provider);
const programId = program.programId;
// Parse arguments
const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
    .option("seed", { type: "string", demandOption: true, describe: "Seed for the state account PDA" })
    .option("originToken", { type: "string", demandOption: true, describe: "Origin token public key" })
    .option("chainId", { type: "string", demandOption: true, describe: "Chain ID" })
    .option("enabled", { type: "boolean", demandOption: true, describe: "Enable or disable the route" }).argv;
async function enableRoute() {
    const resolvedArgv = await argv;
    const seed = new anchor_1.BN(resolvedArgv.seed);
    const originToken = new web3_js_1.PublicKey(resolvedArgv.originToken);
    const chainId = new anchor_1.BN(resolvedArgv.chainId);
    const enabled = resolvedArgv.enabled;
    // Define the state account PDA
    const [statePda, _] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("state"), seed.toArrayLike(Buffer, "le", 8)], programId);
    // Define the route account PDA
    const [routePda] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("route"), originToken.toBytes(), statePda.toBytes(), chainId.toArrayLike(Buffer, "le", 8)], programId);
    // Define the signer (replace with your actual signer)
    const signer = provider.wallet.publicKey;
    console.log("Enabling route...");
    console.table([
        { Property: "seed", Value: seed.toString() },
        { Property: "originToken", Value: originToken.toString() },
        { Property: "chainId", Value: chainId.toString() },
        { Property: "enabled", Value: enabled },
        { Property: "programId", Value: programId.toString() },
        { Property: "providerPublicKey", Value: provider.wallet.publicKey.toString() },
        { Property: "statePda", Value: statePda.toString() },
        { Property: "routePda", Value: routePda.toString() },
    ]);
    // Create ATA for the origin token to be stored by state (vault).
    const vault = (0, spl_token_1.getAssociatedTokenAddressSync)(originToken, statePda, true, spl_token_1.TOKEN_PROGRAM_ID, spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID);
    const tx = await program.methods.setEnableRoute(originToken, chainId, enabled)
        .accounts({
        signer: signer,
        payer: signer,
        state: statePda,
        route: routePda,
        vault: vault,
        originTokenMint: originToken,
        tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
        associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
        systemProgram: web3_js_1.SystemProgram.programId,
    })
        .rpc();
    console.log("Transaction signature:", tx);
}
// Run the enableRoute function
enableRoute();
