test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Keypair, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";
  3. import BN from "bn.js";
  4. import { assert } from "chai";
  5. import type { PdaRentPayer } from "../target/types/pda_rent_payer";
  6. describe("PDA Rent-Payer", () => {
  7. const provider = anchor.AnchorProvider.env();
  8. anchor.setProvider(provider);
  9. const wallet = provider.wallet as anchor.Wallet;
  10. const connection = provider.connection;
  11. const program = anchor.workspace.PdaRentPayer as anchor.Program<PdaRentPayer>;
  12. // PDA for the Rent Vault
  13. const [rentVaultPDA] = PublicKey.findProgramAddressSync(
  14. [Buffer.from("rent_vault")],
  15. program.programId,
  16. );
  17. it("Initialize the Rent Vault", async () => {
  18. // 1 SOL
  19. const fundAmount = new BN(LAMPORTS_PER_SOL);
  20. await program.methods
  21. .initRentVault(fundAmount)
  22. .accounts({
  23. payer: wallet.publicKey,
  24. })
  25. .rpc();
  26. // Check rent vault balance
  27. const accountInfo =
  28. 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. });