test.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. rentVault: rentVaultPDA,
  21. })
  22. .rpc();
  23. // Check rent vault balance
  24. const accountInfo = await program.provider.connection.getAccountInfo(rentVaultPDA);
  25. assert(accountInfo.lamports === fundAmount.toNumber());
  26. });
  27. it('Create a new account using the Rent Vault', async () => {
  28. // Generate a new keypair for the new account
  29. const newAccount = new Keypair();
  30. await program.methods
  31. .createNewAccount()
  32. .accounts({
  33. rentVault: rentVaultPDA,
  34. newAccount: newAccount.publicKey,
  35. })
  36. .signers([newAccount])
  37. .rpc();
  38. // Minimum balance for rent exemption for new account
  39. const lamports = await connection.getMinimumBalanceForRentExemption(0);
  40. // Check that the account was created
  41. const accountInfo = await connection.getAccountInfo(newAccount.publicKey);
  42. assert(accountInfo.lamports === lamports);
  43. });
  44. });