import "dotenv/config";
import {
Keypair,
Transaction,
sendAndConfirmTransaction,
} from "@solana/web3.js";
import { createRpc } from "@lightprotocol/stateless.js";
import {
createMintInterface,
mintToCompressed,
getAssociatedTokenAddressInterface,
} from "@lightprotocol/compressed-token";
import { createLoadAtaInstructions } from "@lightprotocol/compressed-token/unified";
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 () {
// Inactive Light Tokens are cryptographically preserved on the Solana ledger
// as compressed tokens (cold storage)
// Setup: Get compressed tokens in light-token associated token account
const { mint } = await createMintInterface(rpc, payer, payer, null, 9);
await mintToCompressed(rpc, payer, mint, payer, [
{ recipient: payer.publicKey, amount: 1000n },
]);
const lightTokenAta = getAssociatedTokenAddressInterface(
mint,
payer.publicKey
);
// Load compressed tokens to light associated token account (hot balance)
const instructions = await createLoadAtaInstructions(
rpc,
lightTokenAta,
payer.publicKey,
mint,
payer.publicKey
);
if (instructions.length === 0) return console.log("Nothing to load");
for (const ixs of instructions) {
const tx = new Transaction().add(...ixs);
const sig = await sendAndConfirmTransaction(rpc, tx, [payer]);
console.log("Tx:", sig);
}
})();