test.ts 1.7 KB

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