|
| 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/rent_example.json" with { type: "json" }; |
| 13 | + |
| 14 | +describe("LiteSVM: Create a system account", () => { |
| 15 | + const svm = new LiteSVM(); |
| 16 | + const programId = new PublicKey(Idl.address); |
| 17 | + const coder = new anchor.BorshCoder(Idl as anchor.Idl); |
| 18 | + |
| 19 | + const payer = Keypair.generate(); |
| 20 | + svm.airdrop(payer.publicKey, BigInt(2 * LAMPORTS_PER_SOL)); |
| 21 | + |
| 22 | + const programPath = new URL( |
| 23 | + "../target/deploy/rent_example.so", |
| 24 | + import.meta.url, |
| 25 | + ).pathname; |
| 26 | + svm.addProgramFromFile(programId, programPath); |
| 27 | + |
| 28 | + it("Create the account", () => { |
| 29 | + const newKeypair = Keypair.generate(); |
| 30 | + |
| 31 | + const ixArgs = { |
| 32 | + address_data: { |
| 33 | + name: "Marcus", |
| 34 | + address: "123 Main St. San Francisco, CA", |
| 35 | + }, |
| 36 | + }; |
| 37 | + |
| 38 | + /** |
| 39 | + * Create Instructions |
| 40 | + * Create Transactions |
| 41 | + * Send Transactions |
| 42 | + */ |
| 43 | + const data = coder.instruction.encode("create_system_account", ixArgs); |
| 44 | + const ix = new TransactionInstruction({ |
| 45 | + keys: [ |
| 46 | + { pubkey: payer.publicKey, isSigner: true, isWritable: true }, |
| 47 | + { pubkey: newKeypair.publicKey, isSigner: true, isWritable: true }, |
| 48 | + { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, |
| 49 | + ], |
| 50 | + programId, |
| 51 | + data, |
| 52 | + }); |
| 53 | + |
| 54 | + const tx = new Transaction().add(ix); |
| 55 | + tx.feePayer = payer.publicKey; |
| 56 | + tx.recentBlockhash = svm.latestBlockhash(); |
| 57 | + tx.sign(payer, newKeypair); |
| 58 | + svm.sendTransaction(tx); |
| 59 | + |
| 60 | + /** |
| 61 | + * We're just going to serialize our object here so we can check |
| 62 | + * the size on the client side against the program logs |
| 63 | + */ |
| 64 | + const addressDataBuffer = coder.types.encode( |
| 65 | + "AddressData", |
| 66 | + ixArgs.address_data, |
| 67 | + ); |
| 68 | + |
| 69 | + //Fetch newKeypair account and check its rent for space |
| 70 | + const newKeypairInfo = svm.getAccount(newKeypair.publicKey); |
| 71 | + |
| 72 | + assert.equal(newKeypairInfo.data.length, addressDataBuffer.length); |
| 73 | + }); |
| 74 | +}); |
0 commit comments