test.ts 2.4 KB

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