-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathuse-counter-program.ts
64 lines (51 loc) · 1.75 KB
/
use-counter-program.ts
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
// Adapted from https://github.com/Unboxed-Software/solana-ping-client
import {
Connection,
LAMPORTS_PER_SOL,
PublicKey,
Transaction,
TransactionInstruction,
clusterApiUrl,
sendAndConfirmTransaction,
} from "@solana/web3.js";
import * as dotenv from "dotenv";
import { getKeypairFromEnvironment } from "@solana-developers/helpers";
const CLUSTER_NAME = "devnet";
const PING_PROGRAM_ADDRESS = new PublicKey(
"ChT1B39WKLS8qUrkLvFDXMhEJ4F1XZzwUNHUt4AU9aVa"
);
const PING_PROGRAM_DATA_ADDRESS = new PublicKey(
"Ah9K7dQ8EHaZqcAsgBW8w37yN2eAy3koFmUn4x3CJtod"
);
dotenv.config();
const payer = getKeypairFromEnvironment("SECRET_KEY");
console.log(`🔑 Loaded keypair ${payer.publicKey.toBase58()}!`);
const connection = new Connection(clusterApiUrl(CLUSTER_NAME));
console.log(`⚡️ Connected to Solana ${CLUSTER_NAME} cluster!`);
// Note: may not work first time as `await` returns before Lamports are confirmed.
// Being fixed in https://github.com/solana-labs/solana-web3.js/issues/1579
await connection.requestAirdrop(payer.publicKey, LAMPORTS_PER_SOL * 1);
console.log(`💸 Got some ${CLUSTER_NAME} lamports!`);
const transaction = new Transaction();
const programId = new PublicKey(PING_PROGRAM_ADDRESS);
const pingProgramDataId = new PublicKey(PING_PROGRAM_DATA_ADDRESS);
const instruction = new TransactionInstruction({
keys: [
{
pubkey: pingProgramDataId,
isSigner: false,
isWritable: true,
},
],
programId,
});
transaction.add(instruction);
const signature = await sendAndConfirmTransaction(connection, transaction, [
payer,
]);
console.log(
`✅ Transaction completed! You can view your transaction on the Solana Explorer at:`
);
console.log(
`https://explorer.solana.com/tx/${signature}?cluster=${CLUSTER_NAME}`
);