#!/usr/bin/env node
"use strict";
const { Connection, PublicKey, Transaction, sendAndConfirmTransaction } = require("@solana/web3.js");
const { TOKEN_2022_PROGRAM_ID, getAssociatedTokenAddressSync, createBurnCheckedInstruction } = require("@solana/spl-token");
const { parseEnvConfig, assertRequired, resolveConfigPath, parseCluster, ui, getMintPublicKeyFromConfig, getOperatorKeypairFromConfig } = require("./_common");

async function main() {
  const cfg = parseEnvConfig(resolveConfigPath(process.argv[2]));
  const targetOwner = new PublicKey(process.argv[3]);
  if (!process.argv[3]) throw new Error("Использование / Usage: node .../03...js <config.env> <target_owner_wallet>");
  assertRequired(cfg, "GT_CLUSTER");
  const mint = getMintPublicKeyFromConfig(cfg);
  const operator = getOperatorKeypairFromConfig(cfg);
  const connection = new Connection(parseCluster(cfg.GT_CLUSTER), "confirmed");
  const targetAta = getAssociatedTokenAddressSync(mint, targetOwner, false, TOKEN_2022_PROGRAM_ID);
  const ix = createBurnCheckedInstruction(targetAta, mint, operator.publicKey, 1n, 0, [], TOKEN_2022_PROGRAM_ID);
  ui.title("=== Принудительное сжигание 1 токена / Force burn 1 token ===");
  const sig = await sendAndConfirmTransaction(connection, new Transaction().add(ix), [operator], { commitment: "confirmed" });
  ui.ok("Успешно / Success");
  ui.info(`Mint: ${mint.toBase58()}`); ui.info(`Wallet: ${targetOwner.toBase58()}`); ui.info(`Tx: ${sig}`);
}
main().catch((e) => { ui.err(`Ошибка / Error: ${e?.message || e}`); process.exit(1); });
