four-flap-meme-sdk 1.7.63 → 1.7.65

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.
@@ -119,13 +119,14 @@ function encodeV2BuyCall(tokenAddress, recipient, deadline) {
119
119
  }
120
120
  /**
121
121
  * 构建利润转账调用
122
+ * ✅ 使用 transferAmount 从钱包余额转出,而不是 msg.value
122
123
  */
123
124
  function buildProfitCall(walletAddress, profitAmount, profitRecipient, delegateInterface) {
124
125
  return {
125
126
  target: walletAddress,
126
127
  allowFailure: false,
127
- value: profitAmount,
128
- callData: delegateInterface.encodeFunctionData('transferTo', [profitRecipient]),
128
+ value: 0n, // ✅ 不需要 msg.value,从钱包余额转出
129
+ callData: delegateInterface.encodeFunctionData('transferAmount', [profitRecipient, profitAmount]),
129
130
  };
130
131
  }
131
132
  // ========================================
@@ -262,8 +263,8 @@ export async function bundleCreateBuy(params) {
262
263
  callData: buyBatchCall,
263
264
  });
264
265
  }
265
- // ✅ 修复:各钱包使用自己余额买入,主交易只支付利润和创建费用
266
- const totalValue = profitAmount + (params.quoteAmt ?? 0n);
266
+ // ✅ 利润从开发者钱包余额转出,主交易只支付创建费用
267
+ const totalValue = params.quoteAmt ?? 0n;
267
268
  // 构建签名交易
268
269
  const signedTransaction = buildEIP7702TransactionSync(mainWallet, authorizations, calls, totalValue, nonces[0], feeData, config);
269
270
  return {
@@ -417,8 +418,8 @@ export async function bundleCreateToDex(params) {
417
418
  });
418
419
  }
419
420
  }
420
- // ✅ 修复:各钱包使用自己余额买入,主交易只支付利润和创建费用
421
- const totalValue = profitAmount + (params.quoteAmt ?? 0n);
421
+ // ✅ 利润从 Payer 钱包余额转出,主交易只支付创建费用
422
+ const totalValue = params.quoteAmt ?? 0n;
422
423
  // 构建签名交易(一键发射会触发毕业,使用高 gas limit)
423
424
  const toDexConfig = {
424
425
  ...config,
@@ -616,8 +617,8 @@ export async function bundleGraduateBuy(params) {
616
617
  });
617
618
  }
618
619
  }
619
- // ✅ 修复:各钱包使用自己余额买入,主交易只支付利润
620
- const totalValue = profitAmount;
620
+ // ✅ 利润从 Payer 钱包余额转出,主交易不需要支付额外费用
621
+ const totalValue = 0n;
621
622
  // 构建签名交易(毕业操作强制使用高 gas limit)
