test.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { describe, test } from 'node:test';
  2. import { Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js';
  3. import { start } from 'solana-bankrun';
  4. describe('Create a system account', async () => {
  5. const PROGRAM_ID = PublicKey.unique();
  6. const context = await start([{ name: 'create_account_program', programId: PROGRAM_ID }], []);
  7. const client = context.banksClient;
  8. const payer = context.payer;
  9. test('Create the account via a cross program invocation', async () => {
  10. const newKeypair = Keypair.generate();
  11. const blockhash = context.lastBlockhash;
  12. const ix = new TransactionInstruction({
  13. keys: [
  14. { pubkey: payer.publicKey, isSigner: true, isWritable: true },
  15. { pubkey: newKeypair.publicKey, isSigner: true, isWritable: true },
  16. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  17. ],
  18. programId: PROGRAM_ID,
  19. data: Buffer.alloc(0),
  20. });
  21. const tx = new Transaction();
  22. tx.recentBlockhash = blockhash;
  23. tx.add(ix).sign(payer, newKeypair);
  24. await client.processTransaction(tx);
  25. });
  26. test('Create the account via direct call to system program', async () => {
  27. const newKeypair = Keypair.generate();
  28. const blockhash = context.lastBlockhash;
  29. const ix = SystemProgram.createAccount({
  30. fromPubkey: payer.publicKey,
  31. newAccountPubkey: newKeypair.publicKey,
  32. lamports: LAMPORTS_PER_SOL,
  33. space: 0,
  34. programId: SystemProgram.programId,
  35. });
  36. const tx = new Transaction();
  37. tx.recentBlockhash = blockhash;
  38. tx.add(ix).sign(payer, newKeypair);
  39. await client.processTransaction(tx);
  40. console.log(`Account with public key ${newKeypair.publicKey} successfully created`);
  41. });
  42. });