pda-rent-payer.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { PdaRentPayer } from "../target/types/pda_rent_payer";
  4. import { PublicKey } from "@solana/web3.js";
  5. describe("pda-rent-payer", () => {
  6. // Configure the client to use the local cluster.
  7. const provider = anchor.AnchorProvider.env();
  8. anchor.setProvider(provider);
  9. const wallet = provider.wallet;
  10. const connection = provider.connection;
  11. const program = anchor.workspace.PdaRentPayer as Program<PdaRentPayer>;
  12. // Amount of additional lamports to fund the dataAccount with.
  13. const fundLamports = 1 * anchor.web3.LAMPORTS_PER_SOL;
  14. // Derive the PDA that will be used to initialize the dataAccount.
  15. const [dataAccountPDA, bump] = PublicKey.findProgramAddressSync(
  16. [Buffer.from("rent_vault")],
  17. program.programId
  18. );
  19. it("Initialize the Rent Vault", async () => {
  20. // Add your test here.
  21. const tx = await program.methods
  22. .new([bump], new anchor.BN(fundLamports))
  23. .accounts({ dataAccount: dataAccountPDA })
  24. .rpc();
  25. console.log("Your transaction signature", tx);
  26. const accountInfo = await connection.getAccountInfo(dataAccountPDA);
  27. console.log("AccountInfo Lamports:", accountInfo.lamports);
  28. });
  29. it("Create a new account using the Rent Vault", async () => {
  30. const newAccount = anchor.web3.Keypair.generate();
  31. const space = 100; // number of bytes
  32. // Get the minimum balance for the account to be rent exempt.
  33. const lamports = await connection.getMinimumBalanceForRentExemption(space);
  34. // Invoke the createNewAccount instruction on our program
  35. const tx = await program.methods
  36. .createNewAccount(new anchor.BN(lamports))
  37. .accounts({
  38. ownedByProgram: dataAccountPDA,
  39. intendedRecipient: newAccount.publicKey, // account to create by directly transferring lamports
  40. })
  41. .rpc();
  42. console.log("Your transaction signature", tx);
  43. const accountInfo = await connection.getAccountInfo(newAccount.publicKey);
  44. console.log("AccountInfo Lamports:", accountInfo.lamports);
  45. });
  46. });