Skip to content

Commit 8c65bb9

Browse files
committed
add ts tests
1 parent c60fe8a commit 8c65bb9

2 files changed

Lines changed: 61 additions & 9 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Buffer } from "buffer";
2+
import { type PublicKey, SystemProgram, TransactionInstruction } from "@solana/web3.js";
3+
4+
export function createTransferInstruction(
5+
senderPubkey: PublicKey,
6+
recipientPubkey: PublicKey,
7+
programId: PublicKey,
8+
lamports: number,
9+
): TransactionInstruction {
10+
const data = Buffer.alloc(8);
11+
data.writeBigUInt64LE(BigInt(lamports));
12+
13+
return new TransactionInstruction({
14+
keys: [
15+
{ pubkey: senderPubkey, isSigner: true, isWritable: true },
16+
{ pubkey: recipientPubkey, isSigner: false, isWritable: true },
17+
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
18+
],
19+
programId,
20+
data,
21+
});
22+
}
Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,43 @@
11
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";
93
import { start } from "solana-bankrun";
4+
import { createTransferInstruction } from "./instruction";
105

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+
}
1343
});

0 commit comments

Comments
 (0)