|
| 1 | +import "dotenv/config"; |
| 2 | +import { Keypair, PublicKey, ComputeBudgetProgram } from "@solana/web3.js"; |
| 3 | +import { |
| 4 | + createRpc, |
| 5 | + CTOKEN_PROGRAM_ID, |
| 6 | + buildAndSignTx, |
| 7 | + sendAndConfirmTx, |
| 8 | +} from "@lightprotocol/stateless.js"; |
| 9 | +import { |
| 10 | + createMintInterface, |
| 11 | + createAssociatedTokenAccountInterfaceInstruction, |
| 12 | + getAssociatedTokenAddressInterface, |
| 13 | +} from "@lightprotocol/compressed-token"; |
| 14 | +import { homedir } from "os"; |
| 15 | +import { readFileSync } from "fs"; |
| 16 | + |
| 17 | +const LIGHT_TOKEN_CONFIG = new PublicKey( |
| 18 | + "ACXg8a7VaqecBWrSbdu73W4Pg9gsqXJ3EXAqkHyhvVXg", |
| 19 | +); |
| 20 | +const LIGHT_TOKEN_RENT_SPONSOR = new PublicKey( |
| 21 | + "r18WwUxfG8kQ69bQPAB2jV6zGNKy3GosFGctjQoV4ti", |
| 22 | +); |
| 23 | + |
| 24 | +const DEFAULT_COMPRESSIBLE_CONFIG = { |
| 25 | + tokenAccountVersion: 3, |
| 26 | + rentPayment: 16, |
| 27 | + compressionOnly: 1, |
| 28 | + writeTopUp: 766, |
| 29 | + compressToAccountPubkey: null, |
| 30 | +}; |
| 31 | + |
| 32 | +// devnet: |
| 33 | +// const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`; |
| 34 | +// const rpc = createRpc(RPC_URL); |
| 35 | +// localnet: |
| 36 | +const rpc = createRpc(); |
| 37 | + |
| 38 | +const payer = Keypair.fromSecretKey( |
| 39 | + new Uint8Array( |
| 40 | + JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8")), |
| 41 | + ), |
| 42 | +); |
| 43 | + |
| 44 | +(async function () { |
| 45 | + const { mint } = await createMintInterface(rpc, payer, payer, null, 9); |
| 46 | + console.log("Mint:", mint.toBase58()); |
| 47 | + |
| 48 | + const owner = Keypair.generate(); |
| 49 | + const associatedToken = getAssociatedTokenAddressInterface( |
| 50 | + mint, |
| 51 | + owner.publicKey, |
| 52 | + ); |
| 53 | + |
| 54 | + const ix = createAssociatedTokenAccountInterfaceInstruction( |
| 55 | + payer.publicKey, |
| 56 | + associatedToken, |
| 57 | + owner.publicKey, |
| 58 | + mint, |
| 59 | + CTOKEN_PROGRAM_ID, |
| 60 | + CTOKEN_PROGRAM_ID, |
| 61 | + { |
| 62 | + compressibleConfig: DEFAULT_COMPRESSIBLE_CONFIG, |
| 63 | + configAccount: LIGHT_TOKEN_CONFIG, |
| 64 | + rentPayerPda: LIGHT_TOKEN_RENT_SPONSOR, |
| 65 | + }, |
| 66 | + ); |
| 67 | + |
| 68 | + const { blockhash } = await rpc.getLatestBlockhash(); |
| 69 | + const tx = buildAndSignTx( |
| 70 | + [ComputeBudgetProgram.setComputeUnitLimit({ units: 50_000 }), ix], |
| 71 | + payer, |
| 72 | + blockhash, |
| 73 | + [], |
| 74 | + ); |
| 75 | + const signature = await sendAndConfirmTx(rpc, tx); |
| 76 | + |
| 77 | + console.log("ATA:", associatedToken.toBase58()); |
| 78 | + console.log("Tx:", signature); |
| 79 | +})(); |
0 commit comments