four-flap-meme-sdk 1.3.26 → 1.3.28

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.
@@ -27,6 +27,7 @@ import { JsonRpcProvider, Transaction } from 'ethers';
27
27
  export class MerkleClient {
28
28
  constructor(config) {
29
29
  // ✅ 区块号缓存(减少 RPC 调用)
30
+ // 注意:TTL 设为 1 秒,确保区块号尽可能新鲜,减少 "Missed inclusion" 风险
30
31
  this.blockNumberCache = null;
31
32
  this.chainId = config.chainId;
32
33
  this.chainName = config.chainId === 56 ? 'bsc' : 'ethereum';
@@ -291,8 +292,8 @@ export class MerkleClient {
291
292
  // 获取起始 Nonce(使用 latest 避免 pending 状态问题)
292
293
  let nonce = options?.startNonce;
293
294
  if (nonce === undefined) {
294
- // 使用 latest 更可靠,在高并发场景下更安全
295
- nonce = await this.provider.getTransactionCount(wallet.address, 'latest');
295
+ // 使用 'pending' 包含 mempool 中待确认的交易,避免授权后 nonce 冲突
296
+ nonce = await this.provider.getTransactionCount(wallet.address, 'pending');
296
297
  }
297
298
  // 获取 Gas Price
298
299
  let gasPrice = options?.gasPrice;
@@ -383,7 +384,7 @@ export class MerkleClient {
383
384
  };
384
385
  }
385
386
  }
386
- MerkleClient.BLOCK_CACHE_TTL_MS = 2000; // 2秒缓存(BSC 约 3 秒出块)
387
+ MerkleClient.BLOCK_CACHE_TTL_MS = 1000; // 1秒缓存(BSC 约 3 秒出块,ETH 约 12 秒)
387
388
  /**
388
389
  * 创建 Merkle 客户端的便捷函数
389
390
  */
@@ -32,8 +32,9 @@ export class NonceManager {
32
32
  this.tempNonceCache.set(key, cachedNonce + 1);
33
33
  return cachedNonce;
34
34
  }
35
- // 首次查询:获取链上最新 nonce('latest' 不含 pending)
36
- const onchainNonce = await this.provider.getTransactionCount(address, 'latest');
35
+ // 使用 'pending' 获取 nonce,包含 mempool 中待确认的交易
36
+ // 这样可以避免授权交易刚确认后 RPC 延迟导致的 nonce 冲突
37
+ const onchainNonce = await this.provider.getTransactionCount(address, 'pending');
37
38
  // 缓存下一个值(仅在当前批次内有效)
38
39
  this.tempNonceCache.set(key, onchainNonce + 1);
39
40
  return onchainNonce;
@@ -52,7 +53,8 @@ export class NonceManager {
52
53
  startNonce = this.tempNonceCache.get(key);
53
54
  }
54
55
  else {
55
- startNonce = await this.provider.getTransactionCount(address, 'latest');
56
+ // 使用 'pending' 包含 mempool 中待确认的交易
57
+ startNonce = await this.provider.getTransactionCount(address, 'pending');
56
58
  }
57
59
  // 更新缓存
58
60
  this.tempNonceCache.set(key, startNonce + count);
@@ -99,9 +101,10 @@ export class NonceManager {
99
101
  let queryResults;
100
102
  try {
101
103
  // ✅ 使用 JSON-RPC 批量请求(单次网络往返)
104
+ // ✅ 使用 'pending' 包含 mempool 中待确认的交易,避免授权后 nonce 冲突
102
105
  const batchRequests = needQuery.map(({ address }, idx) => ({
103
106
  method: 'eth_getTransactionCount',
104
- params: [address, 'latest'],
107
+ params: [address, 'pending'],
105
108
  id: idx + 1,
106
109
  jsonrpc: '2.0'
107
110
  }));
@@ -117,7 +120,8 @@ export class NonceManager {
117
120
  }
118
121
  catch {
119
122
  // 如果批量请求失败,回退到 Promise.all
120
- const queryPromises = needQuery.map(({ address }) => this.provider.getTransactionCount(address, 'latest'));
123
+ // 使用 'pending' 包含 mempool 中待确认的交易
124
+ const queryPromises = needQuery.map(({ address }) => this.provider.getTransactionCount(address, 'pending'));
121
125
  queryResults = await Promise.all(queryPromises);
122
126
  }
123
127
  // 填充结果并更新缓存
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "four-flap-meme-sdk",
3
- "version": "1.3.26",
3
+ "version": "1.3.28",
4
4
  "description": "SDK for Flap bonding curve and four.meme TokenManager",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",