1
1
import { Buffer } from 'node:buffer' ;
2
+ import { readFileSync } from 'node:fs' ;
3
+ import { homedir } from 'node:os' ;
2
4
import { Connection , Keypair , SystemProgram , Transaction , TransactionInstruction , sendAndConfirmTransaction } from '@solana/web3.js' ;
3
5
import * as borsh from 'borsh' ;
6
+ import { start } from 'solana-bankrun' ;
7
+
4
8
5
9
function 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' ) ) ) ) ;
7
11
}
8
12
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 ;
14
27
15
28
class Assignable {
16
- constructor ( properties ) {
29
+ constructor ( properties : any ) {
17
30
for ( const [ key , value ] of Object . entries ( properties ) ) {
18
- this [ key ] = value ;
31
+ ( this as any ) [ key ] = value ;
19
32
}
20
33
}
21
34
}
22
35
23
36
class PowerStatus extends Assignable {
37
+ is_on ! : number ;
38
+
24
39
toBuffer ( ) {
25
40
return Buffer . from ( borsh . serialize ( PowerStatusSchema , this ) ) ;
26
41
}
27
42
}
28
43
const PowerStatusSchema = new Map ( [ [ PowerStatus , { kind : 'struct' , fields : [ [ 'is_on' , 'u8' ] ] } ] ] ) ;
29
44
30
45
class SetPowerStatus extends Assignable {
46
+ name ! : string ;
47
+
31
48
toBuffer ( ) {
32
49
return Buffer . from ( borsh . serialize ( SetPowerStatusSchema , this ) ) ;
33
50
}
@@ -47,7 +64,12 @@ describe('CPI Example', () => {
47
64
data : new PowerStatus ( { is_on : true } ) . toBuffer ( ) ,
48
65
} ) ;
49
66
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 ) ;
51
73
} ) ;
52
74
53
75
it ( 'Pull the lever!' , async ( ) => {
@@ -60,7 +82,11 @@ describe('CPI Example', () => {
60
82
data : new SetPowerStatus ( { name : 'Chris' } ) . toBuffer ( ) ,
61
83
} ) ;
62
84
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 ) ;
64
90
} ) ;
65
91
66
92
it ( 'Pull it again!' , async ( ) => {
@@ -73,6 +99,11 @@ describe('CPI Example', () => {
73
99
data : new SetPowerStatus ( { name : 'Ashley' } ) . toBuffer ( ) ,
74
100
} ) ;
75
101
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 ) ;
77
108
} ) ;
78
109
} ) ;
0 commit comments