Skip to content

Commit 839fe8f

Browse files
committed
refactor: update transfer-sol tests for improved clarity and functionality
* Renamed test suite to "Anchor: Transfer SOL" for better context. * Simplified recipient and payer account generation in tests. * Replaced balance checks with assertions for clarity. * Removed unused getBalances function to streamline the test code.
1 parent 4c08c57 commit 839fe8f

File tree

1 file changed

+23
-48
lines changed
  • basics/transfer-sol/anchor/tests

1 file changed

+23
-48
lines changed

basics/transfer-sol/anchor/tests/test.ts

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,86 +8,61 @@ import {
88
sendAndConfirmTransaction,
99
} from "@solana/web3.js";
1010
import { BN } from "bn.js";
11-
import type { TransferSol } from "../target/types/transfer_sol";
11+
import { assert } from "chai";
12+
import type { TransferSol } from "../target/types/transfer_sol.ts";
1213

13-
describe.skip("transfer-sol", () => {
14+
describe("Anchor: Transfer SOL", () => {
1415
const provider = anchor.AnchorProvider.env();
1516
anchor.setProvider(provider);
1617
const payer = provider.wallet as anchor.Wallet;
1718
const program = anchor.workspace.TransferSol as anchor.Program<TransferSol>;
1819

19-
// 1 SOL
20-
const transferAmount = 1 * LAMPORTS_PER_SOL;
21-
22-
// Generate a new keypair for the recipient
23-
const recipient = new Keypair();
24-
25-
// Generate a new keypair to create an account owned by our program
26-
const programOwnedAccount = new Keypair();
27-
2820
it("Transfer SOL with CPI", async () => {
29-
await getBalances(payer.publicKey, recipient.publicKey, "Beginning");
21+
const recipient = Keypair.generate();
3022

3123
await program.methods
32-
.transferSolWithCpi(new BN(transferAmount))
24+
.transferSolWithCpi(new BN(LAMPORTS_PER_SOL))
3325
.accounts({
3426
payer: payer.publicKey,
3527
recipient: recipient.publicKey,
3628
})
3729
.rpc();
3830

39-
await getBalances(payer.publicKey, recipient.publicKey, "Resulting");
31+
const recipientBalance = await provider.connection.getBalance(
32+
recipient.publicKey,
33+
);
34+
assert.equal(recipientBalance, LAMPORTS_PER_SOL);
4035
});
4136

42-
it("Create and fund account owned by our program", async () => {
43-
const instruction = SystemProgram.createAccount({
37+
it("Transfer SOL with Program", async () => {
38+
const payerAccount = Keypair.generate();
39+
const ix = SystemProgram.createAccount({
4440
fromPubkey: payer.publicKey,
45-
newAccountPubkey: programOwnedAccount.publicKey,
41+
newAccountPubkey: payerAccount.publicKey,
4642
space: 0,
47-
lamports: 1 * LAMPORTS_PER_SOL, // 1 SOL
43+
lamports: LAMPORTS_PER_SOL, // 1 SOL
4844
programId: program.programId, // Program Owner, our program's address
4945
});
5046

51-
const transaction = new Transaction().add(instruction);
47+
const transaction = new Transaction().add(ix);
5248

5349
await sendAndConfirmTransaction(provider.connection, transaction, [
5450
payer.payer,
55-
programOwnedAccount,
51+
payerAccount,
5652
]);
57-
});
58-
59-
it("Transfer SOL with Program", async () => {
60-
await getBalances(
61-
programOwnedAccount.publicKey,
62-
payer.publicKey,
63-
"Beginning",
64-
);
6553

54+
const recipientAccount = Keypair.generate();
6655
await program.methods
67-
.transferSolWithProgram(new BN(transferAmount))
56+
.transferSolWithProgram(new BN(LAMPORTS_PER_SOL))
6857
.accounts({
69-
payer: programOwnedAccount.publicKey,
70-
recipient: payer.publicKey,
58+
payer: payerAccount.publicKey,
59+
recipient: recipientAccount.publicKey,
7160
})
7261
.rpc();
7362

74-
await getBalances(
75-
programOwnedAccount.publicKey,
76-
payer.publicKey,
77-
"Resulting",
63+
const recipientBalance = await provider.connection.getBalance(
64+
recipientAccount.publicKey,
7865
);
66+
assert.equal(recipientBalance, LAMPORTS_PER_SOL);
7967
});
80-
81-
async function getBalances(
82-
payerPubkey: PublicKey,
83-
recipientPubkey: PublicKey,
84-
timeframe: string,
85-
) {
86-
const payerBalance = await provider.connection.getBalance(payerPubkey);
87-
const recipientBalance =
88-
await provider.connection.getBalance(recipientPubkey);
89-
console.log(`${timeframe} balances:`);
90-
console.log(` Payer: ${payerBalance / LAMPORTS_PER_SOL}`);
91-
console.log(` Recipient: ${recipientBalance / LAMPORTS_PER_SOL}`);
92-
}
9368
});

0 commit comments

Comments
 (0)