test.ts 4.0 KB

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