four-flap-meme-sdk 1.2.24 → 1.2.25

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.
@@ -397,49 +397,53 @@ export async function sweepWithBundleMerkle(params) {
397
397
  }
398
398
  }
399
399
  // ✅ 第二步:生成归集交易(扣除利润后的金额)
400
+ // ✅ 先为支付者预留 nonce,确保 nonce 连续
401
+ let payerProfitNonce;
402
+ if (extractProfit && totalProfit > 0n && maxSweepIndex >= 0) {
403
+ const payerWallet = wallets[maxSweepIndex];
404
+ const nonces = await nonceManager.getNextNonceBatch(payerWallet, 2);
405
+ payerProfitNonce = nonces[1]; // 利润交易的 nonce
406
+ }
400
407
  const txPromises = wallets.map(async (w, i) => {
401
408
  const toSend = sweepAmounts[i];
402
409
  if (toSend <= 0n)
403
- return [];
410
+ return null;
404
411
  let actualToSend = toSend;
405
412
  if (extractProfit) {
406
413
  const { remaining } = calculateProfit(toSend, config);
407
414
  actualToSend = remaining;
408
415
  }
409
- // ✅ 如果是支付利润的钱包,需要预留 2 个 nonce
410
- const isPayerWallet = extractProfit && totalProfit > 0n && i === maxSweepIndex;
411
- const nonces = isPayerWallet
412
- ? await nonceManager.getNextNonceBatch(w, 2)
413
- : [await nonceManager.getNextNonce(w)];
414
- const txs = [];
415
- // 归集交易
416
+ // ✅ 支付者使用预留的第一个 nonce,其他钱包正常获取
417
+ const nonce = (i === maxSweepIndex && payerProfitNonce !== undefined)
418
+ ? payerProfitNonce - 1 // 使用预留的第一个 nonce
419
+ : await nonceManager.getNextNonce(w);
416
420
  const mainTx = await w.signTransaction({
417
421
  to: target,
418
422
  value: actualToSend,
419
- nonce: nonces[0],
423
+ nonce,
420
424
  gasPrice,
421
425
  gasLimit: nativeGasLimit,
422
426
  chainId: chainIdNum,
423
427
  type: txType
424
428
  });
425
- txs.push(mainTx);
426
- // ✅ 如果是支付者,立即生成利润转账(确保 nonce 连续)
427
- if (isPayerWallet) {
428
- const profitTx = await w.signTransaction({
429
- to: config.profitRecipient,
430
- value: totalProfit,
431
- nonce: nonces[1],
432
- gasPrice,
433
- gasLimit: nativeGasLimit,
434
- chainId: chainIdNum,
435
- type: txType
436
- });
437
- txs.push(profitTx);
438
- }
439
- return txs;
429
+ return mainTx;
440
430
  });
441
- const allTxs = (await Promise.all(txPromises)).flat().filter(tx => tx !== null);
431
+ const allTxs = (await Promise.all(txPromises)).filter(tx => tx !== null);
442
432
  signedTxs.push(...allTxs);
433
+ // ✅ 第三步:在所有归集交易之后,生成利润交易
434
+ if (extractProfit && totalProfit > 0n && maxSweepIndex >= 0 && payerProfitNonce !== undefined) {
435
+ const payerWallet = wallets[maxSweepIndex];
436
+ const profitTx = await payerWallet.signTransaction({
437
+ to: config.profitRecipient,
438
+ value: totalProfit,
439
+ nonce: payerProfitNonce,
440
+ gasPrice,
441
+ gasLimit: nativeGasLimit,
442
+ chainId: chainIdNum,
443
+ type: txType
444
+ });
445
+ signedTxs.push(profitTx);
446
+ }
443
447
  }
444
448
  else {
445
449
  // ✅ ERC20:批量查询余额,统一扣除利润
@@ -505,53 +509,57 @@ export async function sweepWithBundleMerkle(params) {
505
509
  }
506
510
  }
507
511
  // ✅ 第二步:生成归集交易(扣除利润后的金额)