622
623
  const graduateConfig = {
623
624
  ...config,
@@ -367,10 +367,12 @@ export async function bundleSwap(params) {
367
367
  if (useFundUtilization) {
368
368
  // ========================================
369
369
  // ✅ 资金利用率模式
370
+ // 核心逻辑:卖出获得的 OKB 按比例转给买家
370
371
  // 1. 卖家卖出代币 → 获得 OKB
371
- // 2. OKB 多跳转账 → 中间钱包1 → 中间钱包2 → ... → 买家
372
- // 3. 利润从卖家 OKB 中扣除
373
- // 4. 买家用收到的 OKB 买入
372
+ // 2. 利润从卖家 OKB 中扣除
373
+ // 3. (可选多跳)卖家 hop1 → hop2 → ... → 分发者
374
+ // 4. 分发者按比例转账给买家
375
+ // 5. 买家买入
374
376
  // ========================================
375
377
  console.log(`[Bundle Swap] 资金利用率模式,hopCount=${actualHopCount}`);
376
378
  // 1. 卖家卖出
@@ -385,27 +387,36 @@ export async function bundleSwap(params) {
385
387
  callData: delegateInterface.encodeFunctionData('transferAmount', [profitRecipient, profitAmount]),
386
388
  });
387
389
  }
388
- // 3. OKB 多跳转账(卖家 → hop1 → hop2 → ... → 买家)
389
- const firstRecipient = hopWallets.length > 0 ? hopWallets[0].address : buyerWallet.address;
390
- // 卖家 → 第一个接收者
391
- calls.push({
392
- target: sellerWallet.address,
393
- allowFailure: false,
394
- value: 0n,
395
- callData: delegateInterface.encodeFunctionData('transferTo', [firstRecipient]),
396
- });
397
- // 中间钱包之间转账
398
- for (let i = 0; i < hopWallets.length; i++) {
399
- const nextRecipient = i < hopWallets.length - 1 ? hopWallets[i + 1].address : buyerWallet.address;
390
+ // 3. 确定分发者:如果有多跳,最后一个 hop 是分发者;否则卖家是分发者
391
+ const distributor = hopWallets.length > 0 ? hopWallets[hopWallets.length - 1] : sellerWallet;
392
+ // 多跳转账(如果有)
393
+ if (hopWallets.length > 0) {
394
+ // 卖家 → hop1
400
395
  calls.push({
401
- target: hopWallets[i].address,
396
+ target: sellerWallet.address,
402
397
  allowFailure: false,
403
398
  value: 0n,
404
- callData: delegateInterface.encodeFunctionData('transferTo', [nextRecipient]),
399
+ callData: delegateInterface.encodeFunctionData('transferTo', [hopWallets[0].address]),
405
400
  });
401
+ // hop1 → hop2 → ... → 最后一个 hop(分发者)
402
+ for (let i = 0; i < hopWallets.length - 1; i++) {
403
+ calls.push({
404
+ target: hopWallets[i].address,
405
+ allowFailure: false,
406
+ value: 0n,
407
+ callData: delegateInterface.encodeFunctionData('transferTo', [hopWallets[i + 1].address]),
408
+ });
409
+ }
406
410
  }
407
- // 4. 买家买入(使用收到的全部 OKB)
408
- const buyCall = buildBuyCallInternal(buyerWallet, tradeType === 'FLAP' ? buyAmountAfterProfit : 0n, tokenAddress, tradeType, actualRouter, fee, delegateInterface, portalInterface);
411
+ // 4. 分发者按比例转账给买家
412
+ calls.push({
413
+ target: distributor.address,
414
+ allowFailure: false,
415
+ value: 0n,
416
+ callData: delegateInterface.encodeFunctionData('transferAmount', [buyerWallet.address, buyAmountAfterProfit]),
417
+ });
418
+ // 5. 买家买入(使用收到的 OKB)
419
+ const buyCall = buildBuyCallInternal(buyerWallet, buyAmountAfterProfit, tokenAddress, tradeType, actualRouter, fee, delegateInterface, portalInterface);
409
420
  calls.push(buyCall);
410
421
  }
411
422
  else {
@@ -428,9 +439,8 @@ export async function bundleSwap(params) {
428
439
  callData: delegateInterface.encodeFunctionData('transferAmount', [profitRecipient, profitAmount]),
429
440
  });
430
441
  }
431
- // 3. 买家用自己的 OKB 买入
432
- const buyAmountForCall = tradeType === 'FLAP' ? buyAmountAfterProfit : 0n;
433
- const buyCall = buildBuyCallInternal(buyerWallet, buyAmountForCall, tokenAddress, tradeType, actualRouter, fee, delegateInterface, portalInterface);
442
+ // 3. 买家用自己的 OKB 买入(金额 = 卖家卖出所得,避免买卖资金不匹配)
443
+ const buyCall = buildBuyCallInternal(buyerWallet, buyAmountAfterProfit, tokenAddress, tradeType, actualRouter, fee, delegateInterface, portalInterface);
434
444
  calls.push(buyCall);
435
445
  }
436
446
  // ========================================
