-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSayHi.js
78 lines (61 loc) · 2.35 KB
/
SayHi.js
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
const {
Connection,
Keypair,
Transaction,
SystemProgram,
LAMPORTS_PER_SOL,
PublicKey,
TransactionInstruction,
sendAndConfirmTransaction,
} = require("@solana/web3.js");
const { extractKeys } = require("./KeyExtractor");
const CONTRACT_ADDRESS = "<PUT_GENERATED_PROGRAM_ID_HERE>";
async function callGetMessage() {
const connection = new Connection('http://localhost:8899', 'confirmed');
const programId = new PublicKey(CONTRACT_ADDRESS)
const keyData = await extractKeys();
const wallet = keyData.keyPair;
// Client
console.log("My address:", wallet.publicKey.toString());
const balance = await connection.getBalance(wallet.publicKey);
console.log(`My balance: ${balance / LAMPORTS_PER_SOL} SOL`);
// get the metrics on the program
const accountInfo = await connection.getAccountInfo(programId);
if (accountInfo?.data) {
console.log(
`The size of Program ID ${CONTRACT_ADDRESS} is ${accountInfo?.data.length}`
);
} else {
console.log("No data found for the account");
}
const instruction = new TransactionInstruction({
keys: [], // Add account keys if required by the program
programId: programId,
data: Buffer.alloc(0), // No instruction data needed for getMessage()
});
const lamports = await connection.getMinimumBalanceForRentExemption(accountInfo?.data.length || 0);
const transaction = new Transaction().add(instruction);
transaction.feePayer = wallet.publicKey;
const txHash = await sendAndConfirmTransaction(
connection,
transaction,
[wallet]
);
console.log(`Transaction confirmed: ${txHash}`);
const transactionResponse = await connection.getTransaction(txHash);
//Take a look at the entire response. What the hell.
console.log(JSON.stringify(transactionResponse, null, 2));
// Extract the message from the transaction logs
const message = transactionResponse.meta.logMessages.find((log) =>
log.includes("Hi from Solana!")
);
if (message) {
console.log("Message:", message);
} else {
console.error("Failed to retrieve message");
}
}
// Call the function to interact with the Solana program
callGetMessage().catch((error) => {
console.error("Error:", error);
});