pda-rent-payer.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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({ dataAccount: dataAccountPDA })
  38. .remainingAccounts([
  39. {
  40. pubkey: newAccount.publicKey, // account to create by directly transferring lamports
  41. isWritable: true,
  42. isSigner: false,
  43. },
  44. ])
  45. .rpc();
  46. console.log("Your transaction signature", tx);
  47. const accountInfo = await connection.getAccountInfo(newAccount.publicKey);
  48. console.log("AccountInfo Lamports:", accountInfo.lamports);
  49. });
  50. });