-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathtest.ts
More file actions
69 lines (60 loc) · 1.85 KB
/
test.ts
File metadata and controls
69 lines (60 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import * as anchor from "@coral-xyz/anchor";
import {
Keypair,
LAMPORTS_PER_SOL,
type PublicKey,
SystemProgram,
Transaction,
sendAndConfirmTransaction,
} from "@solana/web3.js";
import { BN } from "bn.js";
import { assert } from "chai";
import type { TransferSol } from "../target/types/transfer_sol.ts";
describe("Anchor: Transfer SOL", () => {
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
const payer = provider.wallet as anchor.Wallet;
const program = anchor.workspace.TransferSol as anchor.Program<TransferSol>;
it("Transfer SOL with CPI", async () => {
const recipient = Keypair.generate();
await program.methods
.transferSolWithCpi(new BN(LAMPORTS_PER_SOL))
.accounts({
payer: payer.publicKey,
recipient: recipient.publicKey,
})
.rpc();
const recipientBalance = await provider.connection.getBalance(
recipient.publicKey,
);
assert.equal(recipientBalance, LAMPORTS_PER_SOL);
});
it("Transfer SOL with Program", async () => {
const payerAccount = Keypair.generate();
const ix = SystemProgram.createAccount({
fromPubkey: payer.publicKey,
newAccountPubkey: payerAccount.publicKey,
space: 0,
lamports: LAMPORTS_PER_SOL, // 1 SOL
programId: program.programId, // Program Owner, our program's address
});
const transaction = new Transaction().add(ix);
await sendAndConfirmTransaction(provider.connection, transaction, [
payer.payer,
payerAccount,
]);
const recipientAccount = Keypair.generate();
await program.methods
.transferSolWithProgram(new BN(LAMPORTS_PER_SOL))
.accounts({
payer: payerAccount.publicKey,
recipient: recipientAccount.publicKey,
})
.signers([payerAccount])
.rpc();
const recipientBalance = await provider.connection.getBalance(
recipientAccount.publicKey,
);
assert.equal(recipientBalance, LAMPORTS_PER_SOL);
});
});