-
Notifications
You must be signed in to change notification settings - Fork 0
/
index2.js
80 lines (65 loc) · 2.34 KB
/
index2.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
79
// Import Solana web3 functionalities
const {
Connection,
PublicKey,
clusterApiUrl,
Keypair,
LAMPORTS_PER_SOL,
Transaction,
SystemProgram,
sendAndConfirmTransaction
} = require("@solana/web3.js");
// Making a keypair and getting the private key
const newPair = Keypair.generate();
console.log("Below is what you will paste into your code:\n")
console.log(newPair.secretKey);
const DEMO_FROM_SECRET_KEY = new Uint8Array(
// paste your secret key inside this empty array
// then uncomment transferSol() at the bottom
[
]
);
const transferSol = async() => {
const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
// Get Keypair from Secret Key
var from = Keypair.fromSecretKey(DEMO_FROM_SECRET_KEY);
// (Optional) - Other things you can try:
// 1) Form array from userSecretKey
// const from = Keypair.fromSecretKey(Uint8Array.from(userSecretKey));
// 2) Make a new Keypair (starts with 0 SOL)
// const from = Keypair.generate();
// Generate another Keypair (account we'll be sending to)
const to = Keypair.generate();
// Aidrop 2 SOL to Sender wallet
console.log("Airdopping some SOL to Sender wallet!");
const fromAirDropSignature = await connection.requestAirdrop(
new PublicKey(from.publicKey),
2 * LAMPORTS_PER_SOL
);
// Latest blockhash (unique identifer of the block) of the cluster
let latestBlockHash = await connection.getLatestBlockhash();
// Confirm transaction using the last valid block height (refers to its time)
// to check for transaction expiration
await connection.confirmTransaction({
blockhash: latestBlockHash.blockhash,
lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
signature: fromAirDropSignature
});
console.log("Airdrop completed for the Sender account");
// Send money from "from" wallet and into "to" wallet
var transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: from.publicKey,
toPubkey: to.publicKey,
lamports: LAMPORTS_PER_SOL / 100
})
);
// Sign transaction
var signature = await sendAndConfirmTransaction(
connection,
transaction,
[from]
);
console.log('Signature is', signature);
}
//transferSol();