diff --git a/toolkits/indexing-tokens/package.json b/toolkits/indexing-tokens/package.json new file mode 100644 index 0000000..ce2dee1 --- /dev/null +++ b/toolkits/indexing-tokens/package.json @@ -0,0 +1,9 @@ +{ + "name": "light-token-toolkit-indexing", + "version": "1.0.0", + "type": "module", + "scripts": { + "warm-up-action": "tsx warm-up-action.ts", + "warm-up-instruction": "tsx warm-up-instruction.ts" + } +} \ No newline at end of file diff --git a/toolkits/indexing-tokens/warm-up-action.ts b/toolkits/indexing-tokens/warm-up-action.ts new file mode 100644 index 0000000..1ffef4e --- /dev/null +++ b/toolkits/indexing-tokens/warm-up-action.ts @@ -0,0 +1,56 @@ +import "dotenv/config"; +import { Keypair } from "@solana/web3.js"; +import { createRpc } from "@lightprotocol/stateless.js"; +import { + createMintInterface, + createAtaInterface, + mintToCompressed, + loadAta, + transferInterface, + 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 () { + // 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 recipient = Keypair.generate(); + await createAtaInterface(rpc, payer, mint, recipient.publicKey); + + const senderAta = getAssociatedTokenAddressInterface(mint, payer.publicKey); + const recipientAta = getAssociatedTokenAddressInterface(mint, recipient.publicKey); + + // Warm up: load compressed tokens to light-token associated token account (hot balance) + // Returns null if already hot + await loadAta(rpc, senderAta, payer, mint, payer); + + // Transfer tokens + const tx = await transferInterface( + rpc, + payer, + senderAta, + mint, + recipientAta, + payer, + 500n, + ); + + console.log("Tx:", tx); +})(); diff --git a/toolkits/indexing-tokens/warm-up-instruction.ts b/toolkits/indexing-tokens/warm-up-instruction.ts new file mode 100644 index 0000000..da216ff --- /dev/null +++ b/toolkits/indexing-tokens/warm-up-instruction.ts @@ -0,0 +1,76 @@ +import "dotenv/config"; +import { Keypair } from "@solana/web3.js"; +import { + createRpc, + buildAndSignTx, + sendAndConfirmTx, +} from "@lightprotocol/stateless.js"; +import { + createMintInterface, + createAtaInterface, + mintToCompressed, + createLoadAtaInstructions, + createTransferInterfaceInstruction, + 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 () { + // 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 recipient = Keypair.generate(); + await createAtaInterface(rpc, payer, mint, recipient.publicKey); + + const senderAta = getAssociatedTokenAddressInterface(mint, payer.publicKey); + const recipientAta = getAssociatedTokenAddressInterface( + mint, + recipient.publicKey, + ); + + // Warm up: load compressed tokens to light-token associated token account (hot balance) + // Returns null if already hot + const loadIxs = await createLoadAtaInstructions( + rpc, + senderAta, + payer.publicKey, + mint, + payer.publicKey, + ); + + if (loadIxs.length > 0) { + const blockhash = await rpc.getLatestBlockhash(); + const loadTx = buildAndSignTx(loadIxs, payer, blockhash.blockhash); + await sendAndConfirmTx(rpc, loadTx); + } + + // Transfer tokens + const transferIx = createTransferInterfaceInstruction( + senderAta, + recipientAta, + payer.publicKey, + 500n, + ); + + const blockhash = await rpc.getLatestBlockhash(); + const tradeTx = buildAndSignTx([transferIx], payer, blockhash.blockhash); + const signature = await sendAndConfirmTx(rpc, tradeTx); + + console.log("Tx:", signature); +})();