From 5422915cd907305e2d3c310e8735eeba95b25173 Mon Sep 17 00:00:00 2001 From: solwarrior <118261237+sol-warrior@users.noreply.github.com> Date: Wed, 10 Dec 2025 22:27:10 +0530 Subject: [PATCH 1/3] 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. --- .../anchor/tests/litesvm.test.ts | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 basics/pda-rent-payer/anchor/tests/litesvm.test.ts diff --git a/basics/pda-rent-payer/anchor/tests/litesvm.test.ts b/basics/pda-rent-payer/anchor/tests/litesvm.test.ts new file mode 100644 index 000000000..b7bf80451 --- /dev/null +++ b/basics/pda-rent-payer/anchor/tests/litesvm.test.ts @@ -0,0 +1,93 @@ +import anchor from "@coral-xyz/anchor"; +import { + Keypair, + LAMPORTS_PER_SOL, + PublicKey, + SystemProgram, + Transaction, + TransactionInstruction, +} from "@solana/web3.js"; +import { assert } from "chai"; +import { LiteSVM } from "litesvm"; +import IDL from "../target/idl/pda_rent_payer.json" with { type: "json" }; + +describe("LiteSVM: PDA Rent-Payer", () => { + const svm = new LiteSVM(); + const programId = new PublicKey(IDL.address); + const coder = new anchor.BorshCoder(IDL as anchor.Idl); + const payer = Keypair.generate(); + svm.airdrop(payer.publicKey, BigInt(10000000000)); + + const programPath = new URL( + "../target/deploy/pda_rent_payer.so", + import.meta.url, + ).pathname; + svm.addProgramFromFile(programId, programPath); + + /** + * generate PDA for the Rent Vault + */ + const [rentVaultPDA] = PublicKey.findProgramAddressSync( + [Buffer.from("rent_vault")], + programId, + ); + + it("Initialize the Rent Vault", () => { + const ixArgs = { + fund_lamports: new anchor.BN(LAMPORTS_PER_SOL), + }; + + const data = coder.instruction.encode("init_rent_vault", ixArgs); + const ix = new TransactionInstruction({ + keys: [ + { pubkey: payer.publicKey, isSigner: true, isWritable: true }, + { pubkey: rentVaultPDA, isSigner: false, isWritable: true }, + { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, + ], + programId, + data, + }); + + const tx = new Transaction().add(ix); + tx.feePayer = payer.publicKey; + tx.recentBlockhash = svm.latestBlockhash(); + tx.sign(payer); + svm.sendTransaction(tx); + + /** + * Fetch the account and check its rent vault account info + */ + const rentVaultAccountInfo = svm.getAccount(rentVaultPDA); + + assert.equal(rentVaultAccountInfo.lamports, LAMPORTS_PER_SOL); + }); + + it("Create a new account using the Rent Vault", () => { + const newAccount = new Keypair(); + + const data = coder.instruction.encode("create_new_account", {}); + const ix = new TransactionInstruction({ + keys: [ + { pubkey: newAccount.publicKey, isSigner: true, isWritable: true }, + { pubkey: rentVaultPDA, isSigner: false, isWritable: true }, + { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, + ], + programId, + data, + }); + + const tx = new Transaction().add(ix); + tx.feePayer = payer.publicKey; + tx.recentBlockhash = svm.latestBlockhash(); + tx.sign(payer, newAccount); + svm.sendTransaction(tx); + + /** + * Fetch the newAccount and check its rent + */ + const minLamports = svm.minimumBalanceForRentExemption(BigInt(0)); + const newAccountInfo = svm.getAccount(newAccount.publicKey); + + assert.equal(newAccountInfo.lamports, Number(minLamports)); + }); +}); From d59cf180de08061df444c93e1571d20915487ac2 Mon Sep 17 00:00:00 2001 From: solwarrior <118261237+sol-warrior@users.noreply.github.com> Date: Wed, 10 Dec 2025 22:44:43 +0530 Subject: [PATCH 2/3] chore: update dependencies and TypeScript configuration for PDA Rent-Payer - Added litesvm dependency to package.json. - Adjusted import path for PdaRentPayer in test.ts to include the .ts extension. - Updated pnpm-lock.yaml to include litesvm and its variants. - Changed TypeScript module resolution from commonjs to nodenext. --- basics/pda-rent-payer/anchor/package.json | 3 +- basics/pda-rent-payer/anchor/pnpm-lock.yaml | 83 +++++++++++++++++++++ basics/pda-rent-payer/anchor/tests/test.ts | 2 +- basics/pda-rent-payer/anchor/tsconfig.json | 16 ++-- 4 files changed, 94 insertions(+), 10 deletions(-) diff --git a/basics/pda-rent-payer/anchor/package.json b/basics/pda-rent-payer/anchor/package.json index 7865c8c24..36c97ecf6 100644 --- a/basics/pda-rent-payer/anchor/package.json +++ b/basics/pda-rent-payer/anchor/package.json @@ -2,7 +2,8 @@ "type": "module", "dependencies": { "@coral-xyz/anchor": "0.32.1", - "@solana/web3.js": "^1.95.2" + "@solana/web3.js": "^1.95.2", + "litesvm": "^0.4.0" }, "devDependencies": { "@types/bn.js": "^5.1.0", diff --git a/basics/pda-rent-payer/anchor/pnpm-lock.yaml b/basics/pda-rent-payer/anchor/pnpm-lock.yaml index 4cc1440b8..12fb38010 100644 --- a/basics/pda-rent-payer/anchor/pnpm-lock.yaml +++ b/basics/pda-rent-payer/anchor/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@solana/web3.js': specifier: ^1.95.2 version: 1.98.4(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10) + litesvm: + specifier: ^0.4.0 + version: 0.4.0(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10) devDependencies: '@types/bn.js': specifier: ^5.1.0 @@ -322,6 +325,9 @@ packages: fast-stable-stringify@1.0.0: resolution: {integrity: sha512-wpYMUmFu5f00Sm0cj2pfivpmawLZ0NKdviQ4w9zJeR8JVtOpOxHmLaJuj0vxvGqMJQWyP/COUkF75/57OKyRag==} + fastestsmallesttextencoderdecoder@1.0.22: + resolution: {integrity: sha512-Pb8d48e+oIuY4MaM64Cd7OW1gt4nxCHs7/ddPPZ/Ic3sg8yVGM7O9wDvZ7us6ScaUupzM+pfBolwtYhN1IxBIw==} + fill-range@7.1.1: resolution: {integrity: sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==} engines: {node: '>=8'} @@ -434,6 +440,46 @@ packages: resolution: {integrity: sha512-g1MWMLBiz8FKi1e4w0UyVL3w+iJceWAFBAaBnnGKOpNa5f8TLktkbre1+s6oICydWAm+HRUGTmI+//xv2hvXYA==} hasBin: true + litesvm-darwin-arm64@0.4.0: + resolution: {integrity: sha512-LN6iZcUQ6Xi5KO/7yJBYSALjjDCI/s/s2PgV3BqM4dpeBaLz+fXX/+qgMcBgpEVgEdEmhelux+WtAMkbEzJfrA==} + engines: {node: '>= 20'} + cpu: [arm64] + os: [darwin] + + litesvm-darwin-x64@0.4.0: + resolution: {integrity: sha512-3ltogKQdle8LbakVqoB6plxaNwp6Vb3tnkqa3G5mAvvZNorB2iumThDaTZ381Knl69t566LZm+g/VDZwYfsfhA==} + engines: {node: '>= 20'} + cpu: [x64] + os: [darwin] + + litesvm-linux-arm64-gnu@0.4.0: + resolution: {integrity: sha512-SWlcRUqkXCMgLoDX/Wqr/S1lff+ggVI9f0YrRJMraxtEyApxutAoW2AWw4tvo6DsEgNwjxgsZOAwnE6bQBv8CA==} + engines: {node: '>= 20'} + cpu: [arm64] + os: [linux] + + litesvm-linux-arm64-musl@0.4.0: + resolution: {integrity: sha512-YMMqwEWJUSWwL0Rwp8dFwl3jvgNU21eI7Qc+BpH9u2yeIRYQTn3rNGDnsK8v3QIZPHQdMo7NrPhzk4XoB1aKPg==} + engines: {node: '>= 20'} + cpu: [arm64] + os: [linux] + + litesvm-linux-x64-gnu@0.4.0: + resolution: {integrity: sha512-brZ3tFABDVQEYCgci7AO8iVYLw10UXVo97/lpTy75bTzNoqkggg8wFQOrbgCdb9NRwt06Y4Zf8cpIZAoDQq2mw==} + engines: {node: '>= 20'} + cpu: [x64] + os: [linux] + + litesvm-linux-x64-musl@0.4.0: + resolution: {integrity: sha512-D98qdIOuWg4fOewIIiH1D23AtM4I7/3vLKXIL8uQz06D5ev5fsBzNp2gM7libAywTkCYy/u666xgD6PsWhrTaw==} + engines: {node: '>= 20'} + cpu: [x64] + os: [linux] + + litesvm@0.4.0: + resolution: {integrity: sha512-ySr5mB2ap4SzJpmVR2I5+gjzTH8NJbkg7DYPormzA2U9F4LhfvTTrD17X/k5N3Bn4b5Db6/CwSyX2qc0HrJtNA==} + engines: {node: '>= 20'} + locate-path@6.0.0: resolution: {integrity: sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==} engines: {node: '>=10'} @@ -1034,6 +1080,8 @@ snapshots: fast-stable-stringify@1.0.0: {} + fastestsmallesttextencoderdecoder@1.0.22: {} + fill-range@7.1.1: dependencies: to-regex-range: 5.0.1 @@ -1139,6 +1187,41 @@ snapshots: minimist: 1.2.8 optional: true + litesvm-darwin-arm64@0.4.0: + optional: true + + litesvm-darwin-x64@0.4.0: + optional: true + + litesvm-linux-arm64-gnu@0.4.0: + optional: true + + litesvm-linux-arm64-musl@0.4.0: + optional: true + + litesvm-linux-x64-gnu@0.4.0: + optional: true + + litesvm-linux-x64-musl@0.4.0: + optional: true + + litesvm@0.4.0(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10): + dependencies: + '@solana/web3.js': 1.98.4(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10) + fastestsmallesttextencoderdecoder: 1.0.22 + optionalDependencies: + litesvm-darwin-arm64: 0.4.0 + litesvm-darwin-x64: 0.4.0 + litesvm-linux-arm64-gnu: 0.4.0 + litesvm-linux-arm64-musl: 0.4.0 + litesvm-linux-x64-gnu: 0.4.0 + litesvm-linux-x64-musl: 0.4.0 + transitivePeerDependencies: + - bufferutil + - encoding + - typescript + - utf-8-validate + locate-path@6.0.0: dependencies: p-locate: 5.0.0 diff --git a/basics/pda-rent-payer/anchor/tests/test.ts b/basics/pda-rent-payer/anchor/tests/test.ts index f406b16b9..5467255ed 100644 --- a/basics/pda-rent-payer/anchor/tests/test.ts +++ b/basics/pda-rent-payer/anchor/tests/test.ts @@ -2,7 +2,7 @@ import * as anchor from "@coral-xyz/anchor"; import { Keypair, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js"; import BN from "bn.js"; import { assert } from "chai"; -import type { PdaRentPayer } from "../target/types/pda_rent_payer"; +import type { PdaRentPayer } from "../target/types/pda_rent_payer.ts"; describe("PDA Rent-Payer", () => { const provider = anchor.AnchorProvider.env(); diff --git a/basics/pda-rent-payer/anchor/tsconfig.json b/basics/pda-rent-payer/anchor/tsconfig.json index cd5d2e3d0..59fe75cf0 100644 --- a/basics/pda-rent-payer/anchor/tsconfig.json +++ b/basics/pda-rent-payer/anchor/tsconfig.json @@ -1,10 +1,10 @@ { - "compilerOptions": { - "types": ["mocha", "chai"], - "typeRoots": ["./node_modules/@types"], - "lib": ["es2015"], - "module": "commonjs", - "target": "es6", - "esModuleInterop": true - } + "compilerOptions": { + "types": ["mocha", "chai"], + "typeRoots": ["./node_modules/@types"], + "lib": ["es2015"], + "module": "nodenext", + "target": "es6", + "esModuleInterop": true + } } From 060be6afdca205191fd57150f6ab66976979d1c0 Mon Sep 17 00:00:00 2001 From: solwarrior <118261237+sol-warrior@users.noreply.github.com> Date: Wed, 10 Dec 2025 22:48:40 +0530 Subject: [PATCH 3/3] chore: Remove deprecated bankrun and related stuff --- basics/pda-rent-payer/anchor/package.json | 2 - basics/pda-rent-payer/anchor/pnpm-lock.yaml | 84 ------------------- .../anchor/tests/bankrun.test.ts | 67 --------------- 3 files changed, 153 deletions(-) delete mode 100644 basics/pda-rent-payer/anchor/tests/bankrun.test.ts diff --git a/basics/pda-rent-payer/anchor/package.json b/basics/pda-rent-payer/anchor/package.json index 36c97ecf6..1f9417836 100644 --- a/basics/pda-rent-payer/anchor/package.json +++ b/basics/pda-rent-payer/anchor/package.json @@ -9,10 +9,8 @@ "@types/bn.js": "^5.1.0", "@types/chai": "^4.3.0", "@types/mocha": "^9.0.0", - "anchor-bankrun": "^0.4.0", "chai": "^4.3.4", "mocha": "^9.0.3", - "solana-bankrun": "^0.3.0", "ts-mocha": "^10.0.0", "typescript": "^4.3.5" } diff --git a/basics/pda-rent-payer/anchor/pnpm-lock.yaml b/basics/pda-rent-payer/anchor/pnpm-lock.yaml index 12fb38010..b423a72a4 100644 --- a/basics/pda-rent-payer/anchor/pnpm-lock.yaml +++ b/basics/pda-rent-payer/anchor/pnpm-lock.yaml @@ -27,18 +27,12 @@ importers: '@types/mocha': specifier: ^9.0.0 version: 9.1.1 - anchor-bankrun: - specifier: ^0.4.0 - version: 0.4.1(@coral-xyz/anchor@0.32.1(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10))(@solana/web3.js@1.98.4(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10))(solana-bankrun@0.3.1(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10)) chai: specifier: ^4.3.4 version: 4.5.0 mocha: specifier: ^9.0.3 version: 9.2.2 - solana-bankrun: - specifier: ^0.3.0 - version: 0.3.1(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10) ts-mocha: specifier: ^10.0.0 version: 10.1.0(mocha@9.2.2) @@ -140,14 +134,6 @@ packages: resolution: {integrity: sha512-kja8j7PjmncONqaTsB8fQ+wE2mSU2DJ9D4XKoJ5PFWIdRMa6SLSN1ff4mOr4jCbfRSsxR4keIiySJU0N9T5hIQ==} engines: {node: '>= 8.0.0'} - anchor-bankrun@0.4.1: - resolution: {integrity: sha512-ryCT84tw+lP4AqRpBsZJbt/KTRoVVKufkxFGd77gnx9iHkbwA5G/9cALk/eqLQm4xeUWTrJSJdEVyg2e74iP9A==} - engines: {node: '>= 10'} - peerDependencies: - '@coral-xyz/anchor': ^0.30.0 - '@solana/web3.js': '>=1.78.4 <1.92.0' - solana-bankrun: ^0.2.0 - ansi-colors@4.1.1: resolution: {integrity: sha512-JoX0apGbHaUJBNl6yF+p6JAFYZ666/hhCGKN5t9QFjbJQKUU/g8MNbFDbvfrgKXvI1QpZplPOnwIo99lX/AAmA==} engines: {node: '>=6'} @@ -590,39 +576,6 @@ packages: serialize-javascript@6.0.0: resolution: {integrity: sha512-Qr3TosvguFt8ePWqsvRfrKyQXIiW+nGbYpy8XK24NQHE83caxWt+mIymTT19DGFbNWNLfEwsrkSmN64lVWB9ag==} - solana-bankrun-darwin-arm64@0.3.1: - resolution: {integrity: sha512-9LWtH/3/WR9fs8Ve/srdo41mpSqVHmRqDoo69Dv1Cupi+o1zMU6HiEPUHEvH2Tn/6TDbPEDf18MYNfReLUqE6A==} - engines: {node: '>= 10'} - cpu: [arm64] - os: [darwin] - - solana-bankrun-darwin-universal@0.3.1: - resolution: {integrity: sha512-muGHpVYWT7xCd8ZxEjs/bmsbMp8XBqroYGbE4lQPMDUuLvsJEIrjGqs3MbxEFr71sa58VpyvgywWd5ifI7sGIg==} - engines: {node: '>= 10'} - os: [darwin] - - solana-bankrun-darwin-x64@0.3.1: - resolution: {integrity: sha512-oCaxfHyt7RC3ZMldrh5AbKfy4EH3YRMl8h6fSlMZpxvjQx7nK7PxlRwMeflMnVdkKKp7U8WIDak1lilIPd3/lg==} - engines: {node: '>= 10'} - cpu: [x64] - os: [darwin] - - solana-bankrun-linux-x64-gnu@0.3.1: - resolution: {integrity: sha512-PfRFhr7igGFNt2Ecfdzh3li9eFPB3Xhmk0Eib17EFIB62YgNUg3ItRnQQFaf0spazFjjJLnglY1TRKTuYlgSVA==} - engines: {node: '>= 10'} - cpu: [x64] - os: [linux] - - solana-bankrun-linux-x64-musl@0.3.1: - resolution: {integrity: sha512-6r8i0NuXg3CGURql8ISMIUqhE7Hx/O7MlIworK4oN08jYrP0CXdLeB/hywNn7Z8d1NXrox/NpYUgvRm2yIzAsQ==} - engines: {node: '>= 10'} - cpu: [x64] - os: [linux] - - solana-bankrun@0.3.1: - resolution: {integrity: sha512-inRwON7fBU5lPC36HdEqPeDg15FXJYcf77+o0iz9amvkUMJepcwnRwEfTNyMVpVYdgjTOBW5vg+596/3fi1kGA==} - engines: {node: '>= 10'} - source-map-support@0.5.21: resolution: {integrity: sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==} @@ -911,12 +864,6 @@ snapshots: dependencies: humanize-ms: 1.2.1 - anchor-bankrun@0.4.1(@coral-xyz/anchor@0.32.1(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10))(@solana/web3.js@1.98.4(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10))(solana-bankrun@0.3.1(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10)): - dependencies: - '@coral-xyz/anchor': 0.32.1(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10) - '@solana/web3.js': 1.98.4(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10) - solana-bankrun: 0.3.1(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10) - ansi-colors@4.1.1: {} ansi-regex@5.0.1: {} @@ -1344,37 +1291,6 @@ snapshots: dependencies: randombytes: 2.1.0 - solana-bankrun-darwin-arm64@0.3.1: - optional: true - - solana-bankrun-darwin-universal@0.3.1: - optional: true - - solana-bankrun-darwin-x64@0.3.1: - optional: true - - solana-bankrun-linux-x64-gnu@0.3.1: - optional: true - - solana-bankrun-linux-x64-musl@0.3.1: - optional: true - - solana-bankrun@0.3.1(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10): - dependencies: - '@solana/web3.js': 1.98.4(bufferutil@4.0.9)(typescript@4.9.5)(utf-8-validate@5.0.10) - bs58: 4.0.1 - optionalDependencies: - solana-bankrun-darwin-arm64: 0.3.1 - solana-bankrun-darwin-universal: 0.3.1 - solana-bankrun-darwin-x64: 0.3.1 - solana-bankrun-linux-x64-gnu: 0.3.1 - solana-bankrun-linux-x64-musl: 0.3.1 - transitivePeerDependencies: - - bufferutil - - encoding - - typescript - - utf-8-validate - source-map-support@0.5.21: dependencies: buffer-from: 1.1.2 diff --git a/basics/pda-rent-payer/anchor/tests/bankrun.test.ts b/basics/pda-rent-payer/anchor/tests/bankrun.test.ts deleted file mode 100644 index 204553a70..000000000 --- a/basics/pda-rent-payer/anchor/tests/bankrun.test.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { describe, it } from "node:test"; -import * as anchor from "@coral-xyz/anchor"; -import { Keypair, LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js"; -import { BankrunProvider } from "anchor-bankrun"; -import BN from "bn.js"; -import { assert } from "chai"; -import { startAnchor } from "solana-bankrun"; -import type { PdaRentPayer } from "../target/types/pda_rent_payer"; - -import IDL from "../target/idl/pda_rent_payer.json"; -const PROGRAM_ID = new PublicKey(IDL.address); - -describe("PDA Rent-Payer", async () => { - const context = await startAnchor( - "", - [{ name: "pda_rent_payer", programId: PROGRAM_ID }], - [], - ); - const provider = new BankrunProvider(context); - const program = new anchor.Program(IDL, provider); - - const wallet = provider.wallet as anchor.Wallet; - const connection = provider.connection; - - // PDA for the Rent Vault - const [rentVaultPDA] = PublicKey.findProgramAddressSync( - [Buffer.from("rent_vault")], - program.programId, - ); - - it("Initialize the Rent Vault", async () => { - // 1 SOL - const fundAmount = new BN(LAMPORTS_PER_SOL); - - await program.methods - .initRentVault(fundAmount) - .accounts({ - payer: wallet.publicKey, - }) - .rpc(); - - // Check rent vault balance - const accountInfo = - await program.provider.connection.getAccountInfo(rentVaultPDA); - assert(accountInfo.lamports === fundAmount.toNumber()); - }); - - it("Create a new account using the Rent Vault", async () => { - // Generate a new keypair for the new account - const newAccount = new Keypair(); - - await program.methods - .createNewAccount() - .accounts({ - newAccount: newAccount.publicKey, - }) - .signers([newAccount]) - .rpc(); - - // Minimum balance for rent exemption for new account - const lamports = await connection.getMinimumBalanceForRentExemption(0); - - // Check that the account was created - const accountInfo = await connection.getAccountInfo(newAccount.publicKey); - assert(accountInfo.lamports === lamports); - }); -});