|
1 | 1 | import "dotenv/config"; |
2 | 2 | import { Keypair } from "@solana/web3.js"; |
3 | 3 | import { createRpc, bn } from "@lightprotocol/stateless.js"; |
4 | | -import { |
5 | | - createMint, |
6 | | - mintTo, |
7 | | - decompress, |
8 | | - compress, |
9 | | -} from "@lightprotocol/compressed-token"; |
| 4 | +import { createMint, mintTo, decompress, compress } from "@lightprotocol/compressed-token"; |
10 | 5 | import { createAssociatedTokenAccount } from "@solana/spl-token"; |
| 6 | +import { homedir } from "os"; |
| 7 | +import { readFileSync } from "fs"; |
11 | 8 |
|
12 | | -async function main() { |
13 | | - // 1. Setup RPC and fund accounts |
14 | | - const rpc = createRpc(); |
15 | | - const payer = Keypair.generate(); |
16 | | - const airdropSig = await rpc.requestAirdrop(payer.publicKey, 10e9); |
17 | | - await rpc.confirmTransaction(airdropSig, "confirmed"); |
18 | | - console.log("Payer:", payer.publicKey.toBase58()); |
| 9 | +const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`; |
| 10 | +const payer = Keypair.fromSecretKey( |
| 11 | + new Uint8Array( |
| 12 | + JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8")) |
| 13 | + ) |
| 14 | +); |
19 | 15 |
|
20 | | - const owner = Keypair.generate(); |
21 | | - const airdropSig2 = await rpc.requestAirdrop(owner.publicKey, 1e9); |
22 | | - await rpc.confirmTransaction(airdropSig2, "confirmed"); |
| 16 | +(async function () { |
| 17 | + const rpc = createRpc(RPC_URL); |
23 | 18 |
|
24 | | - // 2. Create SPL mint with token pool |
25 | | - const mintAuthority = Keypair.generate(); |
26 | | - const mintKeypair = Keypair.generate(); |
27 | | - const { mint } = await createMint( |
28 | | - rpc, |
29 | | - payer, |
30 | | - mintAuthority.publicKey, |
31 | | - 9, |
32 | | - mintKeypair |
33 | | - ); |
34 | | - console.log("Mint:", mint.toBase58()); |
| 19 | + // Setup: Get SPL tokens (needed to compress) |
| 20 | + const { mint } = await createMint(rpc, payer, payer.publicKey, 9); |
| 21 | + const splAta = await createAssociatedTokenAccount(rpc, payer, mint, payer.publicKey); |
| 22 | + await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(10000)); |
| 23 | + await decompress(rpc, payer, mint, bn(10000), payer, splAta); |
35 | 24 |
|
36 | | - // 3. Create SPL ATA and fund it |
37 | | - const splAta = await createAssociatedTokenAccount( |
38 | | - rpc, |
39 | | - payer, |
40 | | - mint, |
41 | | - owner.publicKey |
42 | | - ); |
43 | | - console.log("SPL ATA:", splAta.toBase58()); |
| 25 | + // Batch compress to multiple recipients |
| 26 | + const recipients = Array.from({ length: 5 }, () => Keypair.generate().publicKey); |
| 27 | + const amounts = [bn(100), bn(200), bn(300), bn(400), bn(500)]; |
44 | 28 |
|
45 | | - await mintTo(rpc, payer, mint, owner.publicKey, mintAuthority, bn(10000)); |
46 | | - await decompress(rpc, payer, mint, bn(10000), owner, splAta); |
47 | | - console.log("Funded SPL ATA with 10000 tokens"); |
| 29 | + const tx = await compress(rpc, payer, mint, amounts, payer, splAta, recipients); |
48 | 30 |
|
49 | | - // 4. Batch compress to multiple recipients (max 10 for V2 trees) |
50 | | - const recipients = Array.from({ length: 5 }, () => Keypair.generate().publicKey); |
51 | | - const amounts = [bn(100), bn(200), bn(300), bn(400), bn(500)]; |
52 | | - |
53 | | - const signature = await compress( |
54 | | - rpc, |
55 | | - payer, |
56 | | - mint, |
57 | | - amounts, // array of amounts |
58 | | - owner, |
59 | | - splAta, |
60 | | - recipients // array of recipients (same length as amounts) |
61 | | - ); |
62 | | - |
63 | | - console.log("Batch compressed to 5 recipients"); |
64 | | - console.log("Amounts:", amounts.map((a) => a.toString()).join(", ")); |
65 | | - console.log("Transaction:", signature); |
66 | | -} |
67 | | - |
68 | | -main().catch((err) => { |
69 | | - console.error("Error:", err); |
70 | | - if (err.logs) console.error("Logs:", err.logs); |
71 | | - process.exit(1); |
72 | | -}); |
| 31 | + console.log("Tx:", tx); |
| 32 | +})(); |
0 commit comments