test.ts 5.2 KB

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