test.ts 1.7 KB

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