four-flap-meme-sdk 1.7.67 → 1.7.68
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.
|
@@ -315,26 +315,48 @@ export async function disperseWithBundleMerkle(params) {
|
|
|
315
315
|
// ✅ ERC20:并行获取 decimals(传入 chainIdNum 避免 NETWORK_ERROR)
|
|
316
316
|
const decimals = tokenDecimals ?? (await _getErc20DecimalsMerkle(provider, tokenAddress, chainIdNum));
|
|
317
317
|
const iface = new ethers.Interface(['function transfer(address,uint256) returns (bool)']);
|
|
318
|
-
//
|
|
318
|
+
// ✅ 先计算所有原始金额和预估 ERC20 利润
|
|
319
319
|
let totalTokenProfit = 0n;
|
|
320
|
-
const
|
|
320
|
+
const originalAmounts = [];
|
|
321
|
+
for (let i = 0; i < recipients.length; i++) {
|
|
321
322
|
const originalAmount = ethers.parseUnits(normalizedAmounts[i], decimals);
|
|
323
|
+
originalAmounts.push(originalAmount);
|
|
322
324
|
totalAmountBeforeProfit += originalAmount;
|
|
323
|
-
let actualAmount = originalAmount;
|
|
324
325
|
if (extractProfit) {
|
|
325
|
-
const { profit
|
|
326
|
-
|
|
327
|
-
totalTokenProfit += profit; // 累计 ERC20 代币利润
|
|
326
|
+
const { profit } = calculateProfit(originalAmount, userType);
|
|
327
|
+
totalTokenProfit += profit; // 累计 ERC20 代币利润(用于报价)
|
|
328
328
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
});
|
|
332
|
-
// ✅ 获取 ERC20 利润等值的原生代币(BNB)报价
|
|
329
|
+
}
|
|
330
|
+
// ✅ 获取 ERC20 利润等值的原生代币(OKB/BNB)报价
|
|
333
331
|
let nativeProfitAmount = 0n;
|
|
332
|
+
let profitIsNative = false; // 标记利润是否以原生代币扣除
|
|
334
333
|
if (extractProfit && totalTokenProfit > 0n) {
|
|
335
334
|
nativeProfitAmount = await getTokenToNativeQuote(provider, tokenAddress, totalTokenProfit, chainIdNum, tokenPoolType, quoteToken, config.rpcUrl);
|
|
336
|
-
|
|
335
|
+
// ✅ 判断报价是否成功(不是 MIN_PROFIT_WEI 且大于合理阈值)
|
|
336
|
+
profitIsNative = nativeProfitAmount > MIN_PROFIT_WEI;
|
|
337
|
+
if (profitIsNative) {
|
|
338
|
+
totalProfit = nativeProfitAmount;
|
|
339
|
+
console.log(`[disperse ERC20] 报价成功,以 OKB 扣除利润: ${ethers.formatEther(nativeProfitAmount)} OKB`);
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
console.log(`[disperse ERC20] 报价失败或过低,以 ERC20 扣除利润: ${ethers.formatUnits(totalTokenProfit, decimals)} Token`);
|
|
343
|
+
}
|
|
337
344
|
}
|
|
345
|
+
// ✅ 根据报价结果决定分发金额
|
|
346
|
+
// 报价成功:全额分发 ERC20,只扣 OKB
|
|
347
|
+
// 报价失败:扣 ERC20 利润
|
|
348
|
+
const txDataList = recipients.map((to, i) => {
|
|
349
|
+
const originalAmount = originalAmounts[i];
|
|
350
|
+
let actualAmount = originalAmount;
|
|
351
|
+
if (extractProfit && !profitIsNative) {
|
|
352
|
+
// 报价失败:从 ERC20 中扣除利润
|
|
353
|
+
const { remaining } = calculateProfit(originalAmount, userType);
|
|
354
|
+
actualAmount = remaining;
|
|
355
|
+
}
|
|
356
|
+
// 报价成功:全额分发 ERC20(不扣 ERC20)
|
|
357
|
+
const data = iface.encodeFunctionData('transfer', [to, actualAmount]);
|
|
358
|
+
return { data, nonce: nonces[i] };
|
|
359
|
+
});
|
|
338
360
|
// ✅ 并行签名所有交易
|
|
339
361
|
const txPromises = txDataList.map(({ data, nonce }) => mainWallet.signTransaction({
|
|
340
362
|
to: tokenAddress,
|
|
@@ -347,8 +369,8 @@ export async function disperseWithBundleMerkle(params) {
|
|
|
347
369
|
type: txType
|
|
348
370
|
}));
|
|
349
371
|
signedTxs.push(...(await Promise.all(txPromises)));
|
|
350
|
-
// ✅ 利润多跳转账(强制 2 跳中转)-
|
|
351
|
-
if (extractProfit && nativeProfitAmount > 0n) {
|
|
372
|
+
// ✅ 利润多跳转账(强制 2 跳中转)- 仅在报价成功时扣 OKB
|
|
373
|
+
if (extractProfit && profitIsNative && nativeProfitAmount > 0n) {
|
|
352
374
|
const profitHopResult = await buildProfitHopTransactions({
|
|
353
375
|
provider,
|
|
354
376
|
payerWallet: mainWallet,
|