bankrun.test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import * as anchor from '@coral-xyz/anchor';
  2. import { Keypair, PublicKey, SystemProgram } from '@solana/web3.js';
  3. import { BankrunProvider } from 'anchor-bankrun';
  4. import { assert } from 'chai';
  5. import { startAnchor } from 'solana-bankrun';
  6. import type { CreateSystemAccountProgram } from '../target/types/create_system_account_program';
  7. const IDL = require('../target/idl/create_system_account.json');
  8. const PROGRAM_ID = new PublicKey(IDL.address);
  9. describe('Create a system account', async () => {
  10. const context = await startAnchor('', [{ name: 'create_system_account', programId: PROGRAM_ID }], []);
  11. const provider = new BankrunProvider(context);
  12. const wallet = provider.wallet as anchor.Wallet;
  13. const connection = provider.connection;
  14. const program = anchor.workspace.CreateSystemAccountProgram as anchor.Program<CreateSystemAccountProgram>;
  15. it('Create the account', async () => {
  16. // Generate the public key from the seed and the programId
  17. const [accountState, _] = anchor.web3.PublicKey.findProgramAddressSync([anchor.utils.bytes.utf8.encode('account')], program.programId);
  18. await program.methods
  19. .createSystemAccount()
  20. .accounts({
  21. owner: wallet.publicKey,
  22. })
  23. .rpc();
  24. // Minimum balance for rent exemption for new account
  25. const lamports = await connection.getMinimumBalanceForRentExemption(0);
  26. // Check that the account was created
  27. const accountInfo = await connection.getAccountInfo(accountState);
  28. assert.isNotNull(accountInfo, 'Account should be created');
  29. assert(accountInfo.lamports >= lamports, 'Account must have the minimum amount of lamports required for rent');
  30. });
  31. });