test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_non_transferable_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: true, isWritable: true }, // Transaction Payer
  45. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account
  46. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program
  47. { pubkey: TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
  48. ],
  49. programId: PROGRAM_ID,
  50. data: instructionData.toBuffer(),
  51. });
  52. const tx = new Transaction();
  53. tx.recentBlockhash = blockhash;
  54. tx.add(ix).sign(payer, mintKeypair);
  55. const transaction = await client.processTransaction(tx);
  56. assert(transaction.logMessages[0].startsWith(`Program ${PROGRAM_ID}`));
  57. console.log('Token Mint Address: ', mintKeypair.publicKey.toBase58());
  58. });
  59. });