four-flap-meme-sdk 1.7.1 → 1.7.2

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.
@@ -79,6 +79,8 @@ export interface XLayerCrossSwapParams {
79
79
  quoteTokenDecimals?: number;
80
80
  buyersPerSell?: number;
81
81
  disperseHopCount?: number;
82
+ /** ✅ 预获取的 nonce(地址 -> nonce),避免 nonce 不同步 */
83
+ startNonces?: Map<string, number>;
82
84
  }
83
85
  export interface XLayerCrossSwapResult {
84
86
  signedTransactions: string[];
@@ -90,6 +90,17 @@ async function quoteSellOutput(provider, routeParams, sellAmountWei) {
90
90
  console.log(`[XLayer quoteSellOutput] V3 报价成功: ${ethers.formatEther(result.amountOut)} OKB`);
91
91
  return result.amountOut;
92
92
  }
93
+ // ❗️兜底:如果 V3 报价失败且提供了 v2Path,则尝试 V2 报价(避免因为无对应费率池导致失败)
94
+ if (params.v2Path && params.v2Path.length >= 2) {
95
+ console.warn('[XLayer quoteSellOutput] V3 报价失败,尝试 V2 兜底');
96
+ const tokenIn = params.v2Path[0];
97
+ const tokenOut = params.v2Path[params.v2Path.length - 1];
98
+ const v2Result = await quoteV2(provider, tokenIn, tokenOut, sellAmountWei, XLAYER_CHAIN_NAME);
99
+ if (v2Result.amountOut > 0n) {
100
+ console.log(`[XLayer quoteSellOutput] V2 兜底报价成功: ${ethers.formatEther(v2Result.amountOut)} OKB`);
101
+ return v2Result.amountOut;
102
+ }
103
+ }
93
104
  throw new Error(`V3 单跳报价失败: tokenIn=${params.v3TokenIn}, tokenOut=${params.v3TokenOut}, fee=${params.v3Fee}`);
94
105
  }
95
106
  if (routeParams.routeType === 'v3-multi') {
@@ -100,6 +111,17 @@ async function quoteSellOutput(provider, routeParams, sellAmountWei) {
100
111
  console.log(`[XLayer quoteSellOutput] V3 多跳报价成功: ${ethers.formatEther(result.amountOut)} OKB`);
101
112
  return result.amountOut;
102
113
  }
114
+ // ❗️兜底:如果 V3 多跳失败且提供 v2Path,则尝试 V2 报价
115
+ if (params.v2Path && params.v2Path.length >= 2) {
116
+ console.warn('[XLayer quoteSellOutput] V3 多跳报价失败,尝试 V2 兜底');
117
+ const tokenInV2 = params.v2Path[0];
118
+ const tokenOutV2 = params.v2Path[params.v2Path.length - 1];
119
+ const v2Result = await quoteV2(provider, tokenInV2, tokenOutV2, sellAmountWei, XLAYER_CHAIN_NAME);
120
+ if (v2Result.amountOut > 0n) {
121
+ console.log(`[XLayer quoteSellOutput] V2 多跳兜底成功: ${ethers.formatEther(v2Result.amountOut)} OKB`);
122
+ return v2Result.amountOut;
123
+ }
124
+ }
103
125
  throw new Error(`V3 多跳报价失败`);
104
126
  }
105
127
  throw new Error(`不支持的路由类型: ${routeParams.routeType}`);
@@ -461,7 +483,7 @@ export async function xlayerQuickBatchSwapMerkle(params) {
461
483
  }
462
484
  // ==================== XLayer 交叉换手 ====================
463
485
  export async function xlayerCrossSwapMerkle(params) {
464
- const { sellerPrivateKeys, sellAmounts, buyerPrivateKeys, tokenAddress, routeParams, config, quoteToken, quoteTokenDecimals = 18, buyersPerSell, disperseHopCount = 0 } = params;
486
+ const { sellerPrivateKeys, sellAmounts, buyerPrivateKeys, tokenAddress, routeParams, config, quoteToken, quoteTokenDecimals = 18, buyersPerSell, disperseHopCount = 0, startNonces: inputNonces } = params;
465
487
  if (sellerPrivateKeys.length === 0)
466
488
  throw new Error('至少需要一个卖方');
467
489
  if (sellerPrivateKeys.length !== sellAmounts.length)
@@ -472,16 +494,28 @@ export async function xlayerCrossSwapMerkle(params) {
472
494
  const nonceManager = new NonceManager(context.provider);
473
495
  const allSellerWallets = sellerPrivateKeys.map(pk => new Wallet(pk, context.provider));
474
496
  const allBuyerWallets = buyerPrivateKeys.map(pk => new Wallet(pk, context.provider));
475
- // 预先获取所有钱包的初始 nonce
497
+ // 优先使用前端传入的 nonce,避免 nonce 不同步
476
498
  const addressToNonce = new Map();
477
499
  for (const wallet of allSellerWallets) {
478
500
  if (!addressToNonce.has(wallet.address)) {
479
- addressToNonce.set(wallet.address, await nonceManager.getNextNonce(wallet));
501
+ const inputNonce = inputNonces?.get(wallet.address.toLowerCase()) ?? inputNonces?.get(wallet.address);
502
+ if (inputNonce !== undefined) {
503
+ addressToNonce.set(wallet.address, inputNonce);
504
+ }
505
+ else {
506
+ addressToNonce.set(wallet.address, await nonceManager.getNextNonce(wallet));
507
+ }
480
508
  }
481
509
  }
482
510
  for (const wallet of allBuyerWallets) {
483
511
  if (!addressToNonce.has(wallet.address)) {
484
- addressToNonce.set(wallet.address, await nonceManager.getNextNonce(wallet));
512
+ const inputNonce = inputNonces?.get(wallet.address.toLowerCase()) ?? inputNonces?.get(wallet.address);
513
+ if (inputNonce !== undefined) {
514
+ addressToNonce.set(wallet.address, inputNonce);
515
+ }
516
+ else {
517
+ addressToNonce.set(wallet.address, await nonceManager.getNextNonce(wallet));
518
+ }
485
519
  }
486
520
  }
487
521
  console.log(`[xlayerCrossSwapMerkle] 初始化: ${sellerPrivateKeys.length} 卖方, ${buyerPrivateKeys.length} 买方`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "four-flap-meme-sdk",
3
- "version": "1.7.1",
3
+ "version": "1.7.2",
4
4
  "description": "SDK for Flap bonding curve and four.meme TokenManager",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",