test.ts 6.9 KB

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