|
| 1 | +import "dotenv/config"; |
| 2 | +import { Keypair, sendAndConfirmTransaction, Transaction } from "@solana/web3.js"; |
| 3 | +import { createRpc } from "@lightprotocol/stateless.js"; |
| 4 | +import { |
| 5 | + createMintInterface, |
| 6 | + createAtaInterface, |
| 7 | + mintToInterface, |
| 8 | + getAssociatedTokenAddressInterface, |
| 9 | + createApproveInterfaceInstructions, |
| 10 | + createRevokeInterfaceInstructions, |
| 11 | +} from "@lightprotocol/compressed-token"; |
| 12 | +import { homedir } from "os"; |
| 13 | +import { readFileSync } from "fs"; |
| 14 | + |
| 15 | +// devnet: |
| 16 | +// const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`; |
| 17 | +// const rpc = createRpc(RPC_URL); |
| 18 | +// localnet: |
| 19 | +const rpc = createRpc(); |
| 20 | + |
| 21 | +const payer = Keypair.fromSecretKey( |
| 22 | + new Uint8Array( |
| 23 | + JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8")) |
| 24 | + ) |
| 25 | +); |
| 26 | + |
| 27 | +(async function () { |
| 28 | + const { mint } = await createMintInterface(rpc, payer, payer, null, 9); |
| 29 | + |
| 30 | + const owner = Keypair.generate(); |
| 31 | + await createAtaInterface(rpc, payer, mint, owner.publicKey); |
| 32 | + const ownerAta = getAssociatedTokenAddressInterface(mint, owner.publicKey); |
| 33 | + await mintToInterface(rpc, payer, mint, ownerAta, payer, 1_000_000_000); |
| 34 | + |
| 35 | + // Approve a delegate first (so we have something to revoke) |
| 36 | + const delegate = Keypair.generate(); |
| 37 | + const approveBatches = await createApproveInterfaceInstructions( |
| 38 | + rpc, |
| 39 | + payer.publicKey, |
| 40 | + mint, |
| 41 | + ownerAta, |
| 42 | + delegate.publicKey, |
| 43 | + 500_000_000, |
| 44 | + owner.publicKey, |
| 45 | + 9 |
| 46 | + ); |
| 47 | + for (const batch of approveBatches) { |
| 48 | + const tx = new Transaction().add(...batch); |
| 49 | + await sendAndConfirmTransaction(rpc, tx, [payer, owner]); |
| 50 | + } |
| 51 | + console.log("Approved delegate:", delegate.publicKey.toBase58()); |
| 52 | + |
| 53 | + // Build revoke instruction batches. |
| 54 | + // Returns TransactionInstruction[][] — send [0..n-2] first (load batches), |
| 55 | + // then send [n-1] (the revoke instruction). |
| 56 | + const revokeBatches = await createRevokeInterfaceInstructions( |
| 57 | + rpc, |
| 58 | + payer.publicKey, |
| 59 | + mint, |
| 60 | + ownerAta, |
| 61 | + owner.publicKey, |
| 62 | + 9 |
| 63 | + ); |
| 64 | + |
| 65 | + for (let i = 0; i < revokeBatches.length - 1; i++) { |
| 66 | + const tx = new Transaction().add(...revokeBatches[i]); |
| 67 | + await sendAndConfirmTransaction(rpc, tx, [payer, owner]); |
| 68 | + } |
| 69 | + |
| 70 | + const revokeTx = new Transaction().add( |
| 71 | + ...revokeBatches[revokeBatches.length - 1] |
| 72 | + ); |
| 73 | + const signature = await sendAndConfirmTransaction(rpc, revokeTx, [ |
| 74 | + payer, |
| 75 | + owner, |
| 76 | + ]); |
| 77 | + |
| 78 | + console.log("Revoked all delegate permissions"); |
| 79 | + console.log("Tx:", signature); |
| 80 | +})(); |
0 commit comments