|
| 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/transfer_sol.json" with { type: "json" }; |
| 13 | + |
| 14 | +describe("LiteSVM: Transfer SOL", () => { |
| 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(5 * LAMPORTS_PER_SOL)); |
| 20 | + |
| 21 | + const programFilePath = new URL( |
| 22 | + "../target/deploy/transfer_sol.so", |
| 23 | + import.meta.url, |
| 24 | + ).pathname; |
| 25 | + svm.addProgramFromFile(programId, programFilePath); |
| 26 | + |
| 27 | + it("Transfer SOL with CPI", () => { |
| 28 | + const recipient = Keypair.generate(); |
| 29 | + |
| 30 | + const ixArgs = { |
| 31 | + amount: new anchor.BN(LAMPORTS_PER_SOL), |
| 32 | + }; |
| 33 | + const data = coder.instruction.encode("transfer_sol_with_cpi", ixArgs); |
| 34 | + const ix = new TransactionInstruction({ |
| 35 | + keys: [ |
| 36 | + { pubkey: payer.publicKey, isSigner: true, isWritable: true }, |
| 37 | + { pubkey: recipient.publicKey, isSigner: false, isWritable: true }, |
| 38 | + { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, |
| 39 | + ], |
| 40 | + programId, |
| 41 | + data, |
| 42 | + }); |
| 43 | + |
| 44 | + const tx = new Transaction().add(ix); |
| 45 | + tx.feePayer = payer.publicKey; |
| 46 | + tx.recentBlockhash = svm.latestBlockhash(); |
| 47 | + tx.sign(payer); |
| 48 | + svm.sendTransaction(tx); |
| 49 | + |
| 50 | + const recipientAcc = svm.getAccount(recipient.publicKey); |
| 51 | + assert.equal(recipientAcc.lamports, LAMPORTS_PER_SOL); |
| 52 | + }); |
| 53 | + |
| 54 | + it("Transfer SOL with Program", () => { |
| 55 | + const payerAccount = Keypair.generate(); |
| 56 | + const ixPayer = SystemProgram.createAccount({ |
| 57 | + fromPubkey: payer.publicKey, |
| 58 | + newAccountPubkey: payerAccount.publicKey, |
| 59 | + lamports: LAMPORTS_PER_SOL, |
| 60 | + space: 0, |
| 61 | + programId, |
| 62 | + }); |
| 63 | + const txPayer = new Transaction().add(ixPayer); |
| 64 | + txPayer.feePayer = payer.publicKey; |
| 65 | + txPayer.recentBlockhash = svm.latestBlockhash(); |
| 66 | + txPayer.sign(payer, payerAccount); |
| 67 | + svm.sendTransaction(txPayer); |
| 68 | + svm.expireBlockhash(); |
| 69 | + |
| 70 | + const recipientAccount = Keypair.generate(); |
| 71 | + |
| 72 | + const ixArgs = { |
| 73 | + amount: new anchor.BN(LAMPORTS_PER_SOL), |
| 74 | + }; |
| 75 | + const data = coder.instruction.encode("transfer_sol_with_program", ixArgs); |
| 76 | + const ix = new TransactionInstruction({ |
| 77 | + keys: [ |
| 78 | + { pubkey: payerAccount.publicKey, isSigner: true, isWritable: true }, |
| 79 | + { |
| 80 | + pubkey: recipientAccount.publicKey, |
| 81 | + isSigner: false, |
| 82 | + isWritable: true, |
| 83 | + }, |
| 84 | + { pubkey: SystemProgram.programId, isSigner: false, isWritable: false }, |
| 85 | + ], |
| 86 | + programId, |
| 87 | + data, |
| 88 | + }); |
| 89 | + |
| 90 | + const tx = new Transaction().add(ix); |
| 91 | + tx.feePayer = payer.publicKey; |
| 92 | + tx.recentBlockhash = svm.latestBlockhash(); |
| 93 | + tx.sign(payer, payerAccount); |
| 94 | + svm.sendTransaction(tx); |
| 95 | + |
| 96 | + const recipientAcc = svm.getAccount(recipientAccount.publicKey); |
| 97 | + assert.equal(recipientAcc.lamports, LAMPORTS_PER_SOL); |
| 98 | + }); |
| 99 | +}); |
0 commit comments