test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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, 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 mintKeypair: Keypair = Keypair.generate();
  24. it('Create an NFT!', async () => {
  25. const metadataAddress = PublicKey.findProgramAddressSync(
  26. [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mintKeypair.publicKey.toBuffer()],
  27. TOKEN_METADATA_PROGRAM_ID,
  28. )[0];
  29. const instructionData = new CreateTokenArgs({
  30. instruction: NftMinterInstruction.Create,
  31. nft_title: 'Homer NFT',
  32. nft_symbol: 'HOMR',
  33. nft_uri: 'https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json',
  34. });
  35. const ix = new TransactionInstruction({
  36. keys: [
  37. { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true }, // Mint account
  38. { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint authority account
  39. { pubkey: metadataAddress, isSigner: false, isWritable: true }, // Metadata account
  40. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Payer
  41. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account
  42. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program
  43. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
  44. {
  45. pubkey: TOKEN_METADATA_PROGRAM_ID,
  46. isSigner: false,
  47. isWritable: false,
  48. }, // Token metadata program
  49. ],
  50. programId: program.publicKey,
  51. data: instructionData.toBuffer(),
  52. });
  53. const sx = await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer, mintKeypair]);
  54. console.log('Success!');
  55. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  56. console.log(` Tx Signature: ${sx}`);
  57. });
  58. it('Mint the NFT to your wallet!', async () => {
  59. const metadataAddress = PublicKey.findProgramAddressSync(
  60. [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mintKeypair.publicKey.toBuffer()],
  61. TOKEN_METADATA_PROGRAM_ID,
  62. )[0];
  63. const editionAddress = PublicKey.findProgramAddressSync(
  64. [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mintKeypair.publicKey.toBuffer(), Buffer.from('edition')],
  65. TOKEN_METADATA_PROGRAM_ID,
  66. )[0];
  67. const associatedTokenAccountAddress = await getAssociatedTokenAddress(mintKeypair.publicKey, payer.publicKey);
  68. const instructionData = new MintToArgs({
  69. instruction: NftMinterInstruction.Mint,
  70. });
  71. const ix = new TransactionInstruction({
  72. keys: [
  73. { pubkey: mintKeypair.publicKey, isSigner: false, isWritable: true }, // Mint account
  74. { pubkey: metadataAddress, isSigner: false, isWritable: true }, // Metadata account
  75. { pubkey: editionAddress, isSigner: false, isWritable: true }, // Edition account
  76. { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint authority account
  77. {
  78. pubkey: associatedTokenAccountAddress,
  79. isSigner: false,
  80. isWritable: true,
  81. }, // ATA
  82. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Payer
  83. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account
  84. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program
  85. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
  86. {
  87. pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
  88. isSigner: false,
  89. isWritable: false,
  90. }, // Associated token program
  91. {
  92. pubkey: TOKEN_METADATA_PROGRAM_ID,
  93. isSigner: false,
  94. isWritable: false,
  95. }, // Token metadata program
  96. ],
  97. programId: program.publicKey,
  98. data: instructionData.toBuffer(),
  99. });
  100. const sx = await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer]);
  101. console.log('Success!');
  102. console.log(` ATA Address: ${associatedTokenAccountAddress}`);
  103. console.log(` Tx Signature: ${sx}`);
  104. });
  105. });