four-flap-meme-sdk 1.5.55 → 1.5.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js +1 -1
  3. package/dist/sol/constants.d.ts +150 -0
  4. package/dist/sol/constants.js +188 -0
  5. package/dist/sol/dex/blockrazor/client.d.ts +51 -0
  6. package/dist/sol/dex/blockrazor/client.js +96 -0
  7. package/dist/sol/dex/blockrazor/constants.d.ts +34 -0
  8. package/dist/sol/dex/blockrazor/constants.js +55 -0
  9. package/dist/sol/dex/blockrazor/geyser.d.ts +128 -0
  10. package/dist/sol/dex/blockrazor/geyser.js +530 -0
  11. package/dist/sol/dex/blockrazor/index.d.ts +18 -0
  12. package/dist/sol/dex/blockrazor/index.js +23 -0
  13. package/dist/sol/dex/blockrazor/send.d.ts +135 -0
  14. package/dist/sol/dex/blockrazor/send.js +254 -0
  15. package/dist/sol/dex/blockrazor/types.d.ts +191 -0
  16. package/dist/sol/dex/blockrazor/types.js +5 -0
  17. package/dist/sol/dex/index.d.ts +10 -0
  18. package/dist/sol/dex/index.js +16 -0
  19. package/dist/sol/dex/jup/client.d.ts +33 -0
  20. package/dist/sol/dex/jup/client.js +110 -0
  21. package/dist/sol/dex/jup/index.d.ts +16 -0
  22. package/dist/sol/dex/jup/index.js +148 -0
  23. package/dist/sol/dex/jup/legacy.d.ts +623 -0
  24. package/dist/sol/dex/jup/legacy.js +416 -0
  25. package/dist/sol/dex/jup/lend.d.ts +640 -0
  26. package/dist/sol/dex/jup/lend.js +603 -0
  27. package/dist/sol/dex/jup/portfolio.d.ts +362 -0
  28. package/dist/sol/dex/jup/portfolio.js +367 -0
  29. package/dist/sol/dex/jup/price.d.ts +173 -0
  30. package/dist/sol/dex/jup/price.js +220 -0
  31. package/dist/sol/dex/jup/recurring.d.ts +437 -0
  32. package/dist/sol/dex/jup/recurring.js +320 -0
  33. package/dist/sol/dex/jup/send.d.ts +282 -0
  34. package/dist/sol/dex/jup/send.js +295 -0
  35. package/dist/sol/dex/jup/studio.d.ts +457 -0
  36. package/dist/sol/dex/jup/studio.js +488 -0
  37. package/dist/sol/dex/jup/tokens.d.ts +767 -0
  38. package/dist/sol/dex/jup/tokens.js +697 -0
  39. package/dist/sol/dex/jup/trigger.d.ts +511 -0
  40. package/dist/sol/dex/jup/trigger.js +397 -0
  41. package/dist/sol/dex/jup/types.d.ts +433 -0
  42. package/dist/sol/dex/jup/types.js +5 -0
  43. package/dist/sol/dex/jup/ultra.d.ts +646 -0
  44. package/dist/sol/dex/jup/ultra.js +853 -0
  45. package/dist/sol/dex/meteora/client.d.ts +76 -0
  46. package/dist/sol/dex/meteora/client.js +219 -0
  47. package/dist/sol/dex/meteora/damm-v1-bundle.d.ts +61 -0
  48. package/dist/sol/dex/meteora/damm-v1-bundle.js +112 -0
  49. package/dist/sol/dex/meteora/damm-v1.d.ts +118 -0
  50. package/dist/sol/dex/meteora/damm-v1.js +315 -0
  51. package/dist/sol/dex/meteora/damm-v2-bundle.d.ts +82 -0
  52. package/dist/sol/dex/meteora/damm-v2-bundle.js +242 -0
  53. package/dist/sol/dex/meteora/damm-v2.d.ts +172 -0
  54. package/dist/sol/dex/meteora/damm-v2.js +632 -0
  55. package/dist/sol/dex/meteora/dbc-bundle.d.ts +123 -0
  56. package/dist/sol/dex/meteora/dbc-bundle.js +304 -0
  57. package/dist/sol/dex/meteora/dbc.d.ts +192 -0
  58. package/dist/sol/dex/meteora/dbc.js +619 -0
  59. package/dist/sol/dex/meteora/dlmm-bundle.d.ts +39 -0
  60. package/dist/sol/dex/meteora/dlmm-bundle.js +189 -0
  61. package/dist/sol/dex/meteora/dlmm.d.ts +157 -0
  62. package/dist/sol/dex/meteora/dlmm.js +671 -0
  63. package/dist/sol/dex/meteora/index.d.ts +25 -0
  64. package/dist/sol/dex/meteora/index.js +65 -0
  65. package/dist/sol/dex/meteora/types.d.ts +787 -0
  66. package/dist/sol/dex/meteora/types.js +110 -0
  67. package/dist/sol/dex/orca/index.d.ts +10 -0
  68. package/dist/sol/dex/orca/index.js +16 -0
  69. package/dist/sol/dex/orca/orca-bundle.d.ts +41 -0
  70. package/dist/sol/dex/orca/orca-bundle.js +173 -0
  71. package/dist/sol/dex/orca/orca.d.ts +65 -0
  72. package/dist/sol/dex/orca/orca.js +474 -0
  73. package/dist/sol/dex/orca/types.d.ts +263 -0
  74. package/dist/sol/dex/orca/types.js +38 -0
  75. package/dist/sol/dex/orca/wavebreak-bundle.d.ts +34 -0
  76. package/dist/sol/dex/orca/wavebreak-bundle.js +198 -0
  77. package/dist/sol/dex/orca/wavebreak-types.d.ts +227 -0
  78. package/dist/sol/dex/orca/wavebreak-types.js +23 -0
  79. package/dist/sol/dex/orca/wavebreak.d.ts +78 -0
  80. package/dist/sol/dex/orca/wavebreak.js +497 -0
  81. package/dist/sol/dex/pump/index.d.ts +9 -0
  82. package/dist/sol/dex/pump/index.js +14 -0
  83. package/dist/sol/dex/pump/pump-bundle.d.ts +92 -0
  84. package/dist/sol/dex/pump/pump-bundle.js +383 -0
  85. package/dist/sol/dex/pump/pump-swap-bundle.d.ts +103 -0
  86. package/dist/sol/dex/pump/pump-swap-bundle.js +380 -0
  87. package/dist/sol/dex/pump/pump-swap.d.ts +46 -0
  88. package/dist/sol/dex/pump/pump-swap.js +199 -0
  89. package/dist/sol/dex/pump/pump.d.ts +35 -0
  90. package/dist/sol/dex/pump/pump.js +352 -0
  91. package/dist/sol/dex/pump/types.d.ts +215 -0
  92. package/dist/sol/dex/pump/types.js +5 -0
  93. package/dist/sol/dex/raydium/index.d.ts +8 -0
  94. package/dist/sol/dex/raydium/index.js +12 -0
  95. package/dist/sol/dex/raydium/launchlab.d.ts +68 -0
  96. package/dist/sol/dex/raydium/launchlab.js +210 -0
  97. package/dist/sol/dex/raydium/raydium-bundle.d.ts +64 -0
  98. package/dist/sol/dex/raydium/raydium-bundle.js +324 -0
  99. package/dist/sol/dex/raydium/raydium.d.ts +40 -0
  100. package/dist/sol/dex/raydium/raydium.js +366 -0
  101. package/dist/sol/dex/raydium/types.d.ts +240 -0
  102. package/dist/sol/dex/raydium/types.js +5 -0
  103. package/dist/sol/index.d.ts +11 -0
  104. package/dist/sol/index.js +18 -0
  105. package/dist/sol/jito/bundle.d.ts +90 -0
  106. package/dist/sol/jito/bundle.js +263 -0
  107. package/dist/sol/jito/index.d.ts +7 -0
  108. package/dist/sol/jito/index.js +7 -0
  109. package/dist/sol/jito/tip.d.ts +51 -0
  110. package/dist/sol/jito/tip.js +83 -0
  111. package/dist/sol/jito/types.d.ts +100 -0
  112. package/dist/sol/jito/types.js +5 -0
  113. package/dist/sol/nozomi/client.d.ts +63 -0
  114. package/dist/sol/nozomi/client.js +222 -0
  115. package/dist/sol/nozomi/index.d.ts +8 -0
  116. package/dist/sol/nozomi/index.js +8 -0
  117. package/dist/sol/nozomi/tip.d.ts +50 -0
  118. package/dist/sol/nozomi/tip.js +80 -0
  119. package/dist/sol/nozomi/types.d.ts +96 -0
  120. package/dist/sol/nozomi/types.js +5 -0
  121. package/dist/sol/token/create-complete.d.ts +115 -0
  122. package/dist/sol/token/create-complete.js +235 -0
  123. package/dist/sol/token/create-token.d.ts +57 -0
  124. package/dist/sol/token/create-token.js +230 -0
  125. package/dist/sol/token/index.d.ts +9 -0
  126. package/dist/sol/token/index.js +14 -0
  127. package/dist/sol/token/metadata-upload.d.ts +86 -0
  128. package/dist/sol/token/metadata-upload.js +173 -0
  129. package/dist/sol/token/metadata.d.ts +92 -0
  130. package/dist/sol/token/metadata.js +274 -0
  131. package/dist/sol/token/types.d.ts +153 -0
  132. package/dist/sol/token/types.js +5 -0
  133. package/dist/sol/types.d.ts +176 -0
  134. package/dist/sol/types.js +7 -0
  135. package/dist/sol/utils/balance.d.ts +160 -0
  136. package/dist/sol/utils/balance.js +638 -0
  137. package/dist/sol/utils/connection.d.ts +78 -0
  138. package/dist/sol/utils/connection.js +168 -0
  139. package/dist/sol/utils/index.d.ts +9 -0
  140. package/dist/sol/utils/index.js +9 -0
  141. package/dist/sol/utils/lp-inspect.d.ts +75 -0
  142. package/dist/sol/utils/lp-inspect.js +235 -0
  143. package/dist/sol/utils/transfer.d.ts +196 -0
  144. package/dist/sol/utils/transfer.js +307 -0
  145. package/dist/sol/utils/wallet.d.ts +107 -0
  146. package/dist/sol/utils/wallet.js +210 -0
  147. package/package.json +44 -5
  148. package/README.zh-CN.pdf +0 -0
  149. package/dist/flap/portal-bundle-merkle/encryption.d.ts +0 -16
  150. package/dist/flap/portal-bundle-merkle/encryption.js +0 -146
