bankrun.test.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { describe, it } from 'node:test';
  2. import * as anchor from '@coral-xyz/anchor';
  3. import { Keypair, LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js';
  4. import { BankrunProvider } from 'anchor-bankrun';
  5. import { assert } from 'chai';
  6. import { startAnchor } from 'solana-bankrun';
  7. import type { PdaRentPayer } from '../target/types/pda_rent_payer';
  8. const IDL = require('../target/idl/pda_rent_payer.json');
  9. const PROGRAM_ID = new PublicKey(IDL.address);
  10. describe('PDA Rent-Payer', async () => {
  11. const context = await startAnchor('', [{ name: 'pda_rent_payer', programId: PROGRAM_ID }], []);
  12. const provider = new BankrunProvider(context);
  13. const program = new anchor.Program<PdaRentPayer>(IDL, provider);
  14. const wallet = provider.wallet as anchor.Wallet;
  15. const connection = provider.connection;
  16. // PDA for the Rent Vault
  17. const [rentVaultPDA] = PublicKey.findProgramAddressSync([Buffer.from('rent_vault')], program.programId);
  18. it('Initialize the Rent Vault', async () => {
  19. // 1 SOL
  20. const fundAmount = new anchor.BN(LAMPORTS_PER_SOL);
  21. await program.methods
  22. .initRentVault(fundAmount)
  23. .accounts({
  24. payer: wallet.publicKey,
  25. })
  26. .rpc();
  27. // Check rent vault balance
  28. const accountInfo = await program.provider.connection.getAccountInfo(rentVaultPDA);
  29. assert(accountInfo.lamports === fundAmount.toNumber());
  30. });
  31. it('Create a new account using the Rent Vault', async () => {
  32. // Generate a new keypair for the new account
  33. const newAccount = new Keypair();
  34. await program.methods
  35. .createNewAccount()
  36. .accounts({
  37. newAccount: newAccount.publicKey,
  38. })
  39. .signers([newAccount])
  40. .rpc();
  41. // Minimum balance for rent exemption for new account
  42. const lamports = await connection.getMinimumBalanceForRentExemption(0);
  43. // Check that the account was created
  44. const accountInfo = await connection.getAccountInfo(newAccount.publicKey);
  45. assert(accountInfo.lamports === lamports);
  46. });
  47. });