four-flap-meme-sdk 1.2.26 → 1.2.28
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.
|
@@ -348,9 +348,7 @@ export async function sweepWithBundleMerkle(params) {
|
|
|
348
348
|
// ✅ 原生币:批量查询余额,统一扣除利润
|
|
349
349
|
const balances = await _batchGetBalances(provider, addresses);
|
|
350
350
|
const gasCostBase = nativeGasLimit * gasPrice; // 主转账 gas 费
|
|
351
|
-
//
|
|
352
|
-
const profitTxGas = nativeGasLimit * gasPrice;
|
|
353
|
-
const gasCost = extractProfit ? (gasCostBase + profitTxGas) : gasCostBase;
|
|
351
|
+
const profitTxGas = nativeGasLimit * gasPrice; // 利润转账 gas 费
|
|
354
352
|
// ✅ 第一步:计算所有钱包的归集金额和利润,找出归集金额最大的钱包
|
|
355
353
|
const sweepAmounts = [];
|
|
356
354
|
let maxSweepIndex = -1;
|
|
@@ -372,6 +370,8 @@ export async function sweepWithBundleMerkle(params) {
|
|
|
372
370
|
return String(amounts[i]);
|
|
373
371
|
return amount !== undefined ? String(amount) : undefined;
|
|
374
372
|
})();
|
|
373
|
+
// ✅ 只有支付者需要 2 笔交易的 gas,其他钱包只需要 1 笔
|
|
374
|
+
const gasCost = gasCostBase; // 所有钱包都只计算主转账的 gas
|
|
375
375
|
if (ratioForI !== undefined) {
|
|
376
376
|
const want = (bal * BigInt(ratioForI)) / 100n;
|
|
377
377
|
const maxSendable = bal > gasCost ? (bal - gasCost) : 0n;
|
|
@@ -396,6 +396,25 @@ export async function sweepWithBundleMerkle(params) {
|
|
|
396
396
|
totalProfit += profit;
|
|
397
397
|
}
|
|
398
398
|
}
|
|
399
|
+
// ✅ 如果需要提取利润,检查支付者是否有足够余额支付利润转账的额外 gas
|
|
400
|
+
if (extractProfit && totalProfit > 0n && maxSweepIndex >= 0) {
|
|
401
|
+
const payerBalance = balances[maxSweepIndex];
|
|
402
|
+
const payerSweepAmount = sweepAmounts[maxSweepIndex];
|
|
403
|
+
const payerNeedGas = gasCostBase + profitTxGas; // 支付者需要 2 笔交易的 gas
|
|
404
|
+
// 如果支付者余额不足以支付 2 笔交易的 gas,减少其归集金额
|
|
405
|
+
if (payerBalance < payerSweepAmount + payerNeedGas) {
|
|
406
|
+
const maxPayerSweep = payerBalance > payerNeedGas ? payerBalance - payerNeedGas : 0n;
|
|
407
|
+
sweepAmounts[maxSweepIndex] = maxPayerSweep;
|
|
408
|
+
// 重新计算总金额和总利润
|
|
409
|
+
totalAmountBeforeProfit = sweepAmounts.reduce((sum, amt) => sum + amt, 0n);
|
|
410
|
+
totalProfit = 0n;
|
|
411
|
+
for (let i = 0; i < sweepAmounts.length; i++) {
|
|
412
|
+
if (sweepAmounts[i] > 0n) {
|
|
413
|
+
totalProfit += calculateProfit(sweepAmounts[i], config).profit;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
399
418
|
// ✅ 第二步:生成归集交易(扣除利润后的金额)
|
|
400
419
|
// ✅ 先为支付者预留 nonce,确保 nonce 连续
|
|
401
420
|
let payerProfitNonce;
|
|
@@ -410,15 +429,12 @@ export async function sweepWithBundleMerkle(params) {
|
|
|
410
429
|
return null;
|
|
411
430
|
let actualToSend = toSend;
|
|
412
431
|
if (extractProfit) {
|
|
413
|
-
|
|
414
|
-
// ✅ 如果是支付者,需要额外扣除其他所有钱包的利润总和
|
|
432
|
+
// ✅ 如果是支付者,扣除所有利润总和;其他钱包不扣利润,归集干净
|
|
415
433
|
if (i === maxSweepIndex && totalProfit > 0n) {
|
|
416
|
-
|
|
417
|
-
const othersProfit = totalProfit - ownProfit; // 其他钱包的利润总和
|
|
418
|
-
actualToSend = remaining - othersProfit; // 扣除其他钱包的利润
|
|
434
|
+
actualToSend = toSend - totalProfit; // 支付者扣除所有利润总和
|
|
419
435
|
}
|
|
420
436
|
else {
|
|
421
|
-
actualToSend =
|
|
437
|
+
actualToSend = toSend; // 其他钱包不扣利润,归集全部
|
|
422
438
|
}
|
|
423
439
|
}
|
|
424
440
|
// ✅ 支付者使用预留的第一个 nonce,其他钱包正常获取
|
|
@@ -461,8 +477,7 @@ export async function sweepWithBundleMerkle(params) {
|
|
|
461
477
|
config.checkBnbForErc20NoHop ? _batchGetBalances(provider, addresses) : Promise.resolve([])
|
|
462
478
|
]);
|
|
463
479
|
const iface = new ethers.Interface(['function transfer(address,uint256) returns (bool)']);
|
|
464
|
-
//
|
|
465
|
-
const profitTxGas = finalGasLimit * gasPrice;
|
|
480
|
+
const profitTxGas = finalGasLimit * gasPrice; // 利润转账 gas 费
|
|
466
481
|
// ✅ 第一步:计算所有钱包的归集金额和利润,找出归集金额最大的钱包
|
|
467
482
|
const sweepAmounts = [];
|
|
468
483
|
let maxSweepIndex = -1;
|
|
@@ -494,8 +509,8 @@ export async function sweepWithBundleMerkle(params) {
|
|
|
494
509
|
sweepAmounts.push(0n);
|
|
495
510
|
continue;
|
|
496
511
|
}
|
|
497
|
-
// ✅ 检查 BNB
|
|
498
|
-
const totalGasNeeded =
|
|
512
|
+
// ✅ 检查 BNB 余额(只需主转账的 gas,支付者后面单独检查)
|
|
513
|
+
const totalGasNeeded = finalGasLimit * gasPrice;
|
|
499
514
|
if (config.checkBnbForErc20NoHop) {
|
|
500
515
|
const bnb = bnbBalances[i] ?? 0n;
|
|
501
516
|
if (skipIfInsufficient && bnb < totalGasNeeded) {
|
|
@@ -516,6 +531,32 @@ export async function sweepWithBundleMerkle(params) {
|
|
|
516
531
|
totalProfit += profit;
|
|
517
532
|
}
|
|
518
533
|
}
|
|
534
|
+
// ✅ 如果需要提取利润,检查支付者是否有足够 BNB 支付利润转账的额外 gas
|
|
535
|
+
if (extractProfit && totalProfit > 0n && maxSweepIndex >= 0 && config.checkBnbForErc20NoHop) {
|
|
536
|
+
const payerBnbBalance = bnbBalances[maxSweepIndex] ?? 0n;
|
|
537
|
+
const payerNeedGas = finalGasLimit * gasPrice + profitTxGas; // 支付者需要 2 笔交易的 gas
|
|
538
|
+
// 如果支付者 BNB 余额不足以支付 2 笔交易的 gas,清零其归集金额
|
|
539
|
+
if (payerBnbBalance < payerNeedGas) {
|
|
540
|
+
sweepAmounts[maxSweepIndex] = 0n;
|
|
541
|
+
// 重新寻找最大归集金额的钱包
|
|
542
|
+
maxSweepIndex = -1;
|
|
543
|
+
maxSweepAmount = 0n;
|
|
544
|
+
for (let i = 0; i < sweepAmounts.length; i++) {
|
|
545
|
+
if (sweepAmounts[i] > maxSweepAmount) {
|
|
546
|
+
maxSweepAmount = sweepAmounts[i];
|
|
547
|
+
maxSweepIndex = i;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
// 重新计算总金额和总利润
|
|
551
|
+
totalAmountBeforeProfit = sweepAmounts.reduce((sum, amt) => sum + amt, 0n);
|
|
552
|
+
totalProfit = 0n;
|
|
553
|
+
for (let i = 0; i < sweepAmounts.length; i++) {
|
|
554
|
+
if (sweepAmounts[i] > 0n) {
|
|
555
|
+
totalProfit += calculateProfit(sweepAmounts[i], config).profit;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
519
560
|
// ✅ 第二步:生成归集交易(扣除利润后的金额)
|
|
520
561
|
// ✅ 先为支付者预留 nonce,确保 nonce 连续
|
|
521
562
|
let payerProfitNonce;
|
|
@@ -530,15 +571,12 @@ export async function sweepWithBundleMerkle(params) {
|
|
|
530
571
|
return null;
|
|
531
572
|
let actualToSend = toSend;
|
|
532
573
|
if (extractProfit) {
|
|
533
|
-
|
|
534
|
-
// ✅ 如果是支付者,需要额外扣除其他所有钱包的利润总和
|
|
574
|
+
// ✅ 如果是支付者,扣除所有利润总和;其他钱包不扣利润,归集干净
|
|
535
575
|
if (i === maxSweepIndex && totalProfit > 0n) {
|
|
536
|
-
|
|
537
|
-
const othersProfit = totalProfit - ownProfit; // 其他钱包的利润总和
|
|
538
|
-
actualToSend = remaining - othersProfit; // 扣除其他钱包的利润
|
|
576
|
+
actualToSend = toSend - totalProfit; // 支付者扣除所有利润总和
|
|
539
577
|
}
|
|
540
578
|
else {
|
|
541
|
-
actualToSend =
|
|
579
|
+
actualToSend = toSend; // 其他钱包不扣利润,归集全部
|
|
542
580
|
}
|
|
543
581
|
}
|
|
544
582
|
// ✅ 支付者使用预留的第一个 nonce,其他钱包正常获取
|