-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-ping-transaction.ts
61 lines (45 loc) · 1.65 KB
/
send-ping-transaction.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
import * as web3 from "@solana/web3.js";
import "dotenv/config"
import { getKeypairFromEnvironment, airdropIfRequired } from "@solana-developers/helpers";
const PING_PROGRAM_ADDRESS = new web3.PublicKey('ChT1B39WKLS8qUrkLvFDXMhEJ4F1XZzwUNHUt4AU9aVa');
const PING_PROGRAM_DATA_ADDRESS = new web3.PublicKey('Ah9K7dQ8EHaZqcAsgBW8w37yN2eAy3koFmUn4x3CJtod');
const payer = getKeypairFromEnvironment('SECRET_KEY')
const connection = new web3.Connection(web3.clusterApiUrl('devnet'))
async function checkAndAirdrop() {
try {
const newBalance = await airdropIfRequired(
connection,
payer.publicKey,
1 * web3.LAMPORTS_PER_SOL, // 分配1个SOL
0.5 * web3.LAMPORTS_PER_SOL // 检查余额是否足够支付0.5个SOL
);
console.log('Airdrop successful:', newBalance);
} catch (error) {
console.error('Failed to perform airdrop:', error);
}
}
// 调用异步函数
checkAndAirdrop();
const transaction = new web3.Transaction()
const programId = new web3.PublicKey(PING_PROGRAM_ADDRESS)
const pingProgramDataId = new web3.PublicKey(PING_PROGRAM_DATA_ADDRESS)
const transaction = new web3.Transaction()
const programId = new web3.PublicKey(PING_PROGRAM_ADDRESS)
const pingProgramDataId = new web3.PublicKey(PING_PROGRAM_DATA_ADDRESS)
const instruction = new web3.TransactionInstruction({
keys: [
{
pubkey: pingProgramDataId,
isSigner: false,
isWritable: true
},
],
programId
})
transaction.add(instruction)
const signature = await web3.sendAndConfirmTransaction(
connection,
transaction,
[payer]
)
console.log(`✅ Transaction completed! Signature is ${signature}`)