test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { Buffer } from 'node:buffer';
  2. import { describe, test } from 'node:test';
  3. import { TOKEN_2022_PROGRAM_ID } from '@solana/spl-token';
  4. import { Keypair, PublicKey, SYSVAR_RENT_PUBKEY, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js';
  5. import * as borsh from 'borsh';
  6. import { assert } from 'chai';
  7. import { start } from 'solana-bankrun';
  8. class Assignable {
  9. constructor(properties) {
  10. for (const [key, value] of Object.entries(properties)) {
  11. this[key] = value;
  12. }
  13. }
  14. }
  15. class CreateTokenArgs extends Assignable {
  16. toBuffer() {
  17. return Buffer.from(borsh.serialize(CreateTokenArgsSchema, this));
  18. }
  19. }
  20. const CreateTokenArgsSchema = new Map([
  21. [
  22. CreateTokenArgs,
  23. {
  24. kind: 'struct',
  25. fields: [['token_decimals', 'u8']],
  26. },
  27. ],
  28. ]);
  29. describe('Create Token', async () => {
  30. const PROGRAM_ID = PublicKey.unique();
  31. const context = await start([{ name: 'token_2022_multiple_extensions_program', programId: PROGRAM_ID }], []);
  32. const client = context.banksClient;
  33. const payer = context.payer;
  34. test('Create a Token-22 SPL-Token !', async () => {
  35. const blockhash = context.lastBlockhash;
  36. const mintKeypair: Keypair = Keypair.generate();
  37. const instructionData = new CreateTokenArgs({
  38. token_decimals: 9,
  39. });
  40. const ix = new TransactionInstruction({
  41. keys: [
  42. { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true }, // Mint account
  43. { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint authority account
  44. { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint close authority account
  45. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Transaction Payer
  46. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account
  47. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program
  48. { pubkey: TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
  49. ],
  50. programId: PROGRAM_ID,
  51. data: instructionData.toBuffer(),
  52. });
  53. const tx = new Transaction();
  54. tx.recentBlockhash = blockhash;
  55. tx.add(ix).sign(payer, mintKeypair);
  56. const transaction = await client.processTransaction(tx);
  57. assert(transaction.logMessages[0].startsWith(`Program ${PROGRAM_ID}`));
  58. console.log('Token Mint Address: ', mintKeypair.publicKey.toBase58());
  59. });
  60. });