test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { describe, test } from 'node:test';
  2. import { Keypair, PublicKey, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js';
  3. import { start } from 'solana-bankrun';
  4. describe('Checking accounts', async () => {
  5. const PROGRAM_ID = PublicKey.unique();
  6. const context = await start([{ name: 'checking_accounts_program', programId: PROGRAM_ID }], []);
  7. const client = context.banksClient;
  8. const payer = context.payer;
  9. const rent = await client.getRent();
  10. // We'll create this ahead of time.
  11. // Our program will try to modify it.
  12. const accountToChange = Keypair.generate();
  13. // Our program will create this.
  14. const accountToCreate = Keypair.generate();
  15. test('Create an account owned by our program', async () => {
  16. const blockhash = context.lastBlockhash;
  17. const ix = SystemProgram.createAccount({
  18. fromPubkey: payer.publicKey,
  19. newAccountPubkey: accountToChange.publicKey,
  20. lamports: Number(rent.minimumBalance(BigInt(0))),
  21. space: 0,
  22. programId: PROGRAM_ID, // Our program
  23. });
  24. const tx = new Transaction();
  25. tx.recentBlockhash = blockhash;
  26. tx.add(ix).sign(payer, accountToChange);
  27. await client.processTransaction(tx);
  28. });
  29. test('Check accounts', async () => {
  30. const blockhash = context.lastBlockhash;
  31. const ix = new TransactionInstruction({
  32. keys: [
  33. { pubkey: payer.publicKey, isSigner: true, isWritable: true },
  34. { pubkey: accountToCreate.publicKey, isSigner: true, isWritable: true },
  35. { pubkey: accountToChange.publicKey, isSigner: true, isWritable: true },
  36. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  37. ],
  38. programId: PROGRAM_ID,
  39. data: Buffer.alloc(0),
  40. });
  41. const tx = new Transaction();
  42. tx.recentBlockhash = blockhash;
  43. tx.add(ix).sign(payer, accountToChange, accountToCreate);
  44. await client.processTransaction(tx);
  45. });
  46. });