-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer.ts
56 lines (46 loc) · 1.42 KB
/
transfer.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
import {
Transaction,
SystemProgram,
Connection,
Keypair,
LAMPORTS_PER_SOL,
sendAndConfirmTransaction,
PublicKey,
} from "@solana/web3.js";
import wallet from "./dev-wallet.json";
const from = Keypair.fromSecretKey(new Uint8Array(wallet));
const to = new PublicKey("ErQh1bVN4ciHWNMZw5DmiE7md9E7G2HbBxm6tX3xqvoz");
const connection = new Connection("https://api.devnet.solana.com");
// const balance = await connection.getBalance(from.publicKey);
(async () => {
try {
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: from.publicKey,
toPubkey: to,
lamports: LAMPORTS_PER_SOL / 100,
})
);
transaction.recentBlockhash = (
await connection.getLatestBlockhash("confirmed")
).blockhash;
const balance = await connection.getBalance(from.publicKey);
transaction.feePayer = from.publicKey;
if (balance == 0) {
connection.requestAirdrop(from.publicKey, 2 * LAMPORTS_PER_SOL);
} else {
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: from.publicKey,
toPubkey: to,
lamports: balance,
})
);
}
transaction.feePayer = from.publicKey;
transaction;
}
catch (e) {
console.error(`Oops, something went wrong: ${e}`);
}
})();