permanent-delegate.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { PermanentDelegate } from "../target/types/permanent_delegate";
  4. import {
  5. TOKEN_2022_PROGRAM_ID,
  6. burnChecked,
  7. createAccount,
  8. getAccount,
  9. mintTo,
  10. } from "@solana/spl-token";
  11. describe("permanent-delegate", () => {
  12. const provider = anchor.AnchorProvider.env();
  13. const connection = provider.connection;
  14. const wallet = provider.wallet as anchor.Wallet;
  15. anchor.setProvider(provider);
  16. const program = anchor.workspace
  17. .PermanentDelegate as Program<PermanentDelegate>;
  18. const mintKeypair = new anchor.web3.Keypair();
  19. it("Create Mint with Permanent Delegate", async () => {
  20. const transactionSignature = await program.methods
  21. .initialize()
  22. .accounts({ mintAccount: mintKeypair.publicKey })
  23. .signers([mintKeypair])
  24. .rpc({ skipPreflight: true });
  25. console.log("Your transaction signature", transactionSignature);
  26. });
  27. it("Create Token Account, Mint Tokens, and burn with Permanent Delegate", async () => {
  28. const amount = 100;
  29. // Random keypair to use as owner of Token Account
  30. const randomKeypair = new anchor.web3.Keypair();
  31. // Create Token Account owned by random keypair
  32. const sourceTokenAccount = await createAccount(
  33. connection,
  34. wallet.payer, // Payer to create Token Account
  35. mintKeypair.publicKey, // Mint Account address
  36. randomKeypair.publicKey, // Token Account owner
  37. undefined, // Optional keypair, default to Associated Token Account
  38. undefined, // Confirmation options
  39. TOKEN_2022_PROGRAM_ID // Token Extension Program ID
  40. );
  41. // Mint tokens to sourceTokenAccount
  42. await mintTo(
  43. connection,
  44. wallet.payer, // Transaction fee payer
  45. mintKeypair.publicKey, // Mint Account address
  46. sourceTokenAccount, // Mint to
  47. wallet.publicKey, // Mint Authority address
  48. amount, // Amount
  49. undefined, // Additional signers
  50. undefined, // Confirmation options
  51. TOKEN_2022_PROGRAM_ID // Token Extension Program ID
  52. );
  53. // Burn tokens from sourceTokenAccount, using Permanent Delegate
  54. // The permanent delegate can burn / transfer tokens from all token account for the mint account
  55. const transactionSignature = await burnChecked(
  56. connection,
  57. wallet.payer, // Transaction fee payer
  58. sourceTokenAccount, // Tranfer from
  59. mintKeypair.publicKey, // Mint Account address
  60. wallet.publicKey, // Use Permanent Delegate as owner
  61. amount, // Amount
  62. 2, // Mint Account decimals
  63. undefined, // Additional signers
  64. undefined, // Confirmation options
  65. TOKEN_2022_PROGRAM_ID // Token Extension Program ID
  66. );
  67. console.log("Your transaction signature", transactionSignature);
  68. const tokenAccount = await getAccount(
  69. connection,
  70. sourceTokenAccount,
  71. null,
  72. TOKEN_2022_PROGRAM_ID
  73. );
  74. console.log("Token Account Balance:", Number(tokenAccount.amount));
  75. });
  76. });