Skip to content

Commit 5422915

Browse files
committed
feat: add LiteSVM tests for PDA Rent-Payer
* Implement tests for initializing the Rent Vault and creating new accounts using the Rent Vault. * Validate lamports in the Rent Vault and new accounts after transactions.
1 parent 020ebe7 commit 5422915

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import anchor from "@coral-xyz/anchor";
2+
import {
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 { LiteSVM } from "litesvm";
12+
import IDL from "../target/idl/pda_rent_payer.json" with { type: "json" };
13+
14+
describe("LiteSVM: PDA Rent-Payer", () => {
15+
const svm = new LiteSVM();
16+
const programId = new PublicKey(IDL.address);
17+
const coder = new anchor.BorshCoder(IDL as anchor.Idl);
18+
const payer = Keypair.generate();
19+
svm.airdrop(payer.publicKey, BigInt(10000000000));
20+
21+
const programPath = new URL(
22+
"../target/deploy/pda_rent_payer.so",
23+
import.meta.url,
24+
).pathname;
25+
svm.addProgramFromFile(programId, programPath);
26+
27+
/**
28+
* generate PDA for the Rent Vault
29+
*/
30+
const [rentVaultPDA] = PublicKey.findProgramAddressSync(
31+
[Buffer.from("rent_vault")],
32+
programId,
33+
);
34+
35+
it("Initialize the Rent Vault", () => {
36+
const ixArgs = {
37+
fund_lamports: new anchor.BN(LAMPORTS_PER_SOL),
38+
};
39+
40+
const data = coder.instruction.encode("init_rent_vault", ixArgs);
41+
const ix = new TransactionInstruction({
42+
keys: [
43+
{ pubkey: payer.publicKey, isSigner: true, isWritable: true },
44+
{ pubkey: rentVaultPDA, isSigner: false, isWritable: true },
45+
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
46+
],
47+
programId,
48+
data,
49+
});
50+
51+
const tx = new Transaction().add(ix);
52+
tx.feePayer = payer.publicKey;
53+
tx.recentBlockhash = svm.latestBlockhash();
54+
tx.sign(payer);
55+
svm.sendTransaction(tx);
56+
57+
/**
58+
* Fetch the account and check its rent vault account info
59+
*/
60+
const rentVaultAccountInfo = svm.getAccount(rentVaultPDA);
61+
62+
assert.equal(rentVaultAccountInfo.lamports, LAMPORTS_PER_SOL);
63+
});
64+
65+
it("Create a new account using the Rent Vault", () => {
66+
const newAccount = new Keypair();
67+
68+
const data = coder.instruction.encode("create_new_account", {});
69+
const ix = new TransactionInstruction({
70+
keys: [
71+
{ pubkey: newAccount.publicKey, isSigner: true, isWritable: true },
72+
{ pubkey: rentVaultPDA, isSigner: false, isWritable: true },
73+
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
74+
],
75+
programId,
76+
data,
77+
});
78+
79+
const tx = new Transaction().add(ix);
80+
tx.feePayer = payer.publicKey;
81+
tx.recentBlockhash = svm.latestBlockhash();
82+
tx.sign(payer, newAccount);
83+
svm.sendTransaction(tx);
84+
85+
/**
86+
* Fetch the newAccount and check its rent
87+
*/
88+
const minLamports = svm.minimumBalanceForRentExemption(BigInt(0));
89+
const newAccountInfo = svm.getAccount(newAccount.publicKey);
90+
91+
assert.equal(newAccountInfo.lamports, Number(minLamports));
92+
});
93+
});

0 commit comments

Comments
 (0)