four-flap-meme-sdk 1.6.96 → 1.6.97

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.
@@ -11,3 +11,4 @@ export { pancakeProxyBatchBuyMerkle, pancakeProxyBatchSellMerkle, approvePancake
11
11
  export { flapDisperseWithBundleMerkle, flapSweepWithBundleMerkle, type FlapDisperseSignParams, type FlapDisperseMerkleResult, type FlapSweepSignParams, type FlapSweepMerkleResult } from './utils.js';
12
12
  export { flapBundleCurveToDex, type CurveToDexChain, type DexPoolType, type CurveToDexSignConfig, type CurveBuyerConfig, type DexBuyerConfig, type FlapCurveToDexParams, type FlapCurveToDexResult } from './curve-to-dex.js';
13
13
  export { flapBundleCreateToDex, type CreateToDexChain, type CreateToDexSignConfig, type CreateTokenInfo, type FlapCreateToDexParams, type FlapCreateToDexResult, type CurveBuyerConfig as CreateCurveBuyerConfig, type DexBuyerConfig as CreateDexBuyerConfig, type DexPoolType as CreateDexPoolType } from './create-to-dex.js';
14
+ export { flapBundleSwapMerkle, flapBatchSwapMerkle, flapQuickBatchSwapMerkle, flapCrossSwapMerkle, type FlapSwapSignConfig, type FlapSwapConfig, type FlapBundleSwapSignParams, type FlapBundleSwapParams, type FlapSwapResult, type FlapBatchSwapSignParams, type FlapBatchSwapResult, type FlapQuickBatchSwapSignParams, type FlapQuickBatchSwapResult, type FlapCrossSwapParams, type FlapCrossSwapResult, type UserType } from './swap.js';
@@ -19,3 +19,5 @@ export { flapDisperseWithBundleMerkle, flapSweepWithBundleMerkle } from './utils
19
19
  export { flapBundleCurveToDex } from './curve-to-dex.js';
20
20
  // ✅ 发币 + 一键买到外盘(Create → Curve → DEX)- 新代币
21
21
  export { flapBundleCreateToDex } from './create-to-dex.js';
22
+ // ✅ 内盘换手方法(一卖一买、一卖多买、快捷资金利用率、交叉换手)
23
+ export { flapBundleSwapMerkle, flapBatchSwapMerkle, flapQuickBatchSwapMerkle, flapCrossSwapMerkle } from './swap.js';
@@ -144,6 +144,38 @@ export interface FlapQuickBatchSwapResult {
144
144
  disperseHopCount?: number;
145
145
  };
146
146
  }
147
+ /** 交叉换手参数 */
148
+ export interface FlapCrossSwapParams {
149
+ chain: FlapChain;
150
+ /** 卖方私钥列表(按顺序轮流卖出) */
151
+ sellerPrivateKeys: string[];
152
+ /** 每个卖方的卖出数量(与 sellerPrivateKeys 长度一致) */
153
+ sellAmounts: string[];
154
+ /** 买方私钥列表(按顺序轮流分配,每笔平分) */
155
+ buyerPrivateKeys: string[];
156
+ tokenAddress: string;
157
+ config: FlapSwapSignConfig;
158
+ quoteToken?: string;
159
+ quoteTokenDecimals?: number;
160
+ /** 每次卖出分配给多少个买方(默认使用全部买方平分) */
161
+ buyersPerSell?: number;
162
+ /** 转账多跳数(可选) */
163
+ disperseHopCount?: number;
164
+ }
165
+ /** 交叉换手结果 */
166
+ export interface FlapCrossSwapResult {
167
+ signedTransactions: string[];
168
+ /** 每轮的元数据列表 */
169
+ rounds: Array<{
170
+ sellerAddress: string;
171
+ buyerAddresses: string[];
172
+ sellAmount: string;
173
+ bundleHash?: string;
174
+ }>;
175
+ /** 汇总的中间钱包信息(转账/利润) */
176
+ disperseHopWallets?: GeneratedWallet[];
177
+ profitHopWallets?: GeneratedWallet[];
178
+ }
147
179
  /**
148
180
  * Flap 内盘快捷批量换手(资金利用率模式)
149
181
  *
@@ -159,3 +191,11 @@ export interface FlapQuickBatchSwapResult {
159
191
  * 限制:根据多跳数动态计算最大买方数量
160
192
  */
161
193
  export declare function flapQuickBatchSwapMerkle(params: FlapQuickBatchSwapSignParams): Promise<FlapQuickBatchSwapResult>;
