test.ts 5.9 KB

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