forked from brianobot/TURBIN3-Q1-25
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransfer.ts
More file actions
54 lines (42 loc) · 1.93 KB
/
Copy pathtransfer.ts
File metadata and controls
54 lines (42 loc) · 1.93 KB
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));
// Define our Turbin3 public key
const to = new PublicKey("4gTWiPwC7AHdsu6BtySRd9KvEZVJmhQJRkB9rNH2P1Kj");
//Create a Solana devnet connection
const connection = new Connection("https://api.devnet.solana.com");
(async () => {
try {
// Get balance of dev wallet
const balance = await connection.getBalance(from.publicKey)
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: from.publicKey,
toPubkey: to,
lamports: balance,
})
);
transaction.recentBlockhash = (await connection.getLatestBlockhash('confirmed')).blockhash;
transaction.feePayer = from.publicKey;
// Calculate exact fee rate to transfer entire SOL amount out of account minus fees
const fee = (await connection.getFeeForMessage(transaction.compileMessage(), 'confirmed')).value || 0;
// Remove our transfer instruction to replace it
transaction.instructions.pop();
// Now add the instruction back with correct amount of lamports
transaction.add(
SystemProgram.transfer({
fromPubkey: from.publicKey,
toPubkey: to,
lamports: balance - fee,
})
);
// Sign transaction, broadcast, and confirm
const signature = await sendAndConfirmTransaction(
connection,
transaction,
[from]
);
console.log(`Success! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);
} catch(e) {
console.error(`Oops, something went wrong: ${e}`)
}})();