|
| 1 | +import anchor from "@coral-xyz/anchor"; |
| 2 | +import { |
| 3 | + Keypair, |
| 4 | + PublicKey, |
| 5 | + Transaction, |
| 6 | + TransactionInstruction, |
| 7 | +} from "@solana/web3.js"; |
| 8 | +import { LiteSVM } from "litesvm"; |
| 9 | +import IDL from "../target/idl/hello_solana.json" with { type: "json" }; |
| 10 | + |
| 11 | +describe("LiteSVM: hello-solana", () => { |
| 12 | + const svm = new LiteSVM(); |
| 13 | + const programId = new PublicKey(IDL.address); |
| 14 | + /** |
| 15 | + * Creates a coder to easily build and encode program instructions based on the IDL. |
| 16 | + */ |
| 17 | + const coder = new anchor.BorshCoder(IDL as anchor.Idl); |
| 18 | + |
| 19 | + const payer = Keypair.generate(); |
| 20 | + svm.airdrop(payer.publicKey, BigInt(1000000000)); |
| 21 | + |
| 22 | + /** |
| 23 | + * Load the hello_solana program binary into the LiteSVM instance |
| 24 | + * for local testing and simulation. |
| 25 | + */ |
| 26 | + const programPath = new URL( |
| 27 | + "../target/deploy/hello_solana.so", |
| 28 | + import.meta.url, |
| 29 | + ).pathname; |
| 30 | + svm.addProgramFromFile(programId, programPath); |
| 31 | + |
| 32 | + it("Say hello!", () => { |
| 33 | + /** |
| 34 | + * Create an instruction for the 'hello' method using the Anchor coder. |
| 35 | + * No arguments are needed for this instruction so i give `{}`. |
| 36 | + */ |
| 37 | + const data = coder.instruction.encode("hello", {}); |
| 38 | + |
| 39 | + /** |
| 40 | + * Build and sign a transaction to call the 'hello' instruction |
| 41 | + * on the hello_solana program with LiteSVM. |
| 42 | + */ |
| 43 | + const ix = new TransactionInstruction({ |
| 44 | + keys: [], |
| 45 | + programId, |
| 46 | + data, |
| 47 | + }); |
| 48 | + |
| 49 | + const tx = new Transaction().add(ix); |
| 50 | + tx.feePayer = payer.publicKey; |
| 51 | + tx.recentBlockhash = svm.latestBlockhash(); |
| 52 | + tx.sign(payer); |
| 53 | + svm.sendTransaction(tx); |
| 54 | + }); |
| 55 | +}); |
0 commit comments