bankrun.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { describe, it } from 'node:test';
  2. import * as anchor from '@coral-xyz/anchor';
  3. import { Connection, Keypair, LAMPORTS_PER_SOL, PublicKey, clusterApiUrl } from '@solana/web3.js';
  4. import { BankrunProvider } from 'anchor-bankrun';
  5. import { assert } from 'chai';
  6. import { startAnchor } from 'solana-bankrun';
  7. import type { PdaRentPayer } from '../target/types/pda_rent_payer';
  8. const IDL = require('../target/idl/pda_rent_payer.json');
  9. const PROGRAM_ID = new PublicKey(IDL.address);
  10. describe('PDA Rent-Payer', async () => {
  11. const context = await startAnchor('', [{ name: 'pda_rent_payer', programId: PROGRAM_ID }], []);
  12. const provider = new BankrunProvider(context);
  13. const program = new anchor.Program<PdaRentPayer>(IDL, provider);
  14. const wallet = provider.wallet as anchor.Wallet;
  15. const connection = provider.connection;
  16. // PDA for the Rent Vault
  17. const auth = new Keypair();
  18. const [authPDA, authBump] = PublicKey.findProgramAddressSync([Buffer.from('auth')], PROGRAM_ID);
  19. const [rentVaultPDA, vaultBump] = PublicKey.findProgramAddressSync([Buffer.from('vault'), auth.publicKey.toBuffer()], PROGRAM_ID);
  20. const [newAccountPDA, newAccountBump] = PublicKey.findProgramAddressSync([Buffer.from('new_account')], PROGRAM_ID);
  21. // const [statePDA, stateBump] = PublicKey.findProgramAddressSync(
  22. // [Buffer.from("state"), wallet.publicKey.toBuffer()],
  23. // PROGRAM_ID
  24. // );
  25. console.log(rentVaultPDA, 'this is the rent vault pda');
  26. console.log(authPDA, 'this is the auth pda');
  27. console.log(newAccountPDA, 'this is the new account pda');
  28. it('Initialize the Rent Vault', async () => {
  29. const tx = await program.methods
  30. .initRentVault()
  31. .accounts({
  32. owner: wallet.publicKey,
  33. })
  34. .rpc();
  35. // Check rent vault balance
  36. const accountInfo = await program.provider.connection.getAccountInfo(rentVaultPDA);
  37. console.log(accountInfo, 'this is the account info');
  38. const lamports = await connection.getMinimumBalanceForRentExemption(8);
  39. assert(accountInfo !== null, 'Rent vault account does not exist');
  40. assert(accountInfo.lamports === lamports, 'Incorrect rent vault balance');
  41. });
  42. it('Deposit to the Rent Vault', async () => {
  43. // 1 SOL
  44. const fundAmount = new anchor.BN(LAMPORTS_PER_SOL);
  45. await program.methods
  46. .depositToRentVault(fundAmount)
  47. .accounts({
  48. owner: wallet.publicKey,
  49. })
  50. .rpc();
  51. // Check rent vault balance
  52. const accountInfo = await program.provider.connection.getAccountInfo(rentVaultPDA);
  53. // 9 is the space the rentVault account occupies, poseidon automatically generates space for you when you initialize a pda
  54. const lamports = await connection.getMinimumBalanceForRentExemption(43);
  55. assert(accountInfo !== null, 'Rent vault account does not exist');
  56. assert(accountInfo.lamports === fundAmount.toNumber() + lamports, 'Incorrect rent vault balance');
  57. });
  58. it('Create a new account using the Rent Vault', async () => {
  59. const newAccount = new Keypair();
  60. const fundAmount = new anchor.BN(LAMPORTS_PER_SOL);
  61. await program.methods
  62. .createNewAccount(fundAmount)
  63. .accounts({
  64. owner: newAccount.publicKey,
  65. })
  66. .signers([newAccount])
  67. .rpc();
  68. // Check that the account was created
  69. const accountInfo = await connection.getAccountInfo(newAccountPDA);
  70. // 9 is the space the newAccount occupies, poseidon automatically generates space for you when you initialize a pda
  71. const lamports = await connection.getMinimumBalanceForRentExemption(41);
  72. console.log(lamports);
  73. console.log(accountInfo.lamports);
  74. assert(accountInfo !== null, 'Rent vault account does not exist');
  75. assert(accountInfo.lamports === fundAmount.toNumber() + lamports, 'Incorrect rent vault balance');
  76. });
  77. });
  78. //
  79. //owner == payer, use payer
  80. //payer --> rentvault --> newAccount