test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. DepositRent: Buffer.from([1]),
  26. CreateNewAccount: Buffer.from([2]),
  27. }
  28. describe("Pay the rent for an account using a PDA", () => {
  29. let context: ProgramTestContext;
  30. let lastBlock: Blockhash;
  31. let client: BanksClient;
  32. let payer: Keypair;
  33. const [vault_pda, _] = PublicKey.findProgramAddressSync(
  34. [VAULT_SEED],
  35. PROGRAM_ID,
  36. );
  37. before(async () => {
  38. context = await start(
  39. [{ name: "pda_rent_payer_program", programId: PROGRAM_ID }],
  40. [],
  41. );
  42. client = context.banksClient;
  43. payer = context.payer;
  44. lastBlock = context.lastBlockhash;
  45. });
  46. it("should initialize rent vault PDA", async () => {
  47. const data = Buffer.concat([instructionDiscriminators.InitializeRentVault]);
  48. const ix = new TransactionInstruction({
  49. keys: [
  50. { pubkey: payer.publicKey, isSigner: true, isWritable: false },
  51. { pubkey: vault_pda, isSigner: true, isWritable: true },
  52. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  53. ],
  54. programId: PROGRAM_ID,
  55. data,
  56. });
  57. const tx = new Transaction();
  58. tx.recentBlockhash = lastBlock;
  59. tx.add(ix).sign(payer);
  60. // Process Transaction with all the instructions
  61. await client.processTransaction(tx);
  62. });
  63. it("should deposit rent into the vault", async () => {
  64. });
  65. it("should create new account using rent vault", async () => {
  66. const new_account = Keypair.generate();
  67. const data = Buffer.concat([instructionDiscriminators.CreateNewAccount]);
  68. const ix = new TransactionInstruction({
  69. keys: [
  70. { pubkey: vault_pda, isSigner: false, isWritable: true },
  71. { pubkey: new_account.publicKey, isSigner: true, isWritable: true },
  72. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  73. ],
  74. programId: PROGRAM_ID,
  75. data,
  76. });
  77. const tx = new Transaction();
  78. tx.recentBlockhash = context.lastBlockhash;
  79. tx.add(ix).sign(new_account);
  80. // Process Transaction with all the instructions
  81. const transaction = await client.processTransaction(tx);
  82. // assert(
  83. // transaction.logMessages[3].startsWith(
  84. // "Program log: Created new account!",
  85. // ),
  86. // );
  87. });
  88. });