four-flap-meme-sdk 1.7.60 → 1.7.61
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.
|
@@ -321,14 +321,17 @@ export async function bundleSwap(params) {
|
|
|
321
321
|
// ========================================
|
|
322
322
|
// 并行获取所有异步数据
|
|
323
323
|
// ========================================
|
|
324
|
-
const [nonces, feeData] = await Promise.all([
|
|
324
|
+
const [nonces, feeData, buyerBalance] = await Promise.all([
|
|
325
325
|
// 并行获取 seller 和 buyer 的 nonce
|
|
326
326
|
batchGetNonces([sellerWallet.address, buyerWallet.address], provider),
|
|
327
327
|
// 获取 gas 费用数据
|
|
328
328
|
provider.getFeeData(),
|
|
329
|
+
// 获取买家的 OKB 余额(普通模式下买家用自己的钱)
|
|
330
|
+
provider.getBalance(buyerWallet.address),
|
|
329
331
|
]);
|
|
330
332
|
const sellerNonce = nonces[0];
|
|
331
333
|
const buyerNonce = nonces[1];
|
|
334
|
+
console.log(`[Bundle Swap] 买家 OKB 余额: ${ethers.formatEther(buyerBalance)} OKB`);
|
|
332
335
|
// ========================================
|
|
333
336
|
// 同步签署授权(只需要卖家和买家,不需要 hop wallets)
|
|
334
337
|
// ========================================
|
|
@@ -342,16 +345,17 @@ export async function bundleSwap(params) {
|
|
|
342
345
|
// ========================================
|
|
343
346
|
// 同步构建调用
|
|
344
347
|
// ========================================
|
|
345
|
-
// ✅
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
348
|
+
// ✅ 计算买入金额(普通模式:买家用自己的 OKB)
|
|
349
|
+
// FLAP 模式需要使用买家的实际余额,扣除利润后的金额
|
|
350
|
+
const buyerAvailable = buyerBalance > profitAmount ? buyerBalance - profitAmount : 0n;
|
|
351
|
+
console.log(`[Bundle Swap] 买家可用金额(扣除利润后): ${ethers.formatEther(buyerAvailable)} OKB`);
|
|
352
|
+
// ✅ FLAP 模式需要买家有足够的 OKB
|
|
353
|
+
if (tradeType === 'FLAP' && buyerAvailable <= 0n) {
|
|
354
|
+
throw new Error('FLAP 内盘模式需要买家钱包有足够的 OKB');
|
|
351
355
|
}
|
|
352
356
|
const calls = [];
|
|
353
357
|
// ========================================
|
|
354
|
-
// ✅
|
|
358
|
+
// ✅ 普通换手模式逻辑
|
|
355
359
|
// 1. 卖家卖出代币 → OKB 留在卖家账户
|
|
356
360
|
// 2. 买家用自己预存的 OKB 买入代币
|
|
357
361
|
// 3. 利润从买家的 OKB 中扣除
|
|
@@ -362,15 +366,15 @@ export async function bundleSwap(params) {
|
|
|
362
366
|
// 2. 利润刮取(从买家余额扣除,在买入之前)
|
|
363
367
|
if (profitAmount > 0n) {
|
|
364
368
|
calls.push({
|
|
365
|
-
target: buyerWallet.address,
|
|
369
|
+
target: buyerWallet.address,
|
|
366
370
|
allowFailure: false,
|
|
367
371
|
value: 0n,
|
|
368
372
|
callData: delegateInterface.encodeFunctionData('transferAmount', [profitRecipient, profitAmount]),
|
|
369
373
|
});
|
|
370
374
|
}
|
|
371
375
|
// 3. 买家用自己预存的 OKB 买入
|
|
372
|
-
// ✅ FLAP
|
|
373
|
-
const buyAmountForCall = tradeType === 'FLAP' ?
|
|
376
|
+
// ✅ FLAP 模式使用买家实际余额(扣除利润后),V2/V3 模式使用 0n(合约自动使用全部余额)
|
|
377
|
+
const buyAmountForCall = tradeType === 'FLAP' ? buyerAvailable : 0n;
|
|
374
378
|
const buyCall = buildBuyCallInternal(buyerWallet, buyAmountForCall, tokenAddress, tradeType, actualRouter, fee, delegateInterface, portalInterface);
|
|
375
379
|
calls.push(buyCall);
|
|
376
380
|
// ========================================
|
|
@@ -443,12 +447,15 @@ export async function bundleBatchSwap(params) {
|
|
|
443
447
|
// 并行获取所有异步数据
|
|
444
448
|
// ========================================
|
|
445
449
|
const addressesToQuery = [sellerWallet.address, ...buyerWallets.map(w => w.address)];
|
|
446
|
-
const [nonces, feeData] = await Promise.all([
|
|
450
|
+
const [nonces, feeData, ...buyerBalances] = await Promise.all([
|
|
447
451
|
batchGetNonces(addressesToQuery, provider),
|
|
448
452
|
provider.getFeeData(),
|
|
453
|
+
// 获取所有买家的 OKB 余额(普通模式下买家用自己的钱)
|
|
454
|
+
...buyerWallets.map(w => provider.getBalance(w.address)),
|
|
449
455
|
]);
|
|
450
456
|
const sellerNonce = nonces[0];
|
|
451
457
|
const buyerNonces = nonces.slice(1);
|
|
458
|
+
console.log(`[Bundle Batch Swap] 买家余额:`, buyerBalances.map(b => ethers.formatEther(b)));
|
|
452
459
|
// ========================================
|
|
453
460
|
// 同步构建调用
|
|
454
461
|
// ========================================
|
|
@@ -467,35 +474,31 @@ export async function bundleBatchSwap(params) {
|
|
|
467
474
|
// 2. 利润刮取(从第一个买家扣除,在买入之前)
|
|
468
475
|
if (profitAmount > 0n) {
|
|
469
476
|
calls.push({
|
|
470
|
-
target: buyerWallets[0].address,
|
|
477
|
+
target: buyerWallets[0].address,
|
|
471
478
|
allowFailure: false,
|
|
472
479
|
value: 0n,
|
|
473
480
|
callData: delegateInterface.encodeFunctionData('transferAmount', [profitRecipient, profitAmount]),
|
|
474
481
|
});
|
|
475
482
|
}
|
|
476
|
-
// ✅ 3.
|
|
477
|
-
//
|
|
478
|
-
// 如果前端传入了 buyerRatios,按比例计算;否则均分
|
|
483
|
+
// ✅ 3. 计算每个买家的买入金额(普通模式:买家用自己的 OKB)
|
|
484
|
+
// FLAP 模式需要使用买家的实际余额
|
|
479
485
|
let buyAmountsPerBuyer;
|
|
480
|
-
if (
|
|
481
|
-
//
|
|
482
|
-
buyAmountsPerBuyer =
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
console.log(`[bundleBatchSwap] 按比例买入模式,买家将使用自己预存的 OKB 买入`);
|
|
486
|
+
if (tradeType === 'FLAP') {
|
|
487
|
+
// FLAP 模式:使用买家的实际 OKB 余额(第一个买家扣除利润)
|
|
488
|
+
buyAmountsPerBuyer = buyerBalances.map((balance, i) => {
|
|
489
|
+
const b = BigInt(balance.toString());
|
|
490
|
+
// 第一个买家需要扣除利润
|
|
491
|
+
return i === 0 ? (b > profitAmount ? b - profitAmount : 0n) : b;
|
|
492
|
+
});
|
|
493
|
+
console.log(`[bundleBatchSwap] FLAP 模式,买家买入金额:`, buyAmountsPerBuyer.map(a => ethers.formatEther(a)));
|
|
489
494
|
}
|
|
490
495
|
else {
|
|
491
|
-
//
|
|
496
|
+
// V2/V3 模式:使用 0n,合约自动使用全部余额
|
|
492
497
|
buyAmountsPerBuyer = buyerWallets.map(() => 0n);
|
|
493
498
|
}
|
|
494
499
|
// 4. 所有买家买入(使用自己预存的 OKB)
|
|
495
500
|
for (let i = 0; i < buyerWallets.length; i++) {
|
|
496
|
-
|
|
497
|
-
const buyAmountForCall = tradeType === 'FLAP' ? buyAmountsPerBuyer[i] : 0n;
|
|
498
|
-
const buyCall = buildBuyCallInternal(buyerWallets[i], buyAmountForCall, tokenAddress, tradeType, actualRouter, fee, delegateInterface, portalInterface);
|
|
501
|
+
const buyCall = buildBuyCallInternal(buyerWallets[i], buyAmountsPerBuyer[i], tokenAddress, tradeType, actualRouter, fee, delegateInterface, portalInterface);
|
|
499
502
|
calls.push(buyCall);
|
|
500
503
|
}
|
|
501
504
|
// ========================================
|