import "dotenv/config";
import { Keypair } from "@solana/web3.js";
import {
createRpc,
bn,
buildAndSignTx,
sendAndConfirmTx,
} from "@lightprotocol/stateless.js";
import {
createMint,
mintTo,
createLoadAtaInstructions,
getAssociatedTokenAddressInterface,
} from "@lightprotocol/compressed-token";
import { homedir } from "os";
import { readFileSync } from "fs";
// devnet:
const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;
const rpc = createRpc(RPC_URL);
// localnet:
// const rpc = createRpc();
const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
)
);
(async function () {
// Setup: mint directly to cold state
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(1000));
const lightTokenAta = getAssociatedTokenAddressInterface(mint, payer.publicKey);
// load from cold to hot state
const ixs = await createLoadAtaInstructions(
rpc,
lightTokenAta,
payer.publicKey,
mint,
payer.publicKey
);
if (ixs.length === 0) return console.log("Nothing to load");
const blockhash = await rpc.getLatestBlockhash();
const tx = buildAndSignTx(ixs, payer, blockhash.blockhash);
const signature = await sendAndConfirmTx(rpc, tx);
console.log("Tx:", signature);
})();