1- import { Blockhash , Keypair , PublicKey , SystemProgram , Transaction , TransactionInstruction } from '@solana/web3.js' ;
2- import { BN } from 'bn.js' ;
3- import * as borsh from 'borsh' ;
4- import { assert , expect } from 'chai' ;
5- import { describe , test } from 'mocha' ;
6- import { BanksClient , ProgramTestContext , start } from 'solana-bankrun' ;
1+ import {
2+ Blockhash ,
3+ Keypair ,
4+ PublicKey ,
5+ SystemProgram ,
6+ Transaction ,
7+ TransactionInstruction ,
8+ } from "@solana/web3.js" ;
9+ import { BN } from "bn.js" ;
10+ import * as borsh from "borsh" ;
11+ import { assert , expect } from "chai" ;
12+ import { describe , test } from "mocha" ;
13+ import { BanksClient , ProgramTestContext , start } from "solana-bankrun" ;
714
815// This is a helper class to assign properties to the class
916class Assignable {
@@ -14,10 +21,10 @@ class Assignable {
1421 }
1522}
1623
17- enum MyInstruction {
18- CreateFav = 0 ,
19- GetFav = 1 ,
20- }
24+ const MyInstruction = {
25+ CreateFav : 0 ,
26+ GetFav : 1 ,
27+ } as const ;
2128
2229class CreateFav extends Assignable {
2330 number : number ;
@@ -33,11 +40,11 @@ class CreateFav extends Assignable {
3340 return borsh . deserialize (
3441 {
3542 struct : {
36- number : ' u64' ,
37- color : ' string' ,
43+ number : " u64" ,
44+ color : " string" ,
3845 hobbies : {
3946 array : {
40- type : ' string' ,
47+ type : " string" ,
4148 } ,
4249 } ,
4350 } ,
@@ -48,12 +55,12 @@ class CreateFav extends Assignable {
4855}
4956const CreateNewAccountSchema = {
5057 struct : {
51- instruction : 'u8' ,
52- number : ' u64' ,
53- color : ' string' ,
58+ instruction : "u8" ,
59+ number : " u64" ,
60+ color : " string" ,
5461 hobbies : {
5562 array : {
56- type : ' string' ,
63+ type : " string" ,
5764 } ,
5865 } ,
5966 } ,
@@ -66,11 +73,11 @@ class GetFav extends Assignable {
6673}
6774const GetFavSchema = {
6875 struct : {
69- instruction : 'u8' ,
76+ instruction : "u8" ,
7077 } ,
7178} ;
7279
73- describe ( ' Favorites Solana Native' , ( ) => {
80+ describe ( " Favorites Solana Native" , ( ) => {
7481 // Randomly generate the program keypair and load the program to solana-bankrun
7582 const programId = PublicKey . unique ( ) ;
7683
@@ -80,16 +87,24 @@ describe('Favorites Solana Native', () => {
8087 let blockhash : Blockhash ;
8188
8289 beforeEach ( async ( ) => {
83- context = await start ( [ { name : ' favorites_native' , programId } ] , [ ] ) ;
90+ context = await start ( [ { name : " favorites_native" , programId } ] , [ ] ) ;
8491 client = context . banksClient ;
8592 // Get the payer keypair from the context, this will be used to sign transactions with enough lamports
8693 payer = context . payer ;
8794 blockhash = context . lastBlockhash ;
8895 } ) ;
8996
90- test ( 'Set the favorite pda and cross-check the updated data' , async ( ) => {
91- const favoritesPda = PublicKey . findProgramAddressSync ( [ Buffer . from ( 'favorite' ) , payer . publicKey . toBuffer ( ) ] , programId ) [ 0 ] ;
92- const favData = { instruction : MyInstruction . CreateFav , number : 42 , color : 'blue' , hobbies : [ 'coding' , 'reading' , 'traveling' ] } ;
97+ test ( "Set the favorite pda and cross-check the updated data" , async ( ) => {
98+ const favoritesPda = PublicKey . findProgramAddressSync (
99+ [ Buffer . from ( "favorite" ) , payer . publicKey . toBuffer ( ) ] ,
100+ programId ,
101+ ) [ 0 ] ;
102+ const favData = {
103+ instruction : MyInstruction . CreateFav ,
104+ number : 42 ,
105+ color : "blue" ,
106+ hobbies : [ "coding" , "reading" , "traveling" ] ,
107+ } ;
93108 const favorites = new CreateFav ( favData ) ;
94109
95110 const ix = new TransactionInstruction ( {
@@ -115,17 +130,27 @@ describe('Favorites Solana Native', () => {
115130
116131 const favoritesData = CreateFav . fromBuffer ( data ) ;
117132
118- console . log ( ' Deserialized data:' , favoritesData ) ;
133+ console . log ( " Deserialized data:" , favoritesData ) ;
119134
120- expect ( new BN ( favoritesData . number as any , 'le' ) . toNumber ( ) ) . to . equal ( favData . number ) ;
135+ expect ( new BN ( favoritesData . number as any , "le" ) . toNumber ( ) ) . to . equal (
136+ favData . number ,
137+ ) ;
121138 expect ( favoritesData . color ) . to . equal ( favData . color ) ;
122139 expect ( favoritesData . hobbies ) . to . deep . equal ( favData . hobbies ) ;
123140 } ) ;
124141
125142 test ( "Check if the test fails if the pda seeds aren't same" , async ( ) => {
126143 // We put the wrong seeds knowingly to see if the test fails because of checks
127- const favoritesPda = PublicKey . findProgramAddressSync ( [ Buffer . from ( 'favorite' ) , payer . publicKey . toBuffer ( ) ] , programId ) [ 0 ] ;
128- const favData = { instruction : MyInstruction . CreateFav , number : 42 , color : 'blue' , hobbies : [ 'coding' , 'reading' , 'traveling' ] } ;
144+ const favoritesPda = PublicKey . findProgramAddressSync (
145+ [ Buffer . from ( "favorite" ) , payer . publicKey . toBuffer ( ) ] ,
146+ programId ,
147+ ) [ 0 ] ;
148+ const favData = {
149+ instruction : MyInstruction . CreateFav ,
150+ number : 42 ,
151+ color : "blue" ,
152+ hobbies : [ "coding" , "reading" , "traveling" ] ,
153+ } ;
129154 const favorites = new CreateFav ( favData ) ;
130155
131156 const ix = new TransactionInstruction ( {
@@ -146,16 +171,24 @@ describe('Favorites Solana Native', () => {
146171 tx . recentBlockhash = blockhash ;
147172 try {
148173 await client . processTransaction ( tx ) ;
149- console . error ( ' Expected the test to fail' ) ;
174+ console . error ( " Expected the test to fail" ) ;
150175 } catch ( _err ) {
151176 assert ( true ) ;
152177 }
153178 } ) ;
154179
155- test ( ' Get the favorite pda and cross-check the data' , async ( ) => {
180+ test ( " Get the favorite pda and cross-check the data" , async ( ) => {
156181 // Creating a new account with payer's pubkey
157- const favoritesPda = PublicKey . findProgramAddressSync ( [ Buffer . from ( 'favorite' ) , payer . publicKey . toBuffer ( ) ] , programId ) [ 0 ] ;
158- const favData = { instruction : MyInstruction . CreateFav , number : 42 , color : 'hazel' , hobbies : [ 'singing' , 'dancing' , 'skydiving' ] } ;
182+ const favoritesPda = PublicKey . findProgramAddressSync (
183+ [ Buffer . from ( "favorite" ) , payer . publicKey . toBuffer ( ) ] ,
184+ programId ,
185+ ) [ 0 ] ;
186+ const favData = {
187+ instruction : MyInstruction . CreateFav ,
188+ number : 42 ,
189+ color : "hazel" ,
190+ hobbies : [ "singing" , "dancing" , "skydiving" ] ,
191+ } ;
159192 const favorites = new CreateFav ( favData ) ;
160193
161194 const ix = new TransactionInstruction ( {
0 commit comments