test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import {
  2. type Blockhash,
  3. Keypair,
  4. LAMPORTS_PER_SOL,
  5. PublicKey,
  6. SystemProgram,
  7. Transaction,
  8. TransactionInstruction,
  9. } from "@solana/web3.js";
  10. import { assert } from "chai";
  11. import { before, describe, it } from "mocha";
  12. import {
  13. type BanksClient,
  14. type ProgramTestContext,
  15. start,
  16. } from "solana-bankrun";
  17. // Constants
  18. const PROGRAM_ID = new PublicKey(
  19. "H8ocBhDZmzxRvWnT1yu5EQyLN3D9AYZv9qsePcx8pidg",
  20. );
  21. const VAULT_SEED = Buffer.from("rent_vault");
  22. const LOAD_LAMPORTS = LAMPORTS_PER_SOL; // 1 SOL
  23. const instructionDiscriminators = {
  24. InitializeRentVault: Buffer.from([0]),
  25. CreateNewAccount: Buffer.from([1]),
  26. }
  27. describe("Pay the rent for an account using a PDA", () => {
  28. let context: ProgramTestContext;
  29. let client: BanksClient;
  30. let payer: Keypair;
  31. const [vault_pda, _] = PublicKey.findProgramAddressSync(
  32. [VAULT_SEED],
  33. PROGRAM_ID,
  34. );
  35. before(async () => {
  36. context = await start(
  37. [{ name: "pda_rent_payer_program", programId: PROGRAM_ID }],
  38. [],
  39. );
  40. client = context.banksClient;
  41. payer = context.payer;
  42. });
  43. it("should initialize rent vault PDA", async () => {
  44. const amount = Buffer.alloc(8);
  45. amount.writeBigInt64BE(BigInt(LOAD_LAMPORTS), 0);
  46. const data = Buffer.concat([instructionDiscriminators.InitializeRentVault, amount]);
  47. const ix = new TransactionInstruction({
  48. keys: [
  49. { pubkey: payer.publicKey, isSigner: true, isWritable: false },
  50. { pubkey: vault_pda, isSigner: false, isWritable: true },
  51. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  52. ],
  53. programId: PROGRAM_ID,
  54. data,
  55. });
  56. const tx = new Transaction();
  57. tx.recentBlockhash = context.lastBlockhash;
  58. tx.add(ix).sign(payer);
  59. // Process Transaction with all the instructions
  60. await client.processTransaction(tx);
  61. });
  62. it("should create new account using rent vault", async () => {
  63. const new_account = Keypair.generate();
  64. const data = Buffer.concat([instructionDiscriminators.CreateNewAccount]);
  65. const ix = new TransactionInstruction({
  66. keys: [
  67. { pubkey: vault_pda, isSigner: false, isWritable: true },
  68. { pubkey: new_account.publicKey, isSigner: true, isWritable: true },
  69. ],
  70. programId: PROGRAM_ID,
  71. data,
  72. });
  73. const tx = new Transaction();
  74. tx.recentBlockhash = context.lastBlockhash;
  75. tx.add(ix).sign(new_account);
  76. // Process Transaction with all the instructions
  77. const transaction = await client.processTransaction(tx);
  78. // assert(
  79. // transaction.logMessages[3].startsWith(
  80. // "Program log: Created new account!",
  81. // ),
  82. // );
  83. });
  84. });