bankrun.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { Buffer } from 'node:buffer';
  2. import { describe, test } from 'node:test';
  3. import { PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID } from '@metaplex-foundation/mpl-token-metadata';
  4. import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, getAssociatedTokenAddressSync } from '@solana/spl-token';
  5. import { Keypair, PublicKey, SYSVAR_RENT_PUBKEY, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js';
  6. import { BN } from 'bn.js';
  7. import { start } from 'solana-bankrun';
  8. import { CreateTokenArgs, MintToArgs, SplMinterInstruction } from './instructions';
  9. describe('SPL Token Minter!', async () => {
  10. const PROGRAM_ID = new PublicKey('8V26fyhrQobKbvkRCV3KvT6jZQLzviovdARfGrw8kUdG');
  11. const context = await start(
  12. [
  13. { name: 'spl_token_minter_program', programId: PROGRAM_ID },
  14. { name: 'token_metadata', programId: TOKEN_METADATA_PROGRAM_ID },
  15. ],
  16. [],
  17. );
  18. const client = context.banksClient;
  19. const payer = context.payer;
  20. const mintKeypair: Keypair = Keypair.generate();
  21. test('Create an SPL Token!', async () => {
  22. const metadataPDA = PublicKey.findProgramAddressSync(
  23. [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), mintKeypair.publicKey.toBuffer()],
  24. TOKEN_METADATA_PROGRAM_ID,
  25. )[0];
  26. // SPL Token default = 9 decimals
  27. //
  28. const createArgs = new CreateTokenArgs(
  29. 'Solana Gold',
  30. 'GOLDSOL',
  31. 'https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json',
  32. );
  33. const createTokenIx = new TransactionInstruction({
  34. keys: [
  35. { pubkey: payer.publicKey, isSigner: true, isWritable: true },
  36. { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true },
  37. { pubkey: metadataPDA, isSigner: false, isWritable: true },
  38. {
  39. pubkey: SystemProgram.programId,
  40. isSigner: false,
  41. isWritable: false,
  42. },
  43. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
  44. {
  45. pubkey: TOKEN_METADATA_PROGRAM_ID,
  46. isSigner: false,
  47. isWritable: false,
  48. },
  49. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
  50. ],
  51. programId: PROGRAM_ID,
  52. data: createArgs.toBuffer(),
  53. });
  54. const tx = new Transaction();
  55. const [blockhash, _] = await client.getLatestBlockhash();
  56. tx.recentBlockhash = blockhash;
  57. tx.add(createTokenIx).sign(payer, mintKeypair);
  58. await client.processTransaction(tx);
  59. console.log('Success!');
  60. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  61. });
  62. test('Mint some tokens to your wallet!', async () => {
  63. const recipientATA = getAssociatedTokenAddressSync(mintKeypair.publicKey, payer.publicKey);
  64. const mintArgs = new MintToArgs(100);
  65. const mintToIx = new TransactionInstruction({
  66. keys: [
  67. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // mint_authority
  68. { pubkey: payer.publicKey, isSigner: false, isWritable: false }, // recipient
  69. { pubkey: mintKeypair.publicKey, isSigner: false, isWritable: true }, // mint_pda must be writable
  70. { pubkey: recipientATA, isSigner: false, isWritable: true }, // associated_token_account must be writable
  71. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false }, // spl_token::ID
  72. {
  73. pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
  74. isSigner: false,
  75. isWritable: false,
  76. }, // spl_associated_token_account::ID
  77. {
  78. pubkey: SystemProgram.programId,
  79. isSigner: false,
  80. isWritable: false,
  81. }, // system_program::ID
  82. ],
  83. programId: PROGRAM_ID,
  84. data: mintArgs.toBuffer(),
  85. });
  86. const tx = new Transaction();
  87. const [blockhash, _] = await client.getLatestBlockhash();
  88. tx.recentBlockhash = blockhash;
  89. tx.add(mintToIx).sign(payer);
  90. await client.processTransaction(tx);
  91. console.log('Success!');
  92. console.log(` ATA Address: ${recipientATA}`);
  93. });
  94. });