test.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { Buffer } from 'node:buffer';
  2. import { PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID } from '@metaplex-foundation/mpl-token-metadata';
  3. import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getAssociatedTokenAddress } from '@solana/spl-token';
  4. import {
  5. Connection,
  6. Keypair,
  7. PublicKey,
  8. SYSVAR_RENT_PUBKEY,
  9. SystemProgram,
  10. Transaction,
  11. TransactionInstruction,
  12. sendAndConfirmTransaction,
  13. } from '@solana/web3.js';
  14. import { CreateTokenArgs, InitArgs, MintToArgs, NftMinterInstruction } from './instructions';
  15. function createKeypairFromFile(path: string): Keypair {
  16. return Keypair.fromSecretKey(Buffer.from(JSON.parse(require('node:fs').readFileSync(path, 'utf-8'))));
  17. }
  18. describe('NFT Minter', async () => {
  19. // const connection = new Connection(`http://localhost:8899`, 'confirmed');
  20. const connection = new Connection('https://api.devnet.solana.com/', 'confirmed');
  21. const payer = createKeypairFromFile(`${require('node:os').homedir()}/.config/solana/id.json`);
  22. const program = createKeypairFromFile('./program/target/deploy/program-keypair.json');
  23. const mintAuthorityPublicKey = PublicKey.findProgramAddressSync([Buffer.from('mint_authority')], program.publicKey)[0];
  24. const mintKeypair: Keypair = Keypair.generate();
  25. it('Init Mint Authority PDA', async () => {
  26. const instructionData = new InitArgs({
  27. instruction: NftMinterInstruction.Init,
  28. });
  29. const ix = new TransactionInstruction({
  30. keys: [
  31. { pubkey: mintAuthorityPublicKey, isSigner: false, isWritable: true },
  32. { pubkey: payer.publicKey, isSigner: false, isWritable: true },
  33. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  34. ],
  35. programId: program.publicKey,
  36. data: instructionData.toBuffer(),
  37. });
  38. const sx = await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer], { skipPreflight: true });
  39. console.log('Success!');
  40. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  41. console.log(` Tx Signature: ${sx}`);
  42. });
  43. it('Create an NFT!', async () => {
  44. const metadataAddress = PublicKey.findProgramAddressSync(
  45. [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mintKeypair.publicKey.toBuffer()],
  46. TOKEN_METADATA_PROGRAM_ID,
  47. )[0];
  48. const instructionData = new CreateTokenArgs({
  49. instruction: NftMinterInstruction.Create,
  50. nft_title: 'Homer NFT',
  51. nft_symbol: 'HOMR',
  52. nft_uri: 'https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json',
  53. });
  54. const ix = new TransactionInstruction({
  55. keys: [
  56. { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true }, // Mint account
  57. { pubkey: mintAuthorityPublicKey, isSigner: false, isWritable: true }, // Mint authority account
  58. { pubkey: metadataAddress, isSigner: false, isWritable: true }, // Metadata account
  59. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Payer
  60. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account
  61. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program
  62. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
  63. {
  64. pubkey: TOKEN_METADATA_PROGRAM_ID,
  65. isSigner: false,
  66. isWritable: false,
  67. }, // Token metadata program
  68. ],
  69. programId: program.publicKey,
  70. data: instructionData.toBuffer(),
  71. });
  72. const sx = await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer, mintKeypair], { skipPreflight: true });
  73. console.log('Success!');
  74. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  75. console.log(` Tx Signature: ${sx}`);
  76. });
  77. it('Mint the NFT to your wallet!', async () => {
  78. const metadataAddress = PublicKey.findProgramAddressSync(
  79. [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mintKeypair.publicKey.toBuffer()],
  80. TOKEN_METADATA_PROGRAM_ID,
  81. )[0];
  82. const editionAddress = PublicKey.findProgramAddressSync(
  83. [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mintKeypair.publicKey.toBuffer(), Buffer.from('edition')],
  84. TOKEN_METADATA_PROGRAM_ID,
  85. )[0];
  86. const associatedTokenAccountAddress = await getAssociatedTokenAddress(mintKeypair.publicKey, payer.publicKey);
  87. const instructionData = new MintToArgs({
  88. instruction: NftMinterInstruction.Mint,
  89. });
  90. const ix = new TransactionInstruction({
  91. keys: [
  92. { pubkey: mintKeypair.publicKey, isSigner: false, isWritable: true }, // Mint account
  93. { pubkey: metadataAddress, isSigner: false, isWritable: true }, // Metadata account
  94. { pubkey: editionAddress, isSigner: false, isWritable: true }, // Edition account
  95. { pubkey: mintAuthorityPublicKey, isSigner: false, isWritable: true }, // Mint authority account
  96. {
  97. pubkey: associatedTokenAccountAddress,
  98. isSigner: false,
  99. isWritable: true,
  100. }, // ATA
  101. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Payer
  102. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account
  103. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program
  104. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
  105. {
  106. pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
  107. isSigner: false,
  108. isWritable: false,
  109. }, // Associated token program
  110. {
  111. pubkey: TOKEN_METADATA_PROGRAM_ID,
  112. isSigner: false,
  113. isWritable: false,
  114. }, // Token metadata program
  115. ],
  116. programId: program.publicKey,
  117. data: instructionData.toBuffer(),
  118. });
  119. const sx = await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer]);
  120. console.log('Success!');
  121. console.log(` ATA Address: ${associatedTokenAccountAddress}`);
  122. console.log(` Tx Signature: ${sx}`);
  123. });
  124. });