test.ts 5.0 KB

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