four-flap-meme-sdk 4.5.3 → 4.5.4

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,6 +36,8 @@ export type DisperseSignParams = {
36
36
  config: FourSignConfig;
37
37
  /** ✅ 新增:起始 nonce(用于并行调用时避免 nonce 冲突) */
38
38
  startNonce?: number;
39
+ /** 代币转账税(bps)。ERC20 多跳时每个中转钱包只转出实际到账的数量,否则带税代币整包回滚;不传则读代币的 transferTaxBps() 等 view */
40
+ transferTaxBps?: number;
39
41
  /** ✅ 新增:代币池子类型(用于利润报价),默认 'v2' */
40
42
  tokenPoolType?: TokenPoolType;
41
43
  /** ✅ 新增:报价代币类型(池子的计价代币),默认 'native'(BNB/MON) */
@@ -1,4 +1,6 @@
1
1
  import type { DisperseSignParams, DisperseMerkleResult } from './types/index.js';
2
+ /** 扣转账税后的到账数量;税额向上取整,保证转出不超过实际余额 */
3
+ export declare function amountAfterTransferTax(amount: bigint, taxBps: number): bigint;
2
4
  /**
3
5
  * 分发(仅签名版本 - 不依赖 Merkle)
4
6
  * ✅ 精简版:只负责签名交易,不提交到 Merkle
@@ -2,16 +2,42 @@ import { ethers, Wallet } from 'ethers';
2
2
  import { getOptimizedGasPrice, NonceManager, buildProfitHopTransactions, PROFIT_HOP_COUNT, } from '../../utils/bundle-helpers.js';
3
3
  import { calculateTransferFee, floorErc20TransferGasLimit, GAS_LIMITS, isBscChain } from '../../core/constants/index.js';
4
4
  import { getGasPriceConfig, shouldExtractProfit, getProfitRecipient, getBribeAmount } from '../config-helpers.js';
5
+ import { normalizeFeeToBps } from '../../domains/lp-inspect/inspect-tax.js';
5
6
  import { getErc20Decimals as _getErc20Decimals, generateHopWallets as _generateHopWallets, normalizeAmounts as _normalizeAmounts, calculateGasLimit as _calculateGasLimit, estimateSweepLimits, isNativeTokenAddress as _isNativeTokenAddress, } from '../internal.js';
6
7
  import { normalizeItemsOrRecipientsAmounts, resolveHopCountInput, resolveProvidedHops, resolveSignContext, signBribeTransaction, } from '../../core/sign-context-helpers.js';
7
8
  // ==================== 钱包转账按地址收费 ====================
9
+ const TRANSFER_TAX_FNS = ['transferTaxBps', 'transferTax', 'transferFee'];
10
+ /**
11
+ * 读代币上明确声明的转账税(钱包间互转)。买卖税不算:只收买卖税的币多跳不能少转。
12
+ * 读不到返回 0,行为与不传税率一致。
13
+ */
14
+ async function readTransferTaxBps(provider, token) {
15
+ const contract = new ethers.Contract(token, TRANSFER_TAX_FNS.map((name) => `function ${name}() view returns (uint256)`), provider);
16
+ const reads = await Promise.allSettled(TRANSFER_TAX_FNS.map(async (name) => Number(await contract.getFunction(name).staticCall())));
17
+ let bps = 0;
18
+ for (const [i, item] of reads.entries()) {
19
+ if (item.status !== 'fulfilled')
20
+ continue;
21
+ const value = TRANSFER_TAX_FNS[i] === 'transferTaxBps' ? item.value : normalizeFeeToBps(item.value);
22
+ if (typeof value === 'number' && value > 0 && value < 10000)
23
+ bps = Math.max(bps, value);
24
+ }
25
+ return bps;
26
+ }
27
+ /** 扣转账税后的到账数量;税额向上取整,保证转出不超过实际余额 */
28
+ export function amountAfterTransferTax(amount, taxBps) {
29
+ if (taxBps <= 0)
30
+ return amount;
31
+ const tax = (amount * BigInt(taxBps) + 9999n) / 10000n;
32
+ return amount - tax;
33
+ }
8
34
  /**
9
35
  * 分发(仅签名版本 - 不依赖 Merkle)
10
36
  * ✅ 精简版:只负责签名交易,不提交到 Merkle
11
37
  * ✅ 优化版:支持 startNonce 参数避免并行调用时的 nonce 冲突
12
38
  */
13
39
  export async function disperseWithBundleMerkle(params) {
14
- const { fromPrivateKey, recipients, amount, amounts, tokenAddress, tokenDecimals, hopCount = 0, hopPrivateKeys, items, config, startNonce, } = params;
40
+ const { fromPrivateKey, recipients, amount, amounts, tokenAddress, tokenDecimals, hopCount = 0, hopPrivateKeys, items, config, startNonce, transferTaxBps: transferTaxBpsParam, } = params;
15
41
  // 快速返回空结果
16
42
  if (!recipients || recipients.length === 0) {
17
43
  return {
@@ -142,12 +168,15 @@ export async function disperseWithBundleMerkle(params) {
142
168
  // ✅ BSC 链贿赂支持
143
169
  const bribeAmountHop = isBscChain(chainIdNum) ? getBribeAmount(config) : 0n;
144
170
  const needBribeTxHop = bribeAmountHop > 0n;
145
- // ✅ 优化:并行获取 gasPrice 和 decimals
146
- const [gasPrice, decimals] = await Promise.all([
171
+ // ✅ 优化:并行获取 gasPrice、decimals 和 ERC20 转账税
172
+ const [gasPrice, decimals, transferTaxBps] = await Promise.all([
147
173
  getOptimizedGasPrice(provider, getGasPriceConfig(config)),
148
174
  isNative
149
175
  ? Promise.resolve(18)
150
176
  : Promise.resolve(tokenDecimals ?? (await _getErc20Decimals(provider, tokenAddress, chainIdNum))),
177
+ isNative || transferTaxBpsParam !== undefined
178
+ ? Promise.resolve(transferTaxBpsParam ?? 0)
179
+ : readTransferTaxBps(provider, tokenAddress).catch(() => 0),
151
180
  ]);
152
181
  const iface = isNative ? null : new ethers.Interface(['function transfer(address,uint256) returns (bool)']);
153
182
  // ✅ Gas limit 设置
@@ -317,6 +346,11 @@ export async function disperseWithBundleMerkle(params) {
317
346
  },
318
347
  });
319
348
  // 第三步:中转钱包逐层传递(gas 和 ERC20)
349
+ // hopAmounts[j] = 第 j 个钱包实际收到、可以转出的数量(带税代币逐跳递减)
350
+ const hopAmounts = [amountWei];
351
+ for (let j = 1; j < fullChain.length; j++) {
352
+ hopAmounts.push(amountAfterTransferTax(hopAmounts[j - 1], transferTaxBps));
353
+ }
320
354
  for (let j = 1; j < fullChain.length; j++) {
321
355
  const fromWallet = fullChain[j];
322
356
  const toAddress = addresses[j + 1]; // 下一个地址(可能是中转钱包或最终接收者)
@@ -336,8 +370,8 @@ export async function disperseWithBundleMerkle(params) {
336
370
  type: txType,
337
371
  },
338
372
  });
339
- // 再转 ERC20
340
- const erc20Data = iface.encodeFunctionData('transfer', [toAddress, amountWei]);
373
+ // 再转 ERC20(只转上一跳实际到账的数量)
374
+ const erc20Data = iface.encodeFunctionData('transfer', [toAddress, hopAmounts[j]]);
341
375
  txsToSign.push({
342
376
  wallet: fromWallet,
343
377
  tx: {
@@ -354,7 +388,7 @@ export async function disperseWithBundleMerkle(params) {
354
388
  }
355
389
  else {
356
390
  // 最后一个中转钱包:只转 ERC20 给最终接收者
357
- const erc20Data = iface.encodeFunctionData('transfer', [toAddress, amountWei]);
391
+ const erc20Data = iface.encodeFunctionData('transfer', [toAddress, hopAmounts[j]]);
358
392
  txsToSign.push({
359
393
  wallet: fromWallet,
360
394
  tx: {
@@ -6,7 +6,7 @@ import { generateWallets } from '../../utils/wallet.js';
6
6
  import { NonceManager, getOptimizedGasPrice, buildProfitHopTransactions, PROFIT_HOP_COUNT } from '../../utils/bundle-helpers.js';
7
7
  import { BLOCKRAZOR_BUILDER_EOA, getProfitRecipient, getTransferFeePerAddress, } from '../../core/constants/index.js';
8
8
  import { getErrorMessageFromUnknown } from '../../core/index.js';
9
- import { DEFAULT_GAS_LIMIT, DEFAULT_GAS_PRICE_GWEI, DEFAULT_MAX_WALLETS_PER_BATCH_ERC20, DEFAULT_MAX_WALLETS_PER_BATCH_NATIVE, GAS_BUFFER_MULTIPLIER, NATIVE_TRANSFER_GAS_LIMIT, buildDaoaasBuyTx, buildDisperseHopChainERC20, buildDisperseHopChainNative, buildERC20ApproveTx, buildFlapBuyTx, buildFourBuyTx, buildNativeTransferTx, buildV2BuyTx, buildV2BuyTxWithERC20, buildV3BuyTx, buildV3BuyTxWithERC20, calculateMaxWalletsPerBatch, chunkArray, generateDisperseHopPaths, getBaseTokenAddress, getRouterAddress, } from './holders-maker-helpers.js';
9
+ import { DEFAULT_GAS_LIMIT, DEFAULT_GAS_PRICE_GWEI, DEFAULT_MAX_WALLETS_PER_BATCH_ERC20, DEFAULT_MAX_WALLETS_PER_BATCH_NATIVE, ERC20_APPROVE_GAS_LIMIT, GAS_BUFFER_MULTIPLIER, NATIVE_TRANSFER_GAS_LIMIT, buildDaoaasBuyTx, buildDisperseHopChainERC20, buildDisperseHopChainNative, buildERC20ApproveTx, buildFlapBuyTx, buildFourBuyTx, buildNativeTransferTx, buildV2BuyTx, buildV2BuyTxWithERC20, buildV3BuyTx, buildV3BuyTxWithERC20, calculateMaxWalletsPerBatch, chunkArray, generateDisperseHopPaths, getBaseTokenAddress, getRouterAddress, } from './holders-maker-helpers.js';
10
10
  function formatUnitsFixed(value, decimals, precision = 6) {
11
11
  const raw = ethers.formatUnits(value, decimals);
12
12
  const [intPart, fracPart = ''] = raw.split('.');
@@ -65,18 +65,16 @@ export async function holdersMaker(params) {
65
65
  const gasPriceWei = parseGasPriceGwei(gasPriceGwei);
66
66
  const gasPrice = await getOptimizedGasPrice(provider, { minGasPrice: gasPriceWei });
67
67
  const bribeAmountWei = ethers.parseEther(String(bribeAmount));
68
- // ERC20 模式需要更多 gas(授权 + 购买)
69
- const gasMultiplier = isERC20Mode ? 2 : 1;
70
- const gasFeePerWallet = BigInt(gasLimit) * gasPrice * BigInt(gasMultiplier);
71
- const gasWithBuffer = gasFeePerWallet * BigInt(GAS_BUFFER_MULTIPLIER);
68
+ // 原生模式:买入 gas 留缓冲
69
+ const gasWithBuffer = BigInt(gasLimit) * gasPrice * BigInt(GAS_BUFFER_MULTIPLIER);
72
70
  let buyAmountWei;
73
71
  let transferNativePerWallet;
74
72
  let erc20TokenAddress;
75
73
  if (isERC20Mode) {
76
74
  // ERC20 模式:解析购买金额(考虑精度)
77
75
  buyAmountWei = ethers.parseUnits(buyAmountPerHolder, baseTokenDecimals);
78
- // 只需要分发 gas 费
79
- transferNativePerWallet = gasWithBuffer;
76
+ // 只转够 gas:授权 + 买入两笔的 gasLimit × gasPrice(maxFee=gasPrice,节点预检按上限扣,不再额外翻倍)
77
+ transferNativePerWallet = (ERC20_APPROVE_GAS_LIMIT + BigInt(gasLimit)) * gasPrice;
80
78
  // 获取 ERC20 地址
81
79
  erc20TokenAddress = getBaseTokenAddress(baseToken, chain, baseTokenAddress);
82
80
  }
@@ -287,17 +285,15 @@ export async function holdersMaker(params) {
287
285
  export function estimateHoldersMakerCost(params) {
288
286
  const { holdersCount, buyAmountPerHolder, baseToken = 'native', gasLimit = DEFAULT_GAS_LIMIT, gasPriceGwei = DEFAULT_GAS_PRICE_GWEI, } = params;
289
287
  const isERC20Mode = baseToken !== 'native';
290
- const gasMultiplier = isERC20Mode ? 2 : 1; // ERC20 模式需要授权+购买
291
288
  const gasPriceWei = parseGasPriceGwei(gasPriceGwei);
292
- const gasFeePerWalletWei = BigInt(gasLimit) * gasPriceWei * BigInt(gasMultiplier);
293
- const gasWithBufferWei = gasFeePerWalletWei * BigInt(GAS_BUFFER_MULTIPLIER);
289
+ const gasWithBufferWei = BigInt(gasLimit) * gasPriceWei * BigInt(GAS_BUFFER_MULTIPLIER);
294
290
  const buyAmountWei = ethers.parseUnits(buyAmountPerHolder, 18);
295
291
  // 批次数
296
292
  const maxPerBatch = isERC20Mode ? DEFAULT_MAX_WALLETS_PER_BATCH_ERC20 : DEFAULT_MAX_WALLETS_PER_BATCH_NATIVE;
297
293
  const batchCount = Math.ceil(holdersCount / maxPerBatch);
298
294
  if (isERC20Mode) {
299
- // ERC20 模式:原生代币只用于 gas
300
- const totalNativeCostWei = gasWithBufferWei * BigInt(holdersCount);
295
+ // ERC20 模式:原生代币只用于 gas(与 holdersMaker 实际分发一致)
296
+ const totalNativeCostWei = (ERC20_APPROVE_GAS_LIMIT + BigInt(gasLimit)) * gasPriceWei * BigInt(holdersCount);
301
297
  const totalERC20CostWei = buyAmountWei * BigInt(holdersCount);
302
298
  return {
303
299
  totalNativeCost: formatUnitsFixed(totalNativeCostWei, 18, 6),
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { estimateHoldersMakerCost } from './holders-maker.js';
3
+ describe('estimateHoldersMakerCost', () => {
4
+ it('USDT 模式每个 holder 只转授权 + 买入两笔 gasLimit × gasPrice', () => {
5
+ const r = estimateHoldersMakerCost({
6
+ holdersCount: 10,
7
+ buyAmountPerHolder: '1',
8
+ baseToken: 'usdt',
9
+ gasLimit: 800_000,
10
+ gasPriceGwei: 3,
11
+ });
12
+ // (150000 + 800000) × 3 gwei × 10 = 0.0285 BNB
13
+ expect(r.totalNativeCost).toBe('0.028500');
14
+ expect(r.totalERC20Cost).toBe('10.000000');
15
+ });
16
+ it('原生模式仍是买入金额 + 买入 gas 缓冲', () => {
17
+ const r = estimateHoldersMakerCost({
18
+ holdersCount: 1,
19
+ buyAmountPerHolder: '0.01',
20
+ baseToken: 'native',
21
+ gasLimit: 800_000,
22
+ gasPriceGwei: 3,
23
+ });
24
+ // 0.01 + 800000 × 3 gwei × 2
25
+ expect(r.totalNativeCost).toBe('0.014800');
26
+ });
27
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "four-flap-meme-sdk",
3
- "version": "4.5.3",
3
+ "version": "4.5.4",
4
4
  "description": "SDK for Flap bonding curve and four.meme TokenManager",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",