bankrun.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 { start } from 'solana-bankrun';
  7. import { CreateTokenArgs } from './instructions';
  8. describe('Create Tokens!', async () => {
  9. const PROGRAM_ID = new PublicKey('z7msBPQHDJjTvdQRoEcKyENgXDhSRYeHieN1ZMTqo35');
  10. const context = await start(
  11. [
  12. { name: 'steel_program', programId: PROGRAM_ID },
  13. { name: 'token_metadata', programId: TOKEN_METADATA_PROGRAM_ID },
  14. ],
  15. [],
  16. );
  17. const client = context.banksClient;
  18. const payer = context.payer;
  19. const tokenMintKeypair: Keypair = Keypair.generate();
  20. const nftMintKeypair: Keypair = Keypair.generate();
  21. test('Create an SPL Token!', async () => {
  22. const metadataPDA = PublicKey.findProgramAddressSync(
  23. [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), tokenMintKeypair.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. 9,
  33. );
  34. const createTokenIx = new TransactionInstruction({
  35. keys: [
  36. { pubkey: payer.publicKey, isSigner: true, isWritable: true },
  37. {
  38. pubkey: tokenMintKeypair.publicKey,
  39. isSigner: true,
  40. isWritable: true,
  41. },
  42. { pubkey: metadataPDA, isSigner: false, isWritable: true },
  43. {
  44. pubkey: SystemProgram.programId,
  45. isSigner: false,
  46. isWritable: false,
  47. },
  48. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
  49. {
  50. pubkey: TOKEN_METADATA_PROGRAM_ID,
  51. isSigner: false,
  52. isWritable: false,
  53. },
  54. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
  55. ],
  56. programId: PROGRAM_ID,
  57. data: createArgs.toBuffer(),
  58. });
  59. const tx = new Transaction();
  60. const [blockhash, _] = await client.getLatestBlockhash();
  61. tx.recentBlockhash = blockhash;
  62. tx.add(createTokenIx).sign(payer, tokenMintKeypair);
  63. await client.processTransaction(tx);
  64. console.log('Success!');
  65. console.log(`Mint Address: ${tokenMintKeypair.publicKey}`);
  66. });
  67. test('Create an NFT!', async () => {
  68. const metadataPDA = PublicKey.findProgramAddressSync(
  69. [Buffer.from('metadata'), TOKEN_METADATA_PROGRAM_ID.toBuffer(), nftMintKeypair.publicKey.toBuffer()],
  70. TOKEN_METADATA_PROGRAM_ID,
  71. )[0];
  72. // NFT default = 0 decimals
  73. //
  74. const createArgs = new CreateTokenArgs(
  75. 'Homer NFT',
  76. 'HOMR',
  77. 'https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json',
  78. 0,
  79. );
  80. const createTokenIx = new TransactionInstruction({
  81. keys: [
  82. { pubkey: payer.publicKey, isSigner: true, isWritable: true },
  83. { pubkey: nftMintKeypair.publicKey, isSigner: true, isWritable: true },
  84. { pubkey: metadataPDA, isSigner: false, isWritable: true },
  85. {
  86. pubkey: SystemProgram.programId,
  87. isSigner: false,
  88. isWritable: false,
  89. },
  90. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
  91. {
  92. pubkey: TOKEN_METADATA_PROGRAM_ID,
  93. isSigner: false,
  94. isWritable: false,
  95. },
  96. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
  97. ],
  98. programId: PROGRAM_ID,
  99. data: createArgs.toBuffer(),
  100. });
  101. const tx = new Transaction();
  102. const [blockhash, _] = await client.getLatestBlockhash();
  103. tx.recentBlockhash = blockhash;
  104. tx.add(createTokenIx).sign(payer, nftMintKeypair);
  105. await client.processTransaction(tx);
  106. console.log('Success!');
  107. console.log(`Mint Address: ${nftMintKeypair.publicKey}`);
  108. });
  109. });