1+ import {
2+ Connection ,
3+ Keypair ,
4+ LAMPORTS_PER_SOL ,
5+ Transaction ,
6+ TransactionInstruction ,
7+ sendAndConfirmTransaction ,
8+ SystemProgram
9+ } from '@solana/web3.js' ;
10+ import {
11+ bignum
12+ } from '@metaplex-foundation/beet' ;
13+ import { assert } from 'chai' ;
14+ import { BN } from 'bn.js' ;
15+
16+ import {
17+ createIncrementInstruction ,
18+ Counter ,
19+ PROGRAM_ID ,
20+ } from '../ts' ;
21+
22+ function convertBignumToNumber ( bignum : bignum ) : number {
23+ return new BN ( bignum ) . toNumber ( ) ;
24+ }
25+
26+ describe ( "Counter Solana Native" , ( ) => {
27+ const connection = new Connection ( "http://localhost:8899" ) ;
28+
29+ it ( "Test allocate counter + increment tx" , async ( ) => {
30+ // Randomly generate our wallet
31+ const payerKeypair = Keypair . generate ( ) ;
32+ const payer = payerKeypair . publicKey ;
33+
34+ // Randomly generate the account key
35+ // to sign for setting up the Counter state
36+ const counterKeypair = Keypair . generate ( ) ;
37+ const counter = counterKeypair . publicKey ;
38+
39+ // Airdrop our wallet 1 Sol
40+ await connection . requestAirdrop ( payer , LAMPORTS_PER_SOL ) ;
41+
42+ // Create a TransactionInstruction to interact with our counter program
43+ const allocIx : TransactionInstruction = SystemProgram . createAccount ( {
44+ fromPubkey : payer ,
45+ newAccountPubkey : counter ,
46+ lamports : await connection . getMinimumBalanceForRentExemption ( Counter . byteSize ) ,
47+ space : Counter . byteSize ,
48+ programId : PROGRAM_ID
49+ } )
50+ const incrementIx : TransactionInstruction = createIncrementInstruction ( { counter } ) ;
51+ let tx = new Transaction ( ) . add ( allocIx ) . add ( incrementIx ) ;
52+
53+ // Explicitly set the feePayer to be our wallet (this is set to first signer by default)
54+ tx . feePayer = payer ;
55+
56+ // Fetch a "timestamp" so validators know this is a recent transaction
57+ tx . recentBlockhash = ( await connection . getLatestBlockhash ( 'confirmed' ) ) . blockhash ;
58+
59+ // Send transaction to network (local network)
60+ await sendAndConfirmTransaction (
61+ connection ,
62+ tx ,
63+ [ payerKeypair , counterKeypair ] ,
64+ { skipPreflight : true , commitment : 'confirmed' }
65+ ) ;
66+
67+ // Get the counter account info from network
68+ let count = ( await Counter . fromAccountAddress ( connection , counter ) ) . count ;
69+ assert ( ( new BN ( count ) ) . toNumber ( ) === 1 , "Expected count to have been 1" ) ;
70+ console . log ( `[alloc+increment] count is: ${ count } ` ) ;
71+ } ) ;
72+ it ( "Test allocate tx and increment tx" , async ( ) => {
73+ const payerKeypair = Keypair . generate ( ) ;
74+ const payer = payerKeypair . publicKey ;
75+
76+ const counterKeypair = Keypair . generate ( ) ;
77+ const counter = counterKeypair . publicKey ;
78+
79+ await connection . requestAirdrop ( payer , LAMPORTS_PER_SOL ) ;
80+
81+ // Check allocate tx
82+ const allocIx : TransactionInstruction = SystemProgram . createAccount ( {
83+ fromPubkey : payer ,
84+ newAccountPubkey : counter ,
85+ lamports : await connection . getMinimumBalanceForRentExemption ( Counter . byteSize ) ,
86+ space : Counter . byteSize ,
87+ programId : PROGRAM_ID
88+ } )
89+ let tx = new Transaction ( ) . add ( allocIx ) ;
90+ tx . feePayer = payer ;
91+ tx . recentBlockhash = ( await connection . getLatestBlockhash ( 'confirmed' ) ) . blockhash ;
92+ await sendAndConfirmTransaction (
93+ connection ,
94+ tx ,
95+ [ payerKeypair , counterKeypair ] ,
96+ { skipPreflight : true , commitment : 'confirmed' }
97+ ) ;
98+
99+ let count = ( await Counter . fromAccountAddress ( connection , counter ) ) . count ;
100+ assert ( convertBignumToNumber ( count ) === 0 , "Expected count to have been 0" ) ;
101+ console . log ( `[allocate] count is: ${ count } ` ) ;
102+
103+ // Check increment tx
104+ const incrementIx : TransactionInstruction = createIncrementInstruction ( { counter } ) ;
105+ tx = new Transaction ( ) . add ( incrementIx ) ;
106+ tx . feePayer = payer ;
107+ tx . recentBlockhash = ( await connection . getLatestBlockhash ( 'confirmed' ) ) . blockhash ;
108+ await sendAndConfirmTransaction (
109+ connection ,
110+ tx ,
111+ [ payerKeypair ] ,
112+ { skipPreflight : true , commitment : 'confirmed' }
113+ ) ;
114+
115+ count = ( await Counter . fromAccountAddress ( connection , counter ) ) . count ;
116+ assert ( convertBignumToNumber ( count ) === 1 , "Expected count to have been 1" ) ;
117+ console . log ( `[increment] count is: ${ count } ` ) ;
118+ } )
119+ } )
0 commit comments