|
| 1 | +import * as web3 from "@solana/web3.js"; |
| 2 | +import { Keypair, PublicKey } from "@solana/web3.js"; |
| 3 | +import { CompressedTokenProgram } from "@lightprotocol/compressed-token"; |
| 4 | +import { |
| 5 | + bn, |
| 6 | + buildAndSignTx, |
| 7 | + calculateComputeUnitPrice, |
| 8 | + createRpc, |
| 9 | + dedupeSigner, |
| 10 | + pickRandomTreeAndQueue, |
| 11 | + Rpc, |
| 12 | + sendAndConfirmTx, |
| 13 | +} from "@lightprotocol/stateless.js"; |
| 14 | +import * as splToken from "@solana/spl-token"; |
| 15 | +import dotenv from "dotenv"; |
| 16 | +import bs58 from "bs58"; |
| 17 | +dotenv.config(); |
| 18 | + |
| 19 | +const RPC_ENDPOINT = process.env.RPC_ENDPOINT; |
| 20 | +const MINT_ADDRESS = new PublicKey(process.env.MINT_ADDRESS!); |
| 21 | +const PAYER_KEYPAIR = Keypair.fromSecretKey( |
| 22 | + bs58.decode(process.env.PAYER_KEYPAIR!) |
| 23 | +); |
| 24 | + |
| 25 | +(async () => { |
| 26 | + try { |
| 27 | + const connection: Rpc = createRpc(RPC_ENDPOINT); |
| 28 | + const mintAddress = MINT_ADDRESS; |
| 29 | + const payer = PAYER_KEYPAIR; |
| 30 | + |
| 31 | + const activeStateTrees = await connection.getCachedActiveStateTreeInfo(); |
| 32 | + |
| 33 | + /// Pick a new tree for each transaction! |
| 34 | + const { tree } = pickRandomTreeAndQueue(activeStateTrees); |
| 35 | + |
| 36 | + // Create an SPL token account for the sender. |
| 37 | + // The sender will send tokens from this account to the recipients as compressed tokens. |
| 38 | + const sourceTokenAccount = await splToken.getOrCreateAssociatedTokenAccount( |
| 39 | + connection, |
| 40 | + payer, |
| 41 | + mintAddress, |
| 42 | + payer.publicKey |
| 43 | + ); |
| 44 | + |
| 45 | + // Airdrop to example recipients addresses |
| 46 | + // 1 recipient = 120_000 CU |
| 47 | + // 5 recipients = 170_000 CU |
| 48 | + const airDropAddresses = [ |
| 49 | + "GMPWaPPrCeZPse5kwSR3WUrqYAPrVZBSVwymqh7auNW7", |
| 50 | + ].map((address) => new web3.PublicKey(address)); |
| 51 | + |
| 52 | + const amount = bn(111); // each recipient will receive 111 tokens |
| 53 | + const instructions: web3.TransactionInstruction[] = []; |
| 54 | + |
| 55 | + instructions.push( |
| 56 | + web3.ComputeBudgetProgram.setComputeUnitLimit({ units: 120_000 }), |
| 57 | + web3.ComputeBudgetProgram.setComputeUnitPrice({ |
| 58 | + // ideally replace this with a dynamic priority_fee based on network conditions. |
| 59 | + microLamports: calculateComputeUnitPrice(20_000, 120_000), |
| 60 | + }) |
| 61 | + ); |
| 62 | + |
| 63 | + const compressInstruction = await CompressedTokenProgram.compress({ |
| 64 | + payer: payer.publicKey, |
| 65 | + owner: payer.publicKey, |
| 66 | + source: sourceTokenAccount.address, // here, the owner of this account is also the payer. |
| 67 | + toAddress: airDropAddresses, |
| 68 | + amount: airDropAddresses.map(() => amount), |
| 69 | + mint: mintAddress, |
| 70 | + outputStateTree: tree, |
| 71 | + }); |
| 72 | + instructions.push(compressInstruction); |
| 73 | + |
| 74 | + // Use zk-compression LUT for your network |
| 75 | + // https://www.zkcompression.com/developers/protocol-addresses-and-urls#lookup-tables |
| 76 | + const lookupTableAddress = new web3.PublicKey( |
| 77 | + // "9NYFyEqPkyXUhkerbGHXUXkvb4qpzeEdHuGpgbgpH1NJ" // mainnet |
| 78 | + "qAJZMgnQJ8G6vA3WRcjD9Jan1wtKkaCFWLWskxJrR5V" // devnet |
| 79 | + ); |
| 80 | + |
| 81 | + // Get the lookup table account state |
| 82 | + const lookupTableAccount = ( |
| 83 | + await connection.getAddressLookupTable(lookupTableAddress) |
| 84 | + ).value!; |
| 85 | + |
| 86 | + // Sign the transaction with the payer and owner keypair |
| 87 | + const owner = payer; |
| 88 | + const additionalSigners = dedupeSigner(payer, [owner]); |
| 89 | + |
| 90 | + const { blockhash } = await connection.getLatestBlockhash(); |
| 91 | + |
| 92 | + const tx = buildAndSignTx( |
| 93 | + instructions, |
| 94 | + payer, |
| 95 | + blockhash, |
| 96 | + additionalSigners, |
| 97 | + [lookupTableAccount] |
| 98 | + ); |
| 99 | + |
| 100 | + const simulate = await connection.simulateTransaction(tx); |
| 101 | + if (simulate.value.err) { |
| 102 | + console.error("Simulation failed", simulate); |
| 103 | + } else { |
| 104 | + console.log("Simulation successful", simulate); |
| 105 | + } |
| 106 | + // Uncomment to send the transaction. |
| 107 | + // const txId = await sendAndConfirmTx(connection, tx); |
| 108 | + // console.log(`txId: ${txId}`); |
| 109 | + } catch (e) { |
| 110 | + console.error(`Compression failed:`, e); |
| 111 | + } |
| 112 | +})(); |
0 commit comments