test.ts 5.8 KB

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