@@ -0,0 +1,196 @@
1
+ /**
2
+ * Solana SOL 转账功能
3
+ * SDK 只负责构建和签名交易,提交到后端服务器广播
4
+ * @module sol/utils/transfer
5
+ */
6
+ import { Connection, Keypair, PublicKey, Transaction, TransactionInstruction } from '@solana/web3.js';
7
+ /**
8
+ * 转账项
9
+ */
10
+ export interface TransferItem {
11
+ to: string;
12
+ amount: bigint;
13
+ }
14
+ /**
15
+ * 签名后的转账交易
16
+ */
17
+ export interface SignedTransferTransaction {
18
+ /** Base64 编码的交易数据 */
19
+ data: string;
20
+ /** 交易签名 */
21
+ signature: string;
22
+ /** 转账详情 */
23
+ transfers: TransferItem[];
24
+ /** blockhash */
25
+ recentBlockhash: string;
26
+ /** 最后有效区块高度 */
27
+ lastValidBlockHeight: number;
28
+ }
29
+ /**
30
+ * 批量签名结果
31
+ */
32
+ export interface BatchSignedTransfers {
33
+ /** 签名后的交易列表 */
34
+ transactions: SignedTransferTransaction[];
35
+ /** 总转账数 */
36
+ totalTransfers: number;
37
+ /** 总交易数 */
38
+ totalTransactions: number;
39
+ /** 创建时间 */
40
+ createdAt: number;
41
+ }
42
+ /** 单笔交易最大转账数量(受 1232 bytes 限制) */
43
+ export declare const MAX_TRANSFERS_PER_TX = 20;
44
+ /**
45
+ * 构建转账指令
46
+ * @param from 发送者公钥
47
+ * @param to 接收者地址
48
+ * @param amount lamports
49
+ */
50
+ export declare function buildTransferInstruction(from: PublicKey, to: string, amount: bigint): TransactionInstruction;
51
+ /**
52
+ * 构建批量转账指令
53
+ * @param from 发送者公钥
54
+ * @param transfers 转账列表
55
+ */
56
+ export declare function buildTransferInstructions(from: PublicKey, transfers: TransferItem[]): TransactionInstruction[];
57
+ /**
58
+ * 指令组(用于批量转账分组)
59
+ */
60
+ export interface InstructionGroup {
61
+ /** 该组的指令列表 */
62
+ instructions: TransactionInstruction[];
63
+ /** 该组的转账详情 */
64
+ transfers: TransferItem[];
65
+ /** 组索引 */
66
+ groupIndex: number;
67
+ /** 总组数 */
68
+ totalGroups: number;
69
+ }
70
+ /**
71
+ * 批量转账指令组结果
72
+ */
73
+ export interface BatchTransferInstructionGroups {
74
+ /** 指令组列表 */
75
+ groups: InstructionGroup[];
76
+ /** 总转账数 */
77
+ totalTransfers: number;
78
+ /** 总组数 */
79
+ totalGroups: number;
80
+ /** 发送者公钥 */
81
+ from: PublicKey;
82
+ }
83
+ /**
84
+ * 构建批量转账指令组(支持超过 20 个地址,自动分组)
85
+ *
86
+ * 用于 SOL 分发场景:一个地址向多个地址转账
87
+ * 自动按 20 个一组分组,每组可以打包成一笔交易
88
+ *
89
+ * @param from 发送者公钥
90
+ * @param transfers 转账列表(可以超过 20 个)
91
+ * @param groupSize 每组大小(默认 20,最大 20)
92
+ * @returns 分组后的指令组
93
+ *
94
+ * @example
95
+ * // 100 个地址分发
96
+ * const groups = buildBatchTransferInstructionGroups(
97
+ * wallet.publicKey,
98
+ * distributions.map(d => ({ to: d.address, amount: BigInt(d.lamports) }))
99
+ * );
100
+ *
101
+ * // 返回 5 个组,每组 20 个转账指令
102
+ * console.log(groups.totalGroups); // 5
103
+ *
104
+ * // 每组可以单独构建交易
105
+ * for (const group of groups.groups) {
106
+ * const tx = new Transaction();
107
+ * group.instructions.forEach(ix => tx.add(ix));
108
+ * // ... 签名并发送
109
+ * }
110
+ */
111
+ export declare function buildBatchTransferInstructionGroups(from: PublicKey, transfers: TransferItem[], groupSize?: number): BatchTransferInstructionGroups;
112
+ /**
113
+ * 从指令组构建交易(不签名)
114
+ * @param group 指令组
115
+ * @param blockhash 区块哈希
116
+ * @param feePayer 费用支付者
117
+ */
118
+ export declare function buildTransactionFromGroup(group: InstructionGroup, blockhash: string, feePayer: PublicKey): Transaction;
119
+ /**
120
+ * 从指令组构建并签名交易
121
+ * @param group 指令组
122
+ * @param blockhash 区块哈希
123
+ * @param signer 签名者
124
+ */
125
+ export declare function buildAndSignTransactionFromGroup(group: InstructionGroup, blockhash: string, signer: Keypair): Transaction;
126
+ /**
127
+ * 构建并签名单笔 SOL 转账
128
+ * @param connection Solana 连接
129
+ * @param from 发送者 Keypair
130
+ * @param to 接收者地址
131
+ * @param amount lamports
132
+ */
133
+ export declare function signTransferSOL(connection: Connection, from: Keypair, to: string, amount: bigint): Promise<SignedTransferTransaction>;
134
+ /**
135
+ * 构建并签名单笔 SOL 转账(使用 SOL 数量)
136
+ */
137
+ export declare function signTransferSOLAmount(connection: Connection, from: Keypair, to: string, solAmount: number): Promise<SignedTransferTransaction>;
138
+ /**
139
+ * 构建并签名批量转账(单笔交易,多个指令)
140
+ * 单笔交易最多约 20-30 个转账
141
+ * @param connection Solana 连接
142
+ * @param from 发送者 Keypair
143
+ * @param transfers 转账列表
144
+ */
145
+ export declare function signBatchTransferSOL(connection: Connection, from: Keypair, transfers: TransferItem[]): Promise<SignedTransferTransaction>;
146
+ /**
147
+ * 构建并签名批量转账(多笔交易)
148
+ * 自动分批处理大量转账
149
+ * @param connection Solana 连接
150
+ * @param from 发送者 Keypair
151
+ * @param transfers 转账列表
152
+ * @param batchSize 每批大小(默认 20)
153
+ */
154
+ export declare function signBatchTransferSOLMultiple(connection: Connection, from: Keypair, transfers: TransferItem[], batchSize?: number): Promise<BatchSignedTransfers>;
155
+ /**
156
+ * 多钱包转账项
157
+ */
158
+ export interface MultiWalletTransferItem {
159
+ from: Keypair;
160
+ to: string;
161
+ amount: bigint;
162
+ }
163
+ /**
164
+ * 签名多钱包转账(每个钱包一笔交易)
165
+ * 用于归集或分发场景
166
+ * @param connection Solana 连接
167
+ * @param transfers 转账列表
168
+ */
169
+ export declare function signMultiWalletTransfers(connection: Connection, transfers: MultiWalletTransferItem[]): Promise<BatchSignedTransfers>;
170
+ /**
171
+ * 归集 SOL(多个钱包转到一个地址)
172
+ * @param connection Solana 连接
173
+ * @param wallets 钱包列表
174
+ * @param toAddress 目标地址
175
+ * @param amounts 每个钱包转账金额(lamports),如果为空则转全部(保留 5000 lamports 作为手续费)
176
+ */
177
+ export declare function signCollectSOL(connection: Connection, wallets: Keypair[], toAddress: string, amounts?: bigint[]): Promise<BatchSignedTransfers>;
178
+ /**
179
+ * 分发 SOL(一个钱包转到多个地址)
180
+ * @param connection Solana 连接
181
+ * @param from 发送者 Keypair
182
+ * @param distributions 分发列表
183
+ */
184
+ export declare function signDistributeSOL(connection: Connection, from: Keypair, distributions: TransferItem[]): Promise<BatchSignedTransfers>;
185
+ /**
186
+ * 将签名结果转换为后端 API 格式
187
+ */
188
+ export declare function toBackendFormat(result: BatchSignedTransfers): {
189
+ transactions: Array<{
190
+ data: string;
191
+ signature: string;
192
+ }>;
193
+ totalTransfers: number;
194
+ totalTransactions: number;
195
+ timestamp: number;
196
+ };
@@ -0,0 +1,307 @@
1
+ /**
2
+ * Solana SOL 转账功能
3
+ * SDK 只负责构建和签名交易,提交到后端服务器广播
4
+ * @module sol/utils/transfer
5
+ */
6
+ import { PublicKey, SystemProgram, Transaction, } from '@solana/web3.js';
7
+ import { solToLamports } from '../constants.js';
8
+ // ==================== 常量 ====================
9
+ /** 单笔交易最大转账数量(受 1232 bytes 限制) */
10
+ export const MAX_TRANSFERS_PER_TX = 20;
11
+ // ==================== 指令构建 ====================
12
+ /**
13
+ * 构建转账指令
14
+ * @param from 发送者公钥
15
+ * @param to 接收者地址
16
+ * @param amount lamports
17
+ */
18
+ export function buildTransferInstruction(from, to, amount) {
19
+ return SystemProgram.transfer({
20
+ fromPubkey: from,
21
+ toPubkey: new PublicKey(to),
22
+ lamports: amount,
23
+ });
24
+ }
25
+ /**
26
+ * 构建批量转账指令
27
+ * @param from 发送者公钥
28
+ * @param transfers 转账列表
29
+ */
30
+ export function buildTransferInstructions(from, transfers) {
31
+ return transfers.map(t => buildTransferInstruction(from, t.to, t.amount));
32
+ }
33
+ /**
34
+ * 构建批量转账指令组(支持超过 20 个地址,自动分组)
35
+ *
36
+ * 用于 SOL 分发场景:一个地址向多个地址转账
37
+ * 自动按 20 个一组分组,每组可以打包成一笔交易
38
+ *
39
+ * @param from 发送者公钥
40
+ * @param transfers 转账列表(可以超过 20 个)
41
+ * @param groupSize 每组大小(默认 20,最大 20)
42
+ * @returns 分组后的指令组
43
+ *
44
+ * @example
45
+ * // 100 个地址分发
46
+ * const groups = buildBatchTransferInstructionGroups(
47
+ * wallet.publicKey,
48
+ * distributions.map(d => ({ to: d.address, amount: BigInt(d.lamports) }))
49
+ * );
50
+ *
51
+ * // 返回 5 个组,每组 20 个转账指令
52
+ * console.log(groups.totalGroups); // 5
53
+ *
54
+ * // 每组可以单独构建交易
55
+ * for (const group of groups.groups) {
56
+ * const tx = new Transaction();
57
+ * group.instructions.forEach(ix => tx.add(ix));
58
+ * // ... 签名并发送
59
+ * }
60
+ */
61
+ export function buildBatchTransferInstructionGroups(from, transfers, groupSize = MAX_TRANSFERS_PER_TX) {
62
+ if (transfers.length === 0) {
63
+ return {
64
+ groups: [],
65
+ totalTransfers: 0,
66
+ totalGroups: 0,
67
+ from,
68
+ };
69
+ }
70
+ // 确保 groupSize 不超过最大限制
71
+ const safeGroupSize = Math.min(groupSize, MAX_TRANSFERS_PER_TX);
72
+ // 计算总组数
73
+ const totalGroups = Math.ceil(transfers.length / safeGroupSize);
74
+ // 分组
75
+ const groups = [];
76
+ for (let i = 0; i < transfers.length; i += safeGroupSize) {
77
+ const batch = transfers.slice(i, i + safeGroupSize);
78
+ const groupIndex = Math.floor(i / safeGroupSize);
79
+ groups.push({
80
+ instructions: batch.map(t => buildTransferInstruction(from, t.to, t.amount)),
81
+ transfers: batch,
82
+ groupIndex,
83
+ totalGroups,
84
+ });
85
+ }
86
+ return {
87
+ groups,
88
+ totalTransfers: transfers.length,
89
+ totalGroups,
90
+ from,
91
+ };
92
+ }
93
+ /**
94
+ * 从指令组构建交易(不签名)
95
+ * @param group 指令组
96
+ * @param blockhash 区块哈希
97
+ * @param feePayer 费用支付者
98
+ */
99
+ export function buildTransactionFromGroup(group, blockhash, feePayer) {
100
+ const tx = new Transaction();
101
+ group.instructions.forEach(ix => tx.add(ix));
102
+ tx.recentBlockhash = blockhash;
103
+ tx.feePayer = feePayer;
104
+ return tx;
105
+ }
106
+ /**
107
+ * 从指令组构建并签名交易
108
+ * @param group 指令组
109
+ * @param blockhash 区块哈希
110
+ * @param signer 签名者
111
+ */
112
+ export function buildAndSignTransactionFromGroup(group, blockhash, signer) {
113
+ const tx = buildTransactionFromGroup(group, blockhash, signer.publicKey);
114
+ tx.sign(signer);
115
+ return tx;
116
+ }
117
+ // ==================== 单笔转账签名 ====================
118
+ /**
119
+ * 构建并签名单笔 SOL 转账
120
+ * @param connection Solana 连接
121
+ * @param from 发送者 Keypair
122
+ * @param to 接收者地址
123
+ * @param amount lamports
124
+ */
125
+ export async function signTransferSOL(connection, from, to, amount) {
126
+ const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash('finalized');
127
+ const transaction = new Transaction().add(buildTransferInstruction(from.publicKey, to, amount));
128
+ transaction.recentBlockhash = blockhash;
129
+ transaction.feePayer = from.publicKey;
130
+ transaction.sign(from);
131
+ const serialized = transaction.serialize();
132
+ return {
133
+ data: Buffer.from(serialized).toString('base64'),
134
+ signature: transaction.signature ? Buffer.from(transaction.signature).toString('base64') : '',
135
+ transfers: [{ to, amount }],
136
+ recentBlockhash: blockhash,
137
+ lastValidBlockHeight,
138
+ };
139
+ }
140
+ /**
141
+ * 构建并签名单笔 SOL 转账(使用 SOL 数量)
142
+ */
143
+ export async function signTransferSOLAmount(connection, from, to, solAmount) {
144
+ const lamports = solToLamports(solAmount);
145
+ return signTransferSOL(connection, from, to, lamports);
146
+ }
147
+ // ==================== 批量转账签名 ====================
148
+ /**
149
+ * 构建并签名批量转账(单笔交易,多个指令)
150
+ * 单笔交易最多约 20-30 个转账
151
+ * @param connection Solana 连接
152
+ * @param from 发送者 Keypair
153
+ * @param transfers 转账列表
154
+ */
155
+ export async function signBatchTransferSOL(connection, from, transfers) {
156
+ if (transfers.length === 0) {
157
+ throw new Error('No transfers provided');
158
+ }
159
+ if (transfers.length > 20) {
160
+ throw new Error(`Too many transfers (${transfers.length}). Max 20 per transaction. Use signBatchTransferSOLMultiple instead.`);
161
+ }
162
+ const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash('finalized');
163
+ const transaction = new Transaction();
164
+ for (const transfer of transfers) {
165
+ transaction.add(buildTransferInstruction(from.publicKey, transfer.to, transfer.amount));
166
+ }
167
+ transaction.recentBlockhash = blockhash;
168
+ transaction.feePayer = from.publicKey;
169
+ transaction.sign(from);
170
+ const serialized = transaction.serialize();
171
+ return {
172
+ data: Buffer.from(serialized).toString('base64'),
173
+ signature: transaction.signature ? Buffer.from(transaction.signature).toString('base64') : '',
174
+ transfers,
175
+ recentBlockhash: blockhash,
176
+ lastValidBlockHeight,
177
+ };
178
+ }
179
+ /**
180
+ * 构建并签名批量转账(多笔交易)
181
+ * 自动分批处理大量转账
182
+ * @param connection Solana 连接
183
+ * @param from 发送者 Keypair
184
+ * @param transfers 转账列表
185
+ * @param batchSize 每批大小(默认 20)
186
+ */
187
+ export async function signBatchTransferSOLMultiple(connection, from, transfers, batchSize = 20) {
188
+ if (transfers.length === 0) {
189
+ throw new Error('No transfers provided');
190
+ }
191
+ const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash('finalized');
192
+ const transactions = [];
193
+ // 分批处理
194
+ for (let i = 0; i < transfers.length; i += batchSize) {
195
+ const batch = transfers.slice(i, i + batchSize);
196
+ const transaction = new Transaction();
197
+ for (const transfer of batch) {
198
+ transaction.add(buildTransferInstruction(from.publicKey, transfer.to, transfer.amount));
199
+ }
200
+ transaction.recentBlockhash = blockhash;
201
+ transaction.feePayer = from.publicKey;
202
+ transaction.sign(from);
203
+ const serialized = transaction.serialize();
204
+ transactions.push({
205
+ data: Buffer.from(serialized).toString('base64'),
206
+ signature: transaction.signature ? Buffer.from(transaction.signature).toString('base64') : '',
207
+ transfers: batch,
208
+ recentBlockhash: blockhash,
209
+ lastValidBlockHeight,
210
+ });
211
+ }
212
+ return {
213
+ transactions,
214
+ totalTransfers: transfers.length,
215
+ totalTransactions: transactions.length,
216
+ createdAt: Date.now(),
217
+ };
218
+ }
219
+ /**
220
+ * 签名多钱包转账(每个钱包一笔交易)
221
+ * 用于归集或分发场景
222
+ * @param connection Solana 连接
223
+ * @param transfers 转账列表
224
+ */
225
+ export async function signMultiWalletTransfers(connection, transfers) {
226
+ if (transfers.length === 0) {
227
+ throw new Error('No transfers provided');
228
+ }
229
+ const { blockhash, lastValidBlockHeight } = await connection.getLatestBlockhash('finalized');
230
+ const transactions = [];
231
+ for (const transfer of transfers) {
232
+ const transaction = new Transaction().add(buildTransferInstruction(transfer.from.publicKey, transfer.to, transfer.amount));
233
+ transaction.recentBlockhash = blockhash;
234
+ transaction.feePayer = transfer.from.publicKey;
235
+ transaction.sign(transfer.from);
236
+ const serialized = transaction.serialize();
237
+ transactions.push({
238
+ data: Buffer.from(serialized).toString('base64'),
239
+ signature: transaction.signature ? Buffer.from(transaction.signature).toString('base64') : '',
240
+ transfers: [{ to: transfer.to, amount: transfer.amount }],
241
+ recentBlockhash: blockhash,
242
+ lastValidBlockHeight,
243
+ });
244
+ }
245
+ return {
246
+ transactions,
247
+ totalTransfers: transfers.length,
248
+ totalTransactions: transactions.length,
249
+ createdAt: Date.now(),
250
+ };
251
+ }
252
+ /**
253
+ * 归集 SOL(多个钱包转到一个地址)
254
+ * @param connection Solana 连接
255
+ * @param wallets 钱包列表
256
+ * @param toAddress 目标地址
257
+ * @param amounts 每个钱包转账金额(lamports),如果为空则转全部(保留 5000 lamports 作为手续费)
258
+ */
259
+ export async function signCollectSOL(connection, wallets, toAddress, amounts) {
260
+ const transfers = [];
261
+ for (let i = 0; i < wallets.length; i++) {
262
+ const wallet = wallets[i];
263
+ let amount;
264
+ if (amounts && amounts[i] !== undefined) {
265
+ amount = amounts[i];
266
+ }
267
+ else {
268
+ // 查询余额,保留 5000 lamports 作为手续费
269
+ const balance = await connection.getBalance(wallet.publicKey);
270
+ const reserved = BigInt(5000);
271
+ amount = BigInt(balance) > reserved ? BigInt(balance) - reserved : BigInt(0);
272
+ }
273
+ if (amount > 0) {
274
+ transfers.push({
275
+ from: wallet,
276
+ to: toAddress,
277
+ amount,
278
+ });
279
+ }
280
+ }
281
+ return signMultiWalletTransfers(connection, transfers);
282
+ }
283
+ /**
284
+ * 分发 SOL(一个钱包转到多个地址)
285
+ * @param connection Solana 连接
286
+ * @param from 发送者 Keypair
287
+ * @param distributions 分发列表
288
+ */
289
+ export async function signDistributeSOL(connection, from, distributions) {
290
+ // 使用批量转账
291
+ return signBatchTransferSOLMultiple(connection, from, distributions);
292
+ }
293
+ // ==================== 工具函数 ====================
294
+ /**
295
+ * 将签名结果转换为后端 API 格式
296
+ */
297
+ export function toBackendFormat(result) {
298
+ return {
299
+ transactions: result.transactions.map(tx => ({
300
+ data: tx.data,
301
+ signature: tx.signature,
302
+ })),
303
+ totalTransfers: result.totalTransfers,
304
+ totalTransactions: result.totalTransactions,
305
+ timestamp: result.createdAt,
306
+ };
307
+ }
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Solana 钱包工具
3
+ * @module sol/utils/wallet
4
+ */
5
+ import { Keypair, PublicKey } from '@solana/web3.js';
6
+ import type { WalletGenerateResult } from '../types.js';
7
+ /**
8
+ * 批量生成钱包的详细结果
9
+ */
10
+ export interface BatchWalletGenerateResult {
11
+ wallets: WalletGenerateResult[];
12
+ count: number;
13
+ /** 所有地址列表(方便使用) */
14
+ addresses: string[];
15
+ /** 所有私钥列表(base58) */
16
+ secretKeys: string[];
17
+ /** Keypair 对象列表(方便直接使用) */
18
+ keypairs: Keypair[];
19
+ }
20
+ /**
21
+ * 生成新的 Solana 钱包
22
+ * @returns 钱包信息
23
+ */
24
+ export declare function generateKeypair(): WalletGenerateResult;
25
+ /**
26
+ * 批量生成钱包
27
+ * @param count 数量
28
+ * @returns 钱包列表
29
+ */
30
+ export declare function generateKeypairs(count: number): WalletGenerateResult[];
31
+ /**
32
+ * 批量生成钱包(增强版,返回更多信息)
33
+ * @param count 数量
34
+ * @returns 详细结果
35
+ */
36
+ export declare function batchGenerateWallets(count: number): BatchWalletGenerateResult;
37
+ /**
38
+ * 从私钥列表批量导入钱包
39
+ * @param secretKeys 私钥列表(base58 字符串)
40
+ * @returns 导入结果
41
+ */
42
+ export declare function batchImportWallets(secretKeys: string[]): {
43
+ keypairs: Keypair[];
44
+ addresses: string[];
45
+ failed: Array<{
46
+ index: number;
47
+ secretKey: string;
48
+ error: string;
49
+ }>;
50
+ };
51
+ /**
52
+ * 从私钥导入钱包
53
+ * @param secretKey 私钥(Uint8Array 或 base58 字符串)
54
+ * @returns Keypair
55
+ */
56
+ export declare function keypairFromSecretKey(secretKey: Uint8Array | string): Keypair;
57
+ /**
58
+ * 导出私钥为 base58 格式
59
+ * @param keypair Keypair
60
+ * @returns base58 编码的私钥
61
+ */
62
+ export declare function exportSecretKey(keypair: Keypair): string;
63
+ /**
64
+ * 导出私钥为 Uint8Array
65
+ * @param keypair Keypair
66
+ * @returns Uint8Array 私钥
67
+ */
68
+ export declare function exportSecretKeyArray(keypair: Keypair): Uint8Array;
69
+ /**
70
+ * 验证 Solana 地址格式
71
+ * @param address 地址字符串
72
+ * @returns 是否有效
73
+ */
74
+ export declare function isValidAddress(address: string): boolean;
75
+ /**
76
+ * 验证私钥格式
77
+ * @param secretKey 私钥(base58 字符串)
78
+ * @returns 是否有效
79
+ */
80
+ export declare function isValidSecretKey(secretKey: string): boolean;
81
+ /**
82
+ * 从私钥获取公钥/地址
83
+ * @param secretKey 私钥(Uint8Array 或 base58 字符串)
84
+ * @returns 公钥地址
85
+ */
86
+ export declare function getAddressFromSecretKey(secretKey: Uint8Array | string): string;
87
+ /**
88
+ * 创建 PublicKey 对象
89
+ * @param address 地址字符串
90
+ * @returns PublicKey
91
+ */
92
+ export declare function toPublicKey(address: string): PublicKey;
93
+ /**
94
+ * 比较两个地址是否相同
95
+ * @param addr1 地址1
96
+ * @param addr2 地址2
97
+ * @returns 是否相同
98
+ */
99
+ export declare function isSameAddress(addr1: string, addr2: string): boolean;
100
+ /**
101
+ * 缩短地址显示
102
+ * @param address 完整地址
103
+ * @param prefixLength 前缀长度(默认4)
104
+ * @param suffixLength 后缀长度(默认4)
105
+ * @returns 缩短后的地址
106
+ */
107
+ export declare function shortenAddress(address: string, prefixLength?: number, suffixLength?: number): string;