test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { Buffer } from 'node:buffer';
  2. import { PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID } from '@metaplex-foundation/mpl-token-metadata';
  3. import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getAssociatedTokenAddress } 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 { BN } from 'bn.js';
  15. import { CreateTokenArgs, MintToArgs, SplMinterInstruction } from './instructions';
  16. function createKeypairFromFile(path: string): Keypair {
  17. return Keypair.fromSecretKey(Buffer.from(JSON.parse(require('node:fs').readFileSync(path, 'utf-8'))));
  18. }
  19. describe('SPL Token Minter', async () => {
  20. // const connection = new Connection(`http://localhost:8899`, 'confirmed');
  21. const connection = new Connection('https://api.devnet.solana.com/', 'confirmed');
  22. const payer = createKeypairFromFile(`${require('node:os').homedir()}/.config/solana/id.json`);
  23. const program = createKeypairFromFile('./program/target/deploy/program-keypair.json');
  24. const mintKeypair: Keypair = Keypair.generate();
  25. it('Create an SPL Token!', async () => {
  26. const metadataAddress = PublicKey.findProgramAddressSync(
  27. [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mintKeypair.publicKey.toBuffer()],
  28. TOKEN_METADATA_PROGRAM_ID,
  29. )[0];
  30. const instructionData = new CreateTokenArgs({
  31. instruction: SplMinterInstruction.Create,
  32. token_title: 'Solana Gold',
  33. token_symbol: 'GOLDSOL',
  34. token_uri: 'https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json',
  35. });
  36. const ix = new TransactionInstruction({
  37. keys: [
  38. { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true }, // Mint account
  39. { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint authority account
  40. { pubkey: metadataAddress, isSigner: false, isWritable: true }, // Metadata account
  41. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Payer
  42. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account
  43. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program
  44. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
  45. {
  46. pubkey: TOKEN_METADATA_PROGRAM_ID,
  47. isSigner: false,
  48. isWritable: false,
  49. }, // Token metadata program
  50. ],
  51. programId: program.publicKey,
  52. data: instructionData.toBuffer(),
  53. });
  54. const sx = await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer, mintKeypair]);
  55. console.log('Success!');
  56. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  57. console.log(` Tx Signature: ${sx}`);
  58. });
  59. it('Mint some tokens to your wallet!', async () => {
  60. const associatedTokenAccountAddress = await getAssociatedTokenAddress(mintKeypair.publicKey, payer.publicKey);
  61. const instructionData = new MintToArgs({
  62. instruction: SplMinterInstruction.Mint,
  63. quantity: new BN(150),
  64. });
  65. const ix = new TransactionInstruction({
  66. keys: [
  67. { pubkey: mintKeypair.publicKey, isSigner: false, isWritable: true }, // Mint account
  68. { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint authority account
  69. {
  70. pubkey: associatedTokenAccountAddress,
  71. isSigner: false,
  72. isWritable: true,
  73. }, // ATA
  74. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Payer
  75. { pubkey: SystemProgram.programId, isSigner: false, isWritable: true }, // System program
  76. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
  77. {
  78. pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
  79. isSigner: false,
  80. isWritable: false,
  81. }, // Token metadata program
  82. ],
  83. programId: program.publicKey,
  84. data: instructionData.toBuffer(),
  85. });
  86. const sx = await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer]);
  87. console.log('Success!');
  88. console.log(` ATA Address: ${associatedTokenAccountAddress}`);
  89. console.log(` Tx Signature: ${sx}`);
  90. });
  91. });