|
| 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 | + |
| 18 | +// Constants |
| 19 | +const PROGRAM_ID = new PublicKey( |
| 20 | + "12rpZ18eGj7BeKvSFRZ45cni97HctTbKziBnW3MsH3NG", |
| 21 | +); |
| 22 | +const VAULT_SEED = Buffer.from("rent_vault"); |
| 23 | +const NEW_ACCOUNT_SEED = Buffer.from("new_account"); |
| 24 | +const LOAD_LAMPORTS = LAMPORTS_PER_SOL; // 1 SOL |
| 25 | + |
| 26 | +const instructionDiscriminators = { |
| 27 | + InitializeRentVault: Buffer.from([0]), |
| 28 | + CreateNewAccount: Buffer.from([1]), |
| 29 | +} |
| 30 | + |
| 31 | +describe("Pay the rent for an account using a PDA", () => { |
| 32 | + let context: ProgramTestContext; |
| 33 | + let lastBlock: Blockhash; |
| 34 | + let client: BanksClient; |
| 35 | + let payer: Keypair; |
| 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 | + |
| 47 | + it("Initialize rent vault PDA", async () => { |
| 48 | + const [vault_pda, _] = await PublicKey.findProgramAddressSync( |
| 49 | + [VAULT_SEED, payer.publicKey.toBuffer()], |
| 50 | + PROGRAM_ID, |
| 51 | + ); |
| 52 | + |
| 53 | + const data = Buffer.concat([instructionDiscriminators.InitializeRentVault, Buffer.from([LOAD_LAMPORTS])]); |
| 54 | + |
| 55 | + const ix = new TransactionInstruction({ |
| 56 | + keys: [ |
| 57 | + { pubkey: payer.publicKey, isSigner: true, isWritable: true }, |
| 58 | + { pubkey: vault_pda, isSigner: false, isWritable: true }, |
| 59 | + { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, |
| 60 | + ], |
| 61 | + programId: PROGRAM_ID, |
| 62 | + data, |
| 63 | + }); |
| 64 | + |
| 65 | + const tx = new Transaction(); |
| 66 | + tx.recentBlockhash = lastBlock; |
| 67 | + tx.add(ix).sign(payer); |
| 68 | + |
| 69 | + // Process Transaction with all the instructions |
| 70 | + const transaction = await client.processTransaction(tx); |
| 71 | + |
| 72 | + assert( |
| 73 | + transaction.logMessages[3].startsWith( |
| 74 | + "Program log: Initialized rent vault.", |
| 75 | + ), |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it("Create new account using rent vault", async () => { |
| 80 | + const new_account = Keypair.generate(); |
| 81 | + |
| 82 | + const data = instructionDiscriminators.CreateNewAccount; |
| 83 | + |
| 84 | + const ix = new TransactionInstruction({ |
| 85 | + keys: [ |
| 86 | + { pubkey: payer.publicKey, isSigner: true, isWritable: true }, |
| 87 | + { pubkey: new_account.publicKey, isSigner: false, isWritable: true }, |
| 88 | + { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, |
| 89 | + ], |
| 90 | + programId: PROGRAM_ID, |
| 91 | + data, |
| 92 | + }); |
| 93 | + |
| 94 | + const tx = new Transaction(); |
| 95 | + tx.recentBlockhash = lastBlock; |
| 96 | + tx.add(ix).sign(payer, new_account); |
| 97 | + |
| 98 | + // Process Transaction with all the instructions |
| 99 | + const transaction = await client.processTransaction(tx); |
| 100 | + |
| 101 | + assert( |
| 102 | + transaction.logMessages[3].startsWith( |
| 103 | + "Program log: Created new account!", |
| 104 | + ), |
| 105 | + ); |
| 106 | + }); |
| 107 | +}); |
0 commit comments