four-flap-meme-sdk 1.7.66 → 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.
|
@@ -36,10 +36,13 @@ export declare function estimateErc20TransferGas(provider: JsonRpcProvider, toke
|
|
|
36
36
|
export declare function estimateMaxErc20TransferGas(provider: JsonRpcProvider, tokenAddress: string, from: string, recipients: string[], amounts: bigint[], bufferPercent?: number): Promise<bigint>;
|
|
37
37
|
/**
|
|
38
38
|
* 计算 Gas Limit
|
|
39
|
-
* - 原生代币转账:
|
|
39
|
+
* - 原生代币转账:
|
|
40
|
+
* - BSC: 21000 gas(固定)
|
|
41
|
+
* - XLayer: 50000 gas(EIP-7702 授权地址需要更多 gas)
|
|
40
42
|
* - ERC20 标准 transfer:约 45000-55000 gas,使用 55000 作为安全值
|
|
41
43
|
*
|
|
42
44
|
* ✅ 优化:降低 ERC20 gas limit,减少中转钱包 BNB 残留
|
|
45
|
+
* ✅ XLayer 特殊处理:EIP-7702 授权的地址接收 OKB 时会触发 delegate 代码,需要更多 gas
|
|
43
46
|
*/
|
|
44
47
|
export declare function calculateGasLimit(config: any, isNative: boolean, hasHops: boolean, hopCount?: number): bigint;
|
|
45
48
|
export declare function isNativeTokenAddress(tokenAddress?: string): boolean;
|
|
@@ -195,17 +195,25 @@ export async function estimateMaxErc20TransferGas(provider, tokenAddress, from,
|
|
|
195
195
|
}
|
|
196
196
|
/**
|
|
197
197
|
* 计算 Gas Limit
|
|
198
|
-
* - 原生代币转账:
|
|
198
|
+
* - 原生代币转账:
|
|
199
|
+
* - BSC: 21000 gas(固定)
|
|
200
|
+
* - XLayer: 50000 gas(EIP-7702 授权地址需要更多 gas)
|
|
199
201
|
* - ERC20 标准 transfer:约 45000-55000 gas,使用 55000 作为安全值
|
|
200
202
|
*
|
|
201
203
|
* ✅ 优化:降低 ERC20 gas limit,减少中转钱包 BNB 残留
|
|
204
|
+
* ✅ XLayer 特殊处理:EIP-7702 授权的地址接收 OKB 时会触发 delegate 代码,需要更多 gas
|
|
202
205
|
*/
|
|
203
206
|
export function calculateGasLimit(config, isNative, hasHops, hopCount = 0) {
|
|
204
207
|
if (config.gasLimit !== undefined) {
|
|
205
208
|
return BigInt(config.gasLimit);
|
|
206
209
|
}
|
|
207
|
-
// ✅
|
|
208
|
-
const
|
|
210
|
+
// ✅ XLayer 链(chainId=196)原生代币需要更多 gas(EIP-7702 delegate 执行)
|
|
211
|
+
const isXLayer = config.chainId === 196;
|
|
212
|
+
// ✅ 原生代币: BSC 21000, XLayer 50000(EIP-7702 安全)
|
|
213
|
+
// ✅ ERC20 标准 transfer: 48000(USDT 最低约 46815)
|
|
214
|
+
const baseGas = isNative
|
|
215
|
+
? (isXLayer ? 50000 : 21000)
|
|
216
|
+
: 46815;
|
|
209
217
|
// ✅ 多跳时只需要累加单次转账的 gas,不需要额外乘数
|
|
210
218
|
// 每个中转钱包只执行一笔 transfer
|
|
211
219
|
if (hasHops && hopCount > 0) {
|
|
@@ -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,
|