194
+ /**
195
+ * Flap 交叉换手(多卖多买,循环执行)
196
+ * - 每个卖方单独卖出,买入金额由卖出所得等分分配给当轮买方
197
+ * - 利润刮取、转账多跳逻辑继承 flapQuickBatchSwapMerkle
198
+ * - 结果按顺序拼接 signedTransactions(可交由前端/后端顺序广播)
199
+ * - ✅ 正确处理 nonce:同一钱包在多轮中使用时 nonce 自动递增
200
+ */
201
+ export declare function flapCrossSwapMerkle(params: FlapCrossSwapParams): Promise<FlapCrossSwapResult>;
@@ -1222,3 +1222,136 @@ export async function flapQuickBatchSwapMerkle(params) {
1222
1222
  }
1223
1223
  };
1224
1224
  }
1225
+ /**
1226
+ * Flap 交叉换手(多卖多买,循环执行)
1227
+ * - 每个卖方单独卖出,买入金额由卖出所得等分分配给当轮买方
1228
+ * - 利润刮取、转账多跳逻辑继承 flapQuickBatchSwapMerkle
1229
+ * - 结果按顺序拼接 signedTransactions(可交由前端/后端顺序广播)
1230
+ * - ✅ 正确处理 nonce:同一钱包在多轮中使用时 nonce 自动递增
1231
+ */
1232
+ export async function flapCrossSwapMerkle(params) {
1233
+ const { chain, sellerPrivateKeys, sellAmounts, buyerPrivateKeys, tokenAddress, config, quoteToken, quoteTokenDecimals = 18, buyersPerSell, disperseHopCount = 0 } = params;
1234
+ if (sellerPrivateKeys.length === 0) {
1235
+ throw new Error('至少需要一个卖方');
1236
+ }
1237
+ if (sellerPrivateKeys.length !== sellAmounts.length) {
1238
+ throw new Error(`sellAmounts 长度 (${sellAmounts.length}) 必须与卖方数量 (${sellerPrivateKeys.length}) 一致`);
1239
+ }
1240
+ if (buyerPrivateKeys.length === 0) {
1241
+ throw new Error('至少需要一个买方');
1242
+ }
1243
+ // ✅ 创建 Provider 和 NonceManager
1244
+ const chainContext = createChainContext(chain, config);
1245
+ const nonceManager = new NonceManager(chainContext.provider);
1246
+ // ✅ 预先获取所有钱包的初始 nonce
1247
+ const allSellerWallets = sellerPrivateKeys.map(pk => new Wallet(pk, chainContext.provider));
1248
+ const allBuyerWallets = buyerPrivateKeys.map(pk => new Wallet(pk, chainContext.provider));
1249
+ // 使用 Map 去重(同一私钥可能出现多次)
1250
+ const addressToNonce = new Map();
1251
+ // 获取所有卖方 nonce
1252
+ for (const wallet of allSellerWallets) {
1253
+ if (!addressToNonce.has(wallet.address)) {
1254
+ const nonce = await nonceManager.getNextNonce(wallet);
1255
+ addressToNonce.set(wallet.address, nonce);
1256
+ }
1257
+ }
1258
+ // 获取所有买方 nonce
1259
+ for (const wallet of allBuyerWallets) {
1260
+ if (!addressToNonce.has(wallet.address)) {
1261
+ const nonce = await nonceManager.getNextNonce(wallet);
1262
+ addressToNonce.set(wallet.address, nonce);
1263
+ }
1264
+ }
1265
+ console.log(`[flapCrossSwapMerkle] 初始化完成: ${sellerPrivateKeys.length} 卖方, ${buyerPrivateKeys.length} 买方`);
1266
+ const allSigned = [];
1267
+ const allDisperse = [];
1268
+ const allProfit = [];
1269
+ const rounds = [];
1270
+ // 轮流分配买方:每轮取 buyersPerSell 个(默认所有买方),不足则从头继续
1271
+ let buyerCursor = 0;
1272
+ const buyerBatchSize = Math.max(1, buyersPerSell ?? buyerPrivateKeys.length);
1273
+ for (let i = 0; i < sellerPrivateKeys.length; i++) {
1274
+ const sellerPk = sellerPrivateKeys[i];
1275
+ const sellAmount = sellAmounts[i];
1276
+ const sellerWallet = allSellerWallets[i];
1277
+ // 选取本轮买方私钥和钱包
1278
+ const roundBuyerPks = [];
1279
+ const roundBuyerWallets = [];
1280
+ for (let k = 0; k < buyerBatchSize; k++) {
1281
+ const idx = buyerCursor % buyerPrivateKeys.length;
1282
+ roundBuyerPks.push(buyerPrivateKeys[idx]);
1283
+ roundBuyerWallets.push(allBuyerWallets[idx]);
1284
+ buyerCursor++;
1285
+ }
1286
+ // ✅ 获取卖方当前 nonce
1287
+ const sellerNonce = addressToNonce.get(sellerWallet.address);
1288
+ // ✅ 获取并更新买方 nonces(每个买方本轮需要 1 个 nonce)
1289
+ const roundBuyerNonces = roundBuyerWallets.map(w => {
1290
+ const nonce = addressToNonce.get(w.address);
1291
+ addressToNonce.set(w.address, nonce + 1); // 每个买方用 1 个 nonce
1292
+ return nonce;
1293
+ });
1294
+ // 平分买入金额(使用比例模式,避免固定金额超出可分配金额)
1295
+ const ratio = 1 / roundBuyerPks.length;
1296
+ const buyerRatios = roundBuyerPks.map(() => ratio);
1297
+ // ✅ 构建 startNonces: [sellerNonce, buyer1Nonce, buyer2Nonce, ...]
1298
+ const startNonces = [sellerNonce, ...roundBuyerNonces];
1299
+ // ✅ 预先计算卖方 nonce 消耗(精确计算)
1300
+ // 卖方 nonce 消耗 = 贿赂(0/1) + 卖出(1) + 转账(N 或 N*2) + 利润(1)
1301
+ const useNativeToken = !quoteToken || quoteToken === ZERO_ADDRESS;
1302
+ const hasBribe = getBribeAmount(config) > 0n;
1303
+ const hasProfit = true; // 默认有利润刮取
1304
+ let sellerNonceConsumed = 0;
1305
+ if (hasBribe)
1306
+ sellerNonceConsumed += 1; // 贿赂
1307
+ sellerNonceConsumed += 1; // 卖出
1308
+ if (disperseHopCount === 0) {
1309
+ sellerNonceConsumed += roundBuyerPks.length; // 直接转账
1310
+ }
1311
+ else if (useNativeToken) {
1312
+ sellerNonceConsumed += roundBuyerPks.length; // 原生多跳:seller→hop1
1313
+ }
1314
+ else {
1315
+ sellerNonceConsumed += roundBuyerPks.length * 2; // ERC20 多跳:BNB + ERC20 两批
1316
+ }
1317
+ if (hasProfit)
1318
+ sellerNonceConsumed += 1; // 利润
1319
+ // 调用单卖多买的签名函数
1320
+ const res = await flapQuickBatchSwapMerkle({
1321
+ chain,
1322
+ sellerPrivateKey: sellerPk,
1323
+ sellAmount,
1324
+ buyerPrivateKeys: roundBuyerPks,
1325
+ buyerRatios,
1326
+ tokenAddress,
1327
+ config,
1328
+ quoteToken,
1329
+ quoteTokenDecimals,
1330
+ disperseHopCount,
1331
+ startNonces // ✅ 传入预计算的 nonces
1332
+ });
1333
+ // ✅ 更新卖方 nonce(使用精确计算的值)
1334
+ addressToNonce.set(sellerWallet.address, sellerNonce + sellerNonceConsumed);
1335
+ console.log(`[flapCrossSwapMerkle] 轮次 ${i + 1}: 卖方=${sellerWallet.address.slice(0, 10)}..., 买方=${roundBuyerPks.length}, 交易=${res.signedTransactions.length}`);
1336
+ // 累积签名与中间钱包
1337
+ allSigned.push(...res.signedTransactions);
1338
+ if (res.disperseHopWallets)
1339
+ allDisperse.push(...res.disperseHopWallets);
1340
+ if (res.profitHopWallets)
1341
+ allProfit.push(...res.profitHopWallets);
1342
+ rounds.push({
1343
+ sellerAddress: res.metadata?.sellerAddress || '',
1344
+ buyerAddresses: res.metadata?.buyerAddresses || roundBuyerPks.map(() => ''),
1345
+ sellAmount,
1346
+ bundleHash: undefined
1347
+ });
1348
+ }
1349
+ nonceManager.clearTemp();
1350
+ console.log(`[flapCrossSwapMerkle] 完成: ${rounds.length} 轮, ${allSigned.length} 笔交易`);
1351
+ return {
1352
+ signedTransactions: allSigned,
1353
+ rounds,
1354
+ disperseHopWallets: allDisperse.length > 0 ? allDisperse : undefined,
1355
+ profitHopWallets: allProfit.length > 0 ? allProfit : undefined
1356
+ };
1357
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "four-flap-meme-sdk",
3
- "version": "1.6.96",
3
+ "version": "1.6.97",
4
4
  "description": "SDK for Flap bonding curve and four.meme TokenManager",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",