Skip to content

Commit de35e51

Browse files
committed
fix: add solana-bankrun to testcase
1 parent 0cb7f3a commit de35e51

File tree

3 files changed

+111
-12
lines changed

3 files changed

+111
-12
lines changed

basics/cross-program-invocation/native/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"@solana/web3.js": "^1.47.3",
1010
"borsh": "^0.7.0",
1111
"buffer": "^6.0.3",
12-
"fs": "^0.0.1-security"
12+
"fs": "^0.0.1-security",
13+
"solana-bankrun": "^0.4.0"
1314
},
1415
"devDependencies": {
1516
"@types/bn.js": "^5.1.0",

basics/cross-program-invocation/native/pnpm-lock.yaml

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

basics/cross-program-invocation/native/tests/test.ts

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,50 @@
11
import { Buffer } from 'node:buffer';
2+
import { readFileSync } from 'node:fs';
3+
import { homedir } from 'node:os';
24
import { Connection, Keypair, SystemProgram, Transaction, TransactionInstruction, sendAndConfirmTransaction } from '@solana/web3.js';
35
import * as borsh from 'borsh';
6+
import { start } from 'solana-bankrun';
7+
48

59
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'))));
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

Comments
 (0)