|
1 | 1 | import { describe, test } from "node:test"; |
2 | | -import { |
3 | | - Keypair, |
4 | | - LAMPORTS_PER_SOL, |
5 | | - PublicKey, |
6 | | - SystemProgram, |
7 | | - Transaction, |
8 | | -} from "@solana/web3.js"; |
| 2 | +import { Keypair, LAMPORTS_PER_SOL, PublicKey, Transaction } from "@solana/web3.js"; |
9 | 3 | import { start } from "solana-bankrun"; |
| 4 | +import { createTransferInstruction } from "./instruction"; |
10 | 5 |
|
11 | | -describe("transfer-sol", async () => { |
12 | | - console.log("transfer-sol"); |
| 6 | +describe("transfer-sol (asm)", async () => { |
| 7 | + const PROGRAM_ID = PublicKey.unique(); |
| 8 | + const context = await start([{ name: "transfer-sol-cpi", programId: PROGRAM_ID }], []); |
| 9 | + const client = context.banksClient; |
| 10 | + const payer = context.payer; |
| 11 | + |
| 12 | + const transferAmount = 1 * LAMPORTS_PER_SOL; |
| 13 | + const recipient = Keypair.generate(); |
| 14 | + |
| 15 | + test("Transfer SOL via CPI to the system program", async () => { |
| 16 | + await getBalances(payer.publicKey, recipient.publicKey, "Beginning"); |
| 17 | + |
| 18 | + const ix = createTransferInstruction( |
| 19 | + payer.publicKey, |
| 20 | + recipient.publicKey, |
| 21 | + PROGRAM_ID, |
| 22 | + transferAmount, |
| 23 | + ); |
| 24 | + |
| 25 | + const tx = new Transaction(); |
| 26 | + const [blockhash, _] = await client.getLatestBlockhash(); |
| 27 | + tx.recentBlockhash = blockhash; |
| 28 | + tx.add(ix).sign(payer); |
| 29 | + |
| 30 | + await client.processTransaction(tx); |
| 31 | + |
| 32 | + await getBalances(payer.publicKey, recipient.publicKey, "Resulting"); |
| 33 | + }); |
| 34 | + |
| 35 | + async function getBalances(payerPubkey: PublicKey, recipientPubkey: PublicKey, timeframe: string) { |
| 36 | + const payerBalance = await client.getBalance(payerPubkey); |
| 37 | + const recipientBalance = await client.getBalance(recipientPubkey); |
| 38 | + |
| 39 | + console.log(`${timeframe} balances:`); |
| 40 | + console.log(` Payer: ${payerBalance}`); |
| 41 | + console.log(` Recipient: ${recipientBalance}`); |
| 42 | + } |
13 | 43 | }); |
0 commit comments