forked from solana-foundation/program-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
109 lines (85 loc) · 3.38 KB
/
Copy pathtest.ts
File metadata and controls
109 lines (85 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { Buffer } from 'node:buffer';
import { readFileSync } from 'node:fs';
import { homedir } from 'node:os';
import { Connection, Keypair, SystemProgram, Transaction, TransactionInstruction, sendAndConfirmTransaction } from '@solana/web3.js';
import * as borsh from 'borsh';
import { start } from 'solana-bankrun';
function createKeypairFromFile(path: string): Keypair {
return Keypair.fromSecretKey(Buffer.from(JSON.parse(readFileSync(path, 'utf-8'))));
}
describe('CPI Example', async () => {
//const connection = new Connection('http://localhost:8899', 'confirmed');
const hand = createKeypairFromFile('./target/deploy/cross_program_invocatio_native_hand-keypair.json');
const lever = createKeypairFromFile('./target/deploy/cross_program_invocatio_native_lever-keypair.json');
const context = await start([
{ name: 'cross_program_invocatio_native_hand', programId: hand.publicKey },
{ name: 'cross_program_invocatio_native_lever', programId: lever.publicKey }
], [])
const client = context.banksClient;
const payer = context.payer;
class Assignable {
constructor(properties: any) {
for (const [key, value] of Object.entries(properties)) {
(this as any)[key] = value;
}
}
}
class PowerStatus extends Assignable {
is_on!: number;
toBuffer() {
return Buffer.from(borsh.serialize(PowerStatusSchema, this));
}
}
const PowerStatusSchema = new Map([[PowerStatus, { kind: 'struct', fields: [['is_on', 'u8']] }]]);
class SetPowerStatus extends Assignable {
name!: string;
toBuffer() {
return Buffer.from(borsh.serialize(SetPowerStatusSchema, this));
}
}
const SetPowerStatusSchema = new Map([[SetPowerStatus, { kind: 'struct', fields: [['name', 'string']] }]]);
const powerAccount = Keypair.generate();
it('Initialize the lever!', async () => {
const ix = new TransactionInstruction({
keys: [
{ pubkey: powerAccount.publicKey, isSigner: true, isWritable: true },
{ pubkey: payer.publicKey, isSigner: true, isWritable: true },
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
],
programId: lever.publicKey,
data: new PowerStatus({ is_on: true }).toBuffer(),
});
const tx = new Transaction();
tx.recentBlockhash = context.lastBlockhash;
tx.add(ix).sign(payer);
await client.processTransaction(tx);
});
it('Pull the lever!', async () => {
const ix = new TransactionInstruction({
keys: [
{ pubkey: powerAccount.publicKey, isSigner: false, isWritable: true },
{ pubkey: lever.publicKey, isSigner: false, isWritable: false },
],
programId: hand.publicKey,
data: new SetPowerStatus({ name: 'Chris' }).toBuffer(),
});
const tx = new Transaction();
tx.recentBlockhash = context.lastBlockhash;
tx.add(ix).sign(payer);
await client.processTransaction(tx);
});
it('Pull it again!', async () => {
const ix = new TransactionInstruction({
keys: [
{ pubkey: powerAccount.publicKey, isSigner: false, isWritable: true },
{ pubkey: lever.publicKey, isSigner: false, isWritable: false },
],
programId: hand.publicKey,
data: new SetPowerStatus({ name: 'Ashley' }).toBuffer(),
});
const tx = new Transaction();
tx.recentBlockhash = context.lastBlockhash;
tx.add(ix).sign(payer);
await client.processTransaction(tx);
});
});