test.ts 1.9 KB

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