four-flap-meme-sdk 1.7.67 → 1.7.69
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,
|
|
@@ -771,11 +771,12 @@ export async function pairwiseTransfer(params) {
|
|
|
771
771
|
// 直接转账:sender → receiver
|
|
772
772
|
// ========================================
|
|
773
773
|
if (isNative) {
|
|
774
|
+
// ✅ 使用 transferAmount 转移指定金额(而不是 transferTo 全额)
|
|
774
775
|
calls.push({
|
|
775
776
|
target: senderWallet.address,
|
|
776
777
|
allowFailure: false,
|
|
777
778
|
value: shouldPassValue ? amtWei : 0n,
|
|
778
|
-
callData: delegateInterface.encodeFunctionData('
|
|
779
|
+
callData: delegateInterface.encodeFunctionData('transferAmount', [receiver, amtWei]),
|
|
779
780
|
});
|
|
780
781
|
}
|
|
781
782
|
else {
|
|
@@ -810,14 +811,14 @@ export async function pairwiseTransfer(params) {
|
|
|
810
811
|
const lastHop = hopWallets[hopWallets.length - 1];
|
|
811
812
|
if (isNative) {
|
|
812
813
|
// 原生 OKB 多跳
|
|
813
|
-
// sender →
|
|
814
|
+
// ✅ sender → 第一个中间钱包(使用 transferAmount 转移指定金额)
|
|
814
815
|
calls.push({
|
|
815
816
|
target: senderWallet.address,
|
|
816
817
|
allowFailure: false,
|
|
817
818
|
value: shouldPassValue ? amtWei : 0n,
|
|
818
|
-
callData: delegateInterface.encodeFunctionData('
|
|
819
|
+
callData: delegateInterface.encodeFunctionData('transferAmount', [firstHop.address, amtWei]),
|
|
819
820
|
});
|
|
820
|
-
//
|
|
821
|
+
// 中间钱包之间转账(使用 transferTo,因为中间钱包只有这笔钱)
|
|
821
822
|
for (let j = 0; j < hopWallets.length - 1; j++) {
|
|
822
823
|
calls.push({
|
|
823
824
|
target: hopWallets[j].address,
|
|
@@ -826,7 +827,7 @@ export async function pairwiseTransfer(params) {
|
|
|
826
827
|
callData: delegateInterface.encodeFunctionData('transferTo', [hopWallets[j + 1].address]),
|
|
827
828
|
});
|
|
828
829
|
}
|
|
829
|
-
// 最后一个中间钱包 → receiver
|
|
830
|
+
// 最后一个中间钱包 → receiver(使用 transferTo 转移全部)
|
|
830
831
|
calls.push({
|
|
831
832
|
target: lastHop.address,
|
|
832
833
|
allowFailure: false,
|