-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.ts
66 lines (54 loc) · 2.04 KB
/
index.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
62
63
64
65
66
import {
Connection,
PublicKey,
Keypair,
Transaction,
SystemProgram,
ComputeBudgetProgram,
sendAndConfirmTransaction
} from '@solana/web3.js';
import fs from "fs";
// 创建RPC连接
const connection = new Connection("https://api.mainnet-beta.solana.com", "confirmed");
// const connection = new Connection("https://mainnet-ams.chainbuff.com", "confirmed");
// 本地导入钱包
// const fromSecretKey = Uint8Array.from(JSON.parse(fs.readFileSync("wallet.json")));
const fromSecretKey = Uint8Array.from(JSON.parse(fs.readFileSync("web3xFMwEPrc92NeeXdAigni95NDnnd2NPuajTirao2.json")));
const fromWallet = Keypair.fromSecretKey(fromSecretKey);
async function main() {
// 创建交易
const transaction = new Transaction();
// CU价格
const computeUnitPriceInstruction = ComputeBudgetProgram.setComputeUnitPrice({
microLamports: 5
});
transaction.add(computeUnitPriceInstruction);
// CU数量
// const computeUnitLimitInstruction = ComputeBudgetProgram.setComputeUnitLimit({
// units: 500,
// });
// transaction.add(computeUnitLimitInstruction);
// 目标地址
const toAddress = new PublicKey('buffaAJKmNLao65TDTUGq8oB9HgxkfPLGqPMFQapotJ');
// 添加转账指令
const instruction1 = SystemProgram.transfer({
fromPubkey: fromWallet.publicKey,
toPubkey: toAddress,
lamports: 1000, // 1000 lamports
});
transaction.add(instruction1);
// // 添加转账指令
// const instruction2 = SystemProgram.transfer({
// fromPubkey: fromWallet.publicKey,
// toPubkey: toAddress,
// lamports: 1000, // 1000 lamports
// });
// transaction.add(instruction2);
// 模拟交易
const simulateResult = await connection.simulateTransaction(transaction, [fromWallet]);
console.log("模拟交易结果: ", simulateResult);
// 发送交易
const signature = await sendAndConfirmTransaction(connection, transaction, [fromWallet]);
console.log(`交易已发送: https://solscan.io/tx/${signature}`);
}
main();