@@ -8,86 +8,61 @@ import {
88 sendAndConfirmTransaction ,
99} from "@solana/web3.js" ;
1010import { 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