tests.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { Buffer } from 'node:buffer';
  2. import { describe, test } from 'node:test';
  3. import { PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID } from '@metaplex-foundation/mpl-token-metadata';
  4. import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getAssociatedTokenAddressSync } from '@solana/spl-token';
  5. import { Keypair, PublicKey, SYSVAR_RENT_PUBKEY, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js';
  6. import { start } from 'solana-bankrun';
  7. import { CreateTokenArgs, InitArgs, MintToArgs } from './instructions';
  8. describe('PDA MINT AUTHORITY', async () => {
  9. const PROGRAM_ID = new PublicKey('z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35');
  10. const context = await start(
  11. [
  12. { name: 'pda_mint_authority_program', programId: PROGRAM_ID },
  13. { name: 'token_metadata', programId: TOKEN_METADATA_PROGRAM_ID },
  14. ],
  15. [],
  16. );
  17. const client = context.banksClient;
  18. const payer = context.payer;
  19. const mintKeypair: Keypair = Keypair.generate();
  20. const mintAuthorityPublicKey = PublicKey.findProgramAddressSync([Buffer.from('mint_authority')], PROGRAM_ID)[0];
  21. test('Init mint authority PDA!', async () => {
  22. const metadataPDA = PublicKey.findProgramAddressSync(
  23. [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mintKeypair.publicKey.toBuffer()],
  24. TOKEN_METADATA_PROGRAM_ID,
  25. )[0];
  26. const initArgs = new InitArgs();
  27. const initIx = new TransactionInstruction({
  28. keys: [
  29. { pubkey: mintAuthorityPublicKey, isSigner: false, isWritable: true },
  30. { pubkey: payer.publicKey, isSigner: false, isWritable: true },
  31. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  32. ],
  33. programId: PROGRAM_ID,
  34. data: initArgs.toBuffer(),
  35. });
  36. const tx = new Transaction();
  37. const [blockhash, _] = await client.getLatestBlockhash();
  38. tx.recentBlockhash = blockhash;
  39. tx.add(initIx).sign(payer);
  40. await client.processTransaction(tx);
  41. console.log('Success!');
  42. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  43. });
  44. test('Create a SPL Token with PDA!', async () => {
  45. const metadataPDA = PublicKey.findProgramAddressSync(
  46. [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mintKeypair.publicKey.toBuffer()],
  47. TOKEN_METADATA_PROGRAM_ID,
  48. )[0];
  49. // SPL Token default = 9 decimals
  50. //
  51. const createArgs = new CreateTokenArgs(
  52. 'Solana Gold',
  53. 'GOLDSOL',
  54. 'https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json',
  55. );
  56. const createTokenIx = new TransactionInstruction({
  57. keys: [
  58. { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true },
  59. { pubkey: mintAuthorityPublicKey, isSigner: false, isWritable: true },
  60. { pubkey: metadataPDA, isSigner: false, isWritable: true },
  61. { pubkey: payer.publicKey, isSigner: true, isWritable: true },
  62. {
  63. pubkey: SystemProgram.programId,
  64. isSigner: false,
  65. isWritable: false,
  66. },
  67. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
  68. {
  69. pubkey: TOKEN_METADATA_PROGRAM_ID,
  70. isSigner: false,
  71. isWritable: false,
  72. },
  73. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
  74. ],
  75. programId: PROGRAM_ID,
  76. data: createArgs.toBuffer(),
  77. });
  78. const tx = new Transaction();
  79. const [blockhash, _] = await client.getLatestBlockhash();
  80. tx.recentBlockhash = blockhash;
  81. tx.add(createTokenIx).sign(payer, mintKeypair);
  82. await client.processTransaction(tx);
  83. console.log('Success!');
  84. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  85. });
  86. test('Mint some tokens to your wallet with PDA!', async () => {
  87. const associatedTokenAccountAddress = getAssociatedTokenAddressSync(mintKeypair.publicKey, payer.publicKey);
  88. const mintArgs = new MintToArgs(100);
  89. const mintToIx = new TransactionInstruction({
  90. keys: [
  91. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // payer
  92. { pubkey: mintKeypair.publicKey, isSigner: false, isWritable: true }, // mint_pda must be writable
  93. {
  94. pubkey: associatedTokenAccountAddress,
  95. isSigner: false,
  96. isWritable: true,
  97. }, // ATA
  98. { pubkey: mintAuthorityPublicKey, isSigner: false, isWritable: true },
  99. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // spl_token::ID
  100. {
  101. pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
  102. isSigner: false,
  103. isWritable: false,
  104. }, // spl_associated_token_account::ID
  105. {
  106. pubkey: SystemProgram.programId,
  107. isSigner: false,
  108. isWritable: false,
  109. }, // system_program::ID
  110. ],
  111. programId: PROGRAM_ID,
  112. data: mintArgs.toBuffer(),
  113. });
  114. const tx = new Transaction();
  115. const [blockhash, _] = await client.getLatestBlockhash();
  116. tx.recentBlockhash = blockhash;
  117. tx.add(mintToIx).sign(payer);
  118. await client.processTransaction(tx);
  119. console.log('Success!');
  120. console.log(` ATA Address: ${associatedTokenAccountAddress}`);
  121. });
  122. });