bankrun.test.ts 4.2 KB

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