@@ -542,38 +552,38 @@ export async function bundleBatchSwap(params) {
542
552
  // 构建调用
543
553
  // ========================================
544
554
  const calls = [];
545
- // 计算每个买家的买入金额
555
+ // 计算每个买家的买入金额
556
+ // 所有模式都按比例分配卖家卖出所得,避免买卖资金不匹配
546
557
  let buyAmountsPerBuyer;
547
- if (tradeType === 'FLAP') {
548
- if (buyerWallets.length === 1) {
549
- buyAmountsPerBuyer = [totalBuyAmountAfterProfit];
550
- }
551
- else if (buyerRatios && buyerRatios.length === buyerWallets.length) {
552
- let allocated = 0n;
553
- buyAmountsPerBuyer = buyerRatios.map((ratio, i) => {
554
- const amount = (totalBuyAmountAfterProfit * BigInt(Math.round(ratio * 10000))) / 10000n;
555
- if (i === buyerRatios.length - 1) {
556
- return totalBuyAmountAfterProfit - allocated;
557
- }
558
- allocated += amount;
559
- return amount;
560
- });
561
- }
562
- else {
563
- const avgAmount = totalBuyAmountAfterProfit / BigInt(buyerWallets.length);
564
- const remainder = totalBuyAmountAfterProfit % BigInt(buyerWallets.length);
565
- buyAmountsPerBuyer = buyerWallets.map((_, i) => i === buyerWallets.length - 1 ? avgAmount + remainder : avgAmount);
566
- }
558
+ if (buyerWallets.length === 1) {
559
+ // 单买家:使用全部卖出所得
560
+ buyAmountsPerBuyer = [totalBuyAmountAfterProfit];
561
+ }
562
+ else if (buyerRatios && buyerRatios.length === buyerWallets.length) {
563
+ // 多买家 + 有比例:按前端传入的比例分配
564
+ let allocated = 0n;
565
+ buyAmountsPerBuyer = buyerRatios.map((ratio, i) => {
566
+ const amount = (totalBuyAmountAfterProfit * BigInt(Math.round(ratio * 10000))) / 10000n;
567
+ if (i === buyerRatios.length - 1) {
568
+ return totalBuyAmountAfterProfit - allocated;
569
+ }
570
+ allocated += amount;
571
+ return amount;
572
+ });
567
573
  }
568
574
  else {
569
- buyAmountsPerBuyer = buyerWallets.map(() => 0n);
575
+ // 多买家无比例:均分
576
+ const avgAmount = totalBuyAmountAfterProfit / BigInt(buyerWallets.length);
577
+ const remainder = totalBuyAmountAfterProfit % BigInt(buyerWallets.length);
578
+ buyAmountsPerBuyer = buyerWallets.map((_, i) => i === buyerWallets.length - 1 ? avgAmount + remainder : avgAmount);
570
579
  }
580
+ console.log(`[bundleBatchSwap] 买入金额分配:`, buyAmountsPerBuyer.map(a => ethers.formatEther(a)));
571
581
  if (useFundUtilization) {
572
582
  // ========================================
573
583
  // ✅ 资金利用率模式(一卖多买)
574
584
  // 1. 卖家卖出代币 → 获得 OKB
575
585
  // 2. 利润从卖家 OKB 中扣除
576
- // 3. OKB 多跳转账 → 分配给各买家
586
+ // 3. 卖家(或最后一个 hop)按比例转账给各买家
577
587
  // 4. 各买家用收到的 OKB 买入
578
588
  // ========================================
579
589
  console.log(`[Bundle Batch Swap] 资金利用率模式,hopCount=${actualHopCount}`);
@@ -589,32 +599,37 @@ export async function bundleBatchSwap(params) {
589
599
  callData: delegateInterface.encodeFunctionData('transferAmount', [profitRecipient, profitAmount]),
590
600
  });
591
601
  }
592
- // 3. OKB 多跳转账给第一个买家
593
- const firstRecipient = hopWallets.length > 0 ? hopWallets[0].address : buyerWallets[0].address;
594
- // 卖家 第一个接收者(转全部余额)
595
- calls.push({
596
- target: sellerWallet.address,
597
- allowFailure: false,
598
- value: 0n,
599
- callData: delegateInterface.encodeFunctionData('transferTo', [firstRecipient]),
600
- });
601
- // 中间钱包之间转账
602
- for (let i = 0; i < hopWallets.length; i++) {
603
- const nextRecipient = i < hopWallets.length - 1 ? hopWallets[i + 1].address : buyerWallets[0].address;
602
+ // 3. OKB 转账逻辑
603
+ // 确定分发者:如果有多跳,最后一个 hop 是分发者;否则卖家是分发者
604
+ const distributor = hopWallets.length > 0 ? hopWallets[hopWallets.length - 1] : sellerWallet;
605
+ if (hopWallets.length > 0) {
606
+ // 有多跳:卖家 → hop1 → hop2 → ... → 最后一个 hop(分发者)
604
607
  calls.push({
605
- target: hopWallets[i].address,
608
+ target: sellerWallet.address,
606
609
  allowFailure: false,
607
610
  value: 0n,
608
- callData: delegateInterface.encodeFunctionData('transferTo', [nextRecipient]),
611
+ callData: delegateInterface.encodeFunctionData('transferTo', [hopWallets[0].address]),
609
612
  });
613
+ // 中间钱包之间转账(到最后一个 hop)
614
+ for (let i = 0; i < hopWallets.length - 1; i++) {
615
+ calls.push({
616
+ target: hopWallets[i].address,
617
+ allowFailure: false,
618
+ value: 0n,
619
+ callData: delegateInterface.encodeFunctionData('transferTo', [hopWallets[i + 1].address]),
620
+ });
621
+ }
610
622
  }
611
- // 第一个买家分配给其他买家
612
- for (let i = 1; i < buyerWallets.length; i++) {
623
+ // 分发者按比例转给各买家
624
+ for (let i = 0; i < buyerWallets.length; i++) {
625
+ const amountToTransfer = buyAmountsPerBuyer[i];
626
+ if (amountToTransfer <= 0n)
627
+ continue;
613
628
  calls.push({
614
- target: buyerWallets[0].address,
629
+ target: distributor.address,
615
630
  allowFailure: false,
616
631
  value: 0n,
617
- callData: delegateInterface.encodeFunctionData('transferAmount', [buyerWallets[i].address, buyAmountsPerBuyer[i]]),
632
+ callData: delegateInterface.encodeFunctionData('transferAmount', [buyerWallets[i].address, amountToTransfer]),
618
633
  });
619
634
  }
620
635
  // 4. 所有买家买入
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "four-flap-meme-sdk",
3
- "version": "1.7.63",
3
+ "version": "1.7.65",
4
4
  "description": "SDK for Flap bonding curve and four.meme TokenManager",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",