test.ts 1.8 KB

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