create_system_account.ts 1.3 KB

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