main.test.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { type Blockhash, Keypair, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction, TransactionInstruction } from '@solana/web3.js';
  2. import { assert } from 'chai';
  3. import { before, describe, it } from 'mocha';
  4. import { type BanksClient, type ProgramTestContext, start } from 'solana-bankrun';
  5. const PROGRAM_ID = new PublicKey('12rpZ18eGj7BeKvSFRZ45cni97HctTbKziBnW3MsH3NG');
  6. const instructionDiscriminators = {
  7. InitializeAccount: Buffer.from([0]),
  8. };
  9. describe('Create a system account', () => {
  10. let context: ProgramTestContext;
  11. let lastBlock: Blockhash;
  12. let client: BanksClient;
  13. let payer: Keypair;
  14. before(async () => {
  15. context = await start([{ name: 'create_account_program', programId: PROGRAM_ID }], []);
  16. client = context.banksClient;
  17. payer = context.payer;
  18. lastBlock = context.lastBlockhash;
  19. });
  20. it('should create the account via a cross program invocation', async () => {
  21. const newAccount = Keypair.generate();
  22. const ix = new TransactionInstruction({
  23. keys: [
  24. { pubkey: payer.publicKey, isSigner: true, isWritable: true },
  25. { pubkey: newAccount.publicKey, isSigner: true, isWritable: true },
  26. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  27. ],
  28. programId: PROGRAM_ID,
  29. data: Buffer.concat([instructionDiscriminators.InitializeAccount]),
  30. });
  31. const tx = new Transaction();
  32. tx.recentBlockhash = lastBlock;
  33. tx.add(ix).sign(payer, newAccount);
  34. // No other tests required besides confirming if the transaction is processed
  35. // Since transactions are atomic, we can be certain the account was created
  36. await client.processTransaction(tx);
  37. });
  38. });