test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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(
  32. [
  33. {
  34. name: 'token_2022_mint_close_authority_program',
  35. programId: PROGRAM_ID,
  36. },
  37. ],
  38. [],
  39. );
  40. const client = context.banksClient;
  41. const payer = context.payer;
  42. test('Create a Token-22 SPL-Token !', async () => {
  43. const mintKeypair: Keypair = Keypair.generate();
  44. const instructionData = new CreateTokenArgs({
  45. token_decimals: 9,
  46. });
  47. const ix = new TransactionInstruction({
  48. keys: [
  49. { pubkey: mintKeypair.publicKey, isSigner: true, isWritable: true }, // Mint account
  50. { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint authority account
  51. { pubkey: payer.publicKey, isSigner: false, isWritable: true }, // Mint close authority account
  52. { pubkey: payer.publicKey, isSigner: true, isWritable: true }, // Transaction Payer
  53. { pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false }, // Rent account
  54. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, // System program
  55. { pubkey: TOKEN_2022_PROGRAM_ID, isSigner: false, isWritable: false }, // Token program
  56. ],
  57. programId: PROGRAM_ID,
  58. data: instructionData.toBuffer(),
  59. });
  60. const blockhash = context.lastBlockhash;
  61. const tx = new Transaction();
  62. tx.recentBlockhash = blockhash;
  63. tx.add(ix).sign(payer, mintKeypair);
  64. const transaction = await client.processTransaction(tx);
  65. assert(transaction.logMessages[0].startsWith(`Program ${PROGRAM_ID}`));
  66. console.log('Token Mint Address: ', mintKeypair.publicKey.toBase58());
  67. });
  68. });