11import { Buffer } from 'node:buffer' ;
2+ import { readFileSync } from 'node:fs' ;
3+ import { homedir } from 'node:os' ;
24import { Connection , Keypair , SystemProgram , Transaction , TransactionInstruction , sendAndConfirmTransaction } from '@solana/web3.js' ;
35import * as borsh from 'borsh' ;
6+ import { start } from 'solana-bankrun' ;
7+
48
59function createKeypairFromFile ( path : string ) : Keypair {
6- return Keypair . fromSecretKey ( Buffer . from ( JSON . parse ( require ( 'node:fs' ) . readFileSync ( path , 'utf-8' ) ) ) ) ;
10+ return Keypair . fromSecretKey ( Buffer . from ( JSON . parse ( readFileSync ( path , 'utf-8' ) ) ) ) ;
711}
812
9- describe ( 'CPI Example' , ( ) => {
10- const connection = new Connection ( 'http://localhost:8899' , 'confirmed' ) ;
11- const payer = createKeypairFromFile ( `${ require ( 'node:os' ) . homedir ( ) } /.config/solana/id.json` ) ;
12- const hand = createKeypairFromFile ( './target/so/hand-keypair.json' ) ;
13- const lever = createKeypairFromFile ( './target/so/lever-keypair.json' ) ;
13+ describe ( 'CPI Example' , async ( ) => {
14+ //const connection = new Connection('http://localhost:8899', 'confirmed');
15+
16+ const hand = createKeypairFromFile ( './target/deploy/cross_program_invocatio_native_hand-keypair.json' ) ;
17+ const lever = createKeypairFromFile ( './target/deploy/cross_program_invocatio_native_lever-keypair.json' ) ;
18+
19+
20+ const context = await start ( [
21+ { name : 'cross_program_invocatio_native_hand' , programId : hand . publicKey } ,
22+ { name : 'cross_program_invocatio_native_lever' , programId : lever . publicKey }
23+ ] , [ ] )
24+
25+ const client = context . banksClient ;
26+ const payer = context . payer ;
1427
1528 class Assignable {
16- constructor ( properties ) {
29+ constructor ( properties : any ) {
1730 for ( const [ key , value ] of Object . entries ( properties ) ) {
18- this [ key ] = value ;
31+ ( this as any ) [ key ] = value ;
1932 }
2033 }
2134 }
2235
2336 class PowerStatus extends Assignable {
37+ is_on ! : number ;
38+
2439 toBuffer ( ) {
2540 return Buffer . from ( borsh . serialize ( PowerStatusSchema , this ) ) ;
2641 }
2742 }
2843 const PowerStatusSchema = new Map ( [ [ PowerStatus , { kind : 'struct' , fields : [ [ 'is_on' , 'u8' ] ] } ] ] ) ;
2944
3045 class SetPowerStatus extends Assignable {
46+ name ! : string ;
47+
3148 toBuffer ( ) {
3249 return Buffer . from ( borsh . serialize ( SetPowerStatusSchema , this ) ) ;
3350 }
@@ -47,7 +64,12 @@ describe('CPI Example', () => {
4764 data : new PowerStatus ( { is_on : true } ) . toBuffer ( ) ,
4865 } ) ;
4966
50- await sendAndConfirmTransaction ( connection , new Transaction ( ) . add ( ix ) , [ payer , powerAccount ] ) ;
67+
68+ const tx = new Transaction ( ) ;
69+ tx . recentBlockhash = context . lastBlockhash ;
70+ tx . add ( ix ) . sign ( payer ) ;
71+
72+ await client . processTransaction ( tx ) ;
5173 } ) ;
5274
5375 it ( 'Pull the lever!' , async ( ) => {
@@ -60,7 +82,11 @@ describe('CPI Example', () => {
6082 data : new SetPowerStatus ( { name : 'Chris' } ) . toBuffer ( ) ,
6183 } ) ;
6284
63- await sendAndConfirmTransaction ( connection , new Transaction ( ) . add ( ix ) , [ payer ] ) ;
85+ const tx = new Transaction ( ) ;
86+ tx . recentBlockhash = context . lastBlockhash ;
87+ tx . add ( ix ) . sign ( payer ) ;
88+
89+ await client . processTransaction ( tx ) ;
6490 } ) ;
6591
6692 it ( 'Pull it again!' , async ( ) => {
@@ -73,6 +99,11 @@ describe('CPI Example', () => {
7399 data : new SetPowerStatus ( { name : 'Ashley' } ) . toBuffer ( ) ,
74100 } ) ;
75101
76- await sendAndConfirmTransaction ( connection , new Transaction ( ) . add ( ix ) , [ payer ] ) ;
102+
103+ const tx = new Transaction ( ) ;
104+ tx . recentBlockhash = context . lastBlockhash ;
105+ tx . add ( ix ) . sign ( payer ) ;
106+
107+ await client . processTransaction ( tx ) ;
77108 } ) ;
78109} ) ;
0 commit comments