four-flap-meme-sdk 1.4.55 → 1.4.57

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.
@@ -4,6 +4,7 @@
4
4
  * 功能:钱包A卖出代币 → 钱包B买入相同数量 → 原子执行
5
5
  */
6
6
  import { CommonBundleConfig } from '../../utils/bundle-helpers.js';
7
+ import { type GeneratedWallet } from '../../utils/wallet.js';
7
8
  export interface FourSwapSignConfig {
8
9
  rpcUrl: string;
9
10
  gasLimit?: number | bigint;
@@ -105,6 +106,7 @@ export interface FourQuickBatchSwapSignParams {
105
106
  buyerAmounts?: string[];
106
107
  tokenAddress: string;
107
108
  config: FourSwapSignConfig;
109
+ disperseHopCount?: number;
108
110
  startNonces?: number[];
109
111
  }
110
112
  /**
@@ -112,6 +114,7 @@ export interface FourQuickBatchSwapSignParams {
112
114
  */
113
115
  export interface FourQuickBatchSwapResult {
114
116
  signedTransactions: string[];
117
+ disperseHopWallets?: GeneratedWallet[];
115
118
  metadata?: {
116
119
  sellerAddress: string;
117
120
  buyerAddresses: string[];
@@ -119,18 +122,20 @@ export interface FourQuickBatchSwapResult {
119
122
  estimatedBNBOut: string;
120
123
  transferAmounts: string[];
121
124
  profitAmount?: string;
125
+ disperseHopCount?: number;
122
126
  };
123
127
  }
124
128
  /**
125
129
  * Four 内盘快捷批量换手(资金利用率模式)
126
130
  *
127
- * 流程:[贿赂] → [卖出] → [转账1, 转账2, ...] → [买入1, 买入2, ...] → [利润]
131
+ * 流程:[贿赂] → [卖出] → [转账多跳...] → [买入1, 买入2, ...] → [利润]
128
132
  *
129
133
  * 特点:
130
134
  * - 子钱包不需要预先有 BNB
131
135
  * - 资金来自主钱包卖出代币所得
132
136
  * - 提升资金利用率
137
+ * - ✅ 支持转账多跳,隐藏资金流向
133
138
  *
134
- * 限制:最多 23 个买方(2N + 3 ≤ 50)
139
+ * 限制:根据多跳数动态计算最大买方数量
135
140
  */
136
141
  export declare function fourQuickBatchSwapMerkle(params: FourQuickBatchSwapSignParams): Promise<FourQuickBatchSwapResult>;
@@ -9,6 +9,79 @@ import { NonceManager, getOptimizedGasPrice, getGasLimit, getGasPriceConfig, get
9
9
  import { ADDRESSES, PROFIT_CONFIG, BLOCKRAZOR_BUILDER_EOA } from '../../utils/constants.js';
10
10
  import { TM_ABI, HELPER3_ABI, TM_ADDRESS } from './swap-internal.js';
11
11
  import { getBribeAmount, getProfitRecipient } from './config.js';
12
+ import { generateWallets } from '../../utils/wallet.js';
13
+ // ==================== 多跳转账常量 ====================
14
+ const NATIVE_TRANSFER_GAS_LIMIT = 21055n;
15
+ /**
16
+ * 生成分发多跳路径
17
+ */
18
+ function generateDisperseHopPaths(targetAddresses, hopCount, provider) {
19
+ if (hopCount <= 0) {
20
+ return targetAddresses.map(addr => ({
21
+ targetAddress: addr,
22
+ hopWallets: [],
23
+ hopWalletsInfo: []
24
+ }));
25
+ }
26
+ return targetAddresses.map(targetAddress => {
27
+ const hopWalletsInfo = generateWallets(hopCount);
28
+ const hopWallets = hopWalletsInfo.map(w => new Wallet(w.privateKey, provider));
29
+ return { targetAddress, hopWallets, hopWalletsInfo };
30
+ });
31
+ }
32
+ /**
33
+ * 构建原生代币多跳转账链
34
+ */
35
+ async function buildNativeHopChain(payer, path, finalAmount, gasPrice, chainId, txType, payerNonce) {
36
+ const signedTxs = [];
37
+ const hopCount = path.hopWallets.length;
38
+ if (hopCount === 0) {
39
+ const tx = {
40
+ to: path.targetAddress,
41
+ value: finalAmount,
42
+ nonce: payerNonce,
43
+ gasLimit: NATIVE_TRANSFER_GAS_LIMIT,
44
+ gasPrice,
45
+ chainId,
46
+ type: txType
47
+ };
48
+ signedTxs.push(await payer.signTransaction(tx));
49
+ return signedTxs;
50
+ }
51
+ const hopGasCost = NATIVE_TRANSFER_GAS_LIMIT * gasPrice;
52
+ // 计算每跳需要的金额
53
+ const amountsPerHop = [];
54
+ for (let i = 0; i < hopCount; i++) {
55
+ const remainingHops = hopCount - i;
56
+ amountsPerHop.push(finalAmount + hopGasCost * BigInt(remainingHops));
57
+ }
58
+ // payer → hop1
59
+ signedTxs.push(await payer.signTransaction({
60
+ to: path.hopWallets[0].address,
61
+ value: amountsPerHop[0],
62
+ nonce: payerNonce,
63
+ gasLimit: NATIVE_TRANSFER_GAS_LIMIT,
64
+ gasPrice,
65
+ chainId,
66
+ type: txType
67
+ }));
68
+ // hop1 → hop2 → ... → target
69
+ for (let i = 0; i < hopCount; i++) {
70
+ const fromWallet = path.hopWallets[i];
71
+ const toAddress = i === hopCount - 1 ? path.targetAddress : path.hopWallets[i + 1].address;
72
+ const amount = i === hopCount - 1 ? finalAmount : amountsPerHop[i + 1];
73
+ signedTxs.push(await fromWallet.signTransaction({
74
+ to: toAddress,
75
+ value: amount,
76
+ nonce: 0,
77
+ gasLimit: NATIVE_TRANSFER_GAS_LIMIT,
78
+ gasPrice,
79
+ chainId,
80
+ type: txType
81
+ }));
82
+ }
83
+ return signedTxs;
84
+ }
12
85
  /**
13
86
  * Four内盘捆绑换手
14
87
  */
@@ -430,26 +503,33 @@ export async function fourBatchSwapMerkle(params) {
430
503
  /**
431
504
  * Four 内盘快捷批量换手(资金利用率模式)
432
505
  *
433
- * 流程:[贿赂] → [卖出] → [转账1, 转账2, ...] → [买入1, 买入2, ...] → [利润]
506
+ * 流程:[贿赂] → [卖出] → [转账多跳...] → [买入1, 买入2, ...] → [利润]
434
507
  *
435
508
  * 特点:
436
509
  * - 子钱包不需要预先有 BNB
437
510
  * - 资金来自主钱包卖出代币所得
438
511
  * - 提升资金利用率
512
+ * - ✅ 支持转账多跳,隐藏资金流向
439
513
  *
440
- * 限制:最多 23 个买方(2N + 3 ≤ 50)
514
+ * 限制:根据多跳数动态计算最大买方数量
441
515
  */
442
516
  export async function fourQuickBatchSwapMerkle(params) {
443
- const { sellerPrivateKey, sellAmount, sellPercentage, buyerPrivateKeys, buyerRatios, buyerAmounts, tokenAddress, config, startNonces // ✅ 可选:前端预获取的 nonces
517
+ const { sellerPrivateKey, sellAmount, sellPercentage, buyerPrivateKeys, buyerRatios, buyerAmounts, tokenAddress, config, disperseHopCount = 0, // ✅ 转账多跳数(默认0=直接转账)
518
+ startNonces // ✅ 可选:前端预获取的 nonces
444
519
  } = params;
445
- // ✅ 校验买方数量
446
- // 贿赂(1) + 卖出(1) + 转账(N) + 买入(N) + 利润(1) ≤ 50 → 2N + 3 ≤ 50 → N ≤ 23
447
- const MAX_BUYERS = 23;
520
+ // ✅ 动态计算最大买方数量(根据多跳数)
521
+ // 固定开销: 贿赂(1) + 卖出(1) + 利润多跳(PROFIT_HOP_COUNT + 1)
522
+ // 转账(N*(H+1)) + 买入(N) = N*(H+2)
523
+ const fixedOverhead = 1 + 1 + PROFIT_HOP_COUNT + 1;
524
+ const maxTxs = 50 - fixedOverhead;
525
+ let MAX_BUYERS = Math.floor(maxTxs / (disperseHopCount + 2));
526
+ MAX_BUYERS = Math.max(1, MAX_BUYERS);
527
+ console.log(`[fourQuickBatchSwapMerkle] 多跳数: ${disperseHopCount}, 最大买方数: ${MAX_BUYERS}`);
448
528
  if (buyerPrivateKeys.length === 0) {
449
529
  throw new Error('至少需要一个买方钱包');
450
530
  }
451
531
  if (buyerPrivateKeys.length > MAX_BUYERS) {
452
- throw new Error(`资金利用率模式买方钱包数量超过限制: ${buyerPrivateKeys.length} > ${MAX_BUYERS}`);
532
+ throw new Error(`资金利用率模式(${disperseHopCount}跳)买方钱包数量超过限制: ${buyerPrivateKeys.length} > ${MAX_BUYERS}`);
453
533
  }
454
534
  // ✅ 校验分配模式
455
535
  if (!buyerRatios && !buyerAmounts) {
@@ -559,25 +639,45 @@ export async function fourQuickBatchSwapMerkle(params) {
559
639
  type: txType
560
640
  });
561
641
  console.log(`[fourQuickBatchSwapMerkle] 卖出交易已签名`);
562
- // ==================== 3. 转账交易(并行签名)====================
642
+ // ==================== 3. 转账交易(支持多跳)====================
563
643
  const reserveGas = ethers.parseEther((config.reserveGasBNB || 0.0005).toString());
564
644
  const buyerGasCost = gasPrice * finalGasLimit;
565
- // ✅ 预分配 nonce,然后并行签名
566
- const transferNonces = buyers.map((_, i) => sellerNonce + i);
567
- sellerNonce += buyers.length; // 更新 sellerNonce
568
- const transferTxs = await Promise.all(buyers.map((buyer, i) => {
569
- const transferValue = transferAmountsWei[i] + reserveGas + buyerGasCost;
570
- return seller.signTransaction({
571
- to: buyer.address,
572
- value: transferValue,
573
- nonce: transferNonces[i],
574
- gasPrice,
575
- gasLimit: 21000n,
576
- chainId: chainIdNum,
577
- type: txType
578
- });
579
- }));
580
- console.log(`[fourQuickBatchSwapMerkle] ${transferTxs.length} 笔转账交易已签名`);
645
+ // ✅ 生成多跳路径
646
+ const hopPaths = generateDisperseHopPaths(buyers.map(b => b.address), disperseHopCount, provider);
647
+ // 收集所有中间钱包信息
648
+ const allHopWallets = [];
649
+ hopPaths.forEach(path => {
650
+ allHopWallets.push(...path.hopWalletsInfo);
651
+ });
652
+ let transferTxs = [];
653
+ if (disperseHopCount === 0) {
654
+ // ✅ 无多跳:直接转账
655
+ const transferNonces = buyers.map((_, i) => sellerNonce + i);
656
+ sellerNonce += buyers.length;
657
+ transferTxs = await Promise.all(buyers.map((buyer, i) => {
658
+ const transferValue = transferAmountsWei[i] + reserveGas + buyerGasCost;
659
+ return seller.signTransaction({
660
+ to: buyer.address,
661
+ value: transferValue,
662
+ nonce: transferNonces[i],
663
+ gasPrice,
664
+ gasLimit: NATIVE_TRANSFER_GAS_LIMIT,
665
+ chainId: chainIdNum,
666
+ type: txType
667
+ });
668
+ }));
669
+ }
670
+ else {
671
+ // ✅ 有多跳:构建多跳转账链
672
+ const hopChains = await Promise.all(hopPaths.map((path, i) => {
673
+ const finalAmount = transferAmountsWei[i] + reserveGas + buyerGasCost;
674
+ const payerNonce = sellerNonce + i * (disperseHopCount + 1);
675
+ return buildNativeHopChain(seller, path, finalAmount, gasPrice, chainIdNum, txType, payerNonce);
676
+ }));
677
+ transferTxs = hopChains.flat();
678
+ sellerNonce += buyers.length * (disperseHopCount + 1);
679
+ }
680
+ console.log(`[fourQuickBatchSwapMerkle] ${transferTxs.length} 笔转账交易已签名 (多跳数=${disperseHopCount})`);
581
681
  // ==================== 4. 买入交易 ====================
582
682
  // ✅ 如果前端传入了 startNonces,使用 buyer 部分(从索引 1 开始)
583
683
  const buyerNonces = startNonces && startNonces.length > 1
@@ -631,13 +731,15 @@ export async function fourQuickBatchSwapMerkle(params) {
631
731
  console.log(` - 利润多跳: ${profitAmount > 0n ? PROFIT_HOP_COUNT + 1 : 0}`);
632
732
  return {
633
733
  signedTransactions,
734
+ disperseHopWallets: allHopWallets.length > 0 ? allHopWallets : undefined, // ✅ 返回中间钱包信息
634
735
  metadata: {
635
736
  sellerAddress: seller.address,
636
737
  buyerAddresses: buyers.map(b => b.address),
637
738
  sellAmount: ethers.formatUnits(sellAmountWei, decimals),
638
739
  estimatedBNBOut: ethers.formatEther(estimatedBNBOut),
639
740
  transferAmounts: transferAmountsWei.map(amt => ethers.formatEther(amt)),
640
- profitAmount: profitAmount > 0n ? ethers.formatEther(profitAmount) : undefined
741
+ profitAmount: profitAmount > 0n ? ethers.formatEther(profitAmount) : undefined,
742
+ disperseHopCount: disperseHopCount > 0 ? disperseHopCount : undefined // ✅ 返回多跳数
641
743
  }
642
744
  };
643
745
  }
@@ -4,6 +4,7 @@
4
4
  * 功能:钱包A卖出代币 → 钱包B买入相同数量 → 原子执行
5
5
  */
6
6
  import { CommonBundleConfig } from '../../utils/bundle-helpers.js';
7
+ import { type GeneratedWallet } from '../../utils/wallet.js';
7
8
  export interface FlapSwapSignConfig {
8
9
  rpcUrl: string;
9
10
  gasLimit?: number | bigint;
@@ -118,6 +119,7 @@ export interface FlapQuickBatchSwapSignParams {
118
119
  config: FlapSwapSignConfig;
119
120
  quoteToken?: string;
120
121
  quoteTokenDecimals?: number;
122
+ disperseHopCount?: number;
121
123
  startNonces?: number[];
122
124
  }
123
125
  /**
@@ -125,6 +127,7 @@ export interface FlapQuickBatchSwapSignParams {
125
127
  */
126
128
  export interface FlapQuickBatchSwapResult {
127
129
  signedTransactions: string[];
130
+ disperseHopWallets?: GeneratedWallet[];
128
131
  metadata?: {
129
132
  sellerAddress: string;
130
133
  buyerAddresses: string[];
@@ -133,19 +136,21 @@ export interface FlapQuickBatchSwapResult {
133
136
  transferAmounts: string[];
134
137
  profitAmount?: string;
135
138
  useNativeToken: boolean;
139
+ disperseHopCount?: number;
136
140
  };
137
141
  }
138
142
  /**
139
143
  * Flap 内盘快捷批量换手(资金利用率模式)
140
144
  *
141
- * 流程:[贿赂] → [卖出] → [转账1, 转账2, ...] → [买入1, 买入2, ...] → [利润]
145
+ * 流程:[贿赂] → [卖出] → [转账多跳...] → [买入1, 买入2, ...] → [利润]
142
146
  *
143
147
  * 特点:
144
148
  * - 子钱包不需要预先有余额
145
149
  * - 资金来自主钱包卖出代币所得
146
150
  * - 提升资金利用率
147
151
  * - 支持原生代币(BNB/OKB/ETH)和 ERC20(如 USDT)两种模式
152
+ * - ✅ 支持转账多跳,隐藏资金流向
148
153
  *
149
- * 限制:最多 23 个买方(2N + 3 ≤ 50)
154
+ * 限制:根据多跳数动态计算最大买方数量
150
155
  */
151
156
  export declare function flapQuickBatchSwapMerkle(params: FlapQuickBatchSwapSignParams): Promise<FlapQuickBatchSwapResult>;