512
+ // ✅ 先为支付者预留 nonce,确保 nonce 连续
513
+ let payerProfitNonce;
514
+ if (extractProfit && totalProfit > 0n && maxSweepIndex >= 0) {
515
+ const payerWallet = wallets[maxSweepIndex];
516
+ const nonces = await nonceManager.getNextNonceBatch(payerWallet, 2);
517
+ payerProfitNonce = nonces[1]; // 利润交易的 nonce
518
+ }
508
519
  const txPromises = wallets.map(async (w, i) => {
509
520
  const toSend = sweepAmounts[i];
510
521
  if (toSend <= 0n)
511
- return [];
522
+ return null;
512
523
  let actualToSend = toSend;
513
524
  if (extractProfit) {
514
525
  const { remaining } = calculateProfit(toSend, config);
515
526
  actualToSend = remaining;
516
527
  }
517
- // ✅ 如果是支付利润的钱包,需要预留 2 个 nonce
518
- const isPayerWallet = extractProfit && totalProfit > 0n && i === maxSweepIndex;
519
- const nonces = isPayerWallet
520
- ? await nonceManager.getNextNonceBatch(w, 2)
521
- : [await nonceManager.getNextNonce(w)];
522
- const txs = [];
523
- // 归集交易
528
+ // ✅ 支付者使用预留的第一个 nonce,其他钱包正常获取
529
+ const nonce = (i === maxSweepIndex && payerProfitNonce !== undefined)
530
+ ? payerProfitNonce - 1 // 使用预留的第一个 nonce
531
+ : await nonceManager.getNextNonce(w);
524
532
  const data = iface.encodeFunctionData('transfer', [target, actualToSend]);
525
533
  const mainTx = await w.signTransaction({
526
534
  to: tokenAddress,
527
535
  data,
528
536
  value: 0n,
529
- nonce: nonces[0],
537
+ nonce,
530
538
  gasPrice,
531
539
  gasLimit: finalGasLimit,
532
540
  chainId: chainIdNum,
533
541
  type: txType
534
542
  });
535
- txs.push(mainTx);
536
- // ✅ 如果是支付者,立即生成利润转账(确保 nonce 连续)
537
- if (isPayerWallet) {
538
- const profitData = iface.encodeFunctionData('transfer', [config.profitRecipient, totalProfit]);
539
- const profitTx = await w.signTransaction({
540
- to: tokenAddress,
541
- data: profitData,
542
- value: 0n,
543
- nonce: nonces[1],
544
- gasPrice,
545
- gasLimit: finalGasLimit,
546
- chainId: chainIdNum,
547
- type: txType
548
- });
549
- txs.push(profitTx);
550
- }
551
- return txs;
543
+ return mainTx;
552
544
  });
553
- const allTxs = (await Promise.all(txPromises)).flat().filter(tx => tx !== null);
545
+ const allTxs = (await Promise.all(txPromises)).filter(tx => tx !== null);
554
546
  signedTxs.push(...allTxs);
547
+ // ✅ 第三步:在所有归集交易之后,生成利润交易
548
+ if (extractProfit && totalProfit > 0n && maxSweepIndex >= 0 && payerProfitNonce !== undefined) {
549
+ const payerWallet = wallets[maxSweepIndex];
550
+ const profitData = iface.encodeFunctionData('transfer', [config.profitRecipient, totalProfit]);
551
+ const profitTx = await payerWallet.signTransaction({
552
+ to: tokenAddress,
553
+ data: profitData,
554
+ value: 0n,
555
+ nonce: payerProfitNonce,
556
+ gasPrice,
557
+ gasLimit: finalGasLimit,
558
+ chainId: chainIdNum,
559
+ type: txType
560
+ });
561
+ signedTxs.push(profitTx);
562
+ }
555
563
  }
556
564
  }
557
565
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "four-flap-meme-sdk",
3
- "version": "1.2.24",
3
+ "version": "1.2.25",
4
4
  "description": "SDK for Flap bonding curve and four.meme TokenManager",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",