permanent-delegate.ts 2.9 KB

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