-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (40 loc) · 1.24 KB
/
index.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
const {
Connection,
PublicKey,
Keypair,
clusterApiUrl,
LAMPORTS_PER_SOL
} = require('@solana/web3.js');
const wallet = Keypair.generate();
const publicKey = wallet.publicKey;
const secretKey = wallet.secretKey;
const getWalletBalance = async () => {
try {
const connection = new Connection(clusterApiUrl('devnet'), 'confirmed');
const balance = await connection.getBalance(publicKey);
console.log("Balance: ", balance / LAMPORTS_PER_SOL);
} catch (error) {
console.error(error);
}
}
const airDropSol = async () => {
try {
const connection = new Connection(clusterApiUrl('devnet'), 'confirmed');
const signature = await connection.requestAirdrop(publicKey, 2 * LAMPORTS_PER_SOL);
const latestBlockhash = await connection.getLatestBlockhash();
await connection.confirmTransaction({
blockhash: latestBlockhash.blockhash,
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
signature: signature
});
console.log("Airdrop successful");
} catch (error) {
console.error(error);
}
}
const main = async () => {
await getWalletBalance();
await airDropSol();
await getWalletBalance();
}
main();