test.ts 4.4 KB

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