four-flap-meme-sdk 1.2.96 → 1.2.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.
@@ -37,9 +37,27 @@ const I_UNIV3_FACTORY_ABI = ['function getPool(address,address,uint24) view retu
37
37
  const I_ROUTER_WITH_FACTORY_ABI = ['function factory() view returns (address)'];
38
38
  // 数字转字符串(避免 BigInt JSON 兼容问题)
39
39
  function toStr(v) { return typeof v === 'bigint' ? v.toString() : String(v); }
40
- // 格式化余额:将 Wei 转换为 Ether 单位(18 decimals)
41
- function formatBalance(balance, format) {
42
- return format ? formatUnits(balance, 18) : balance.toString();
40
+ // 格式化余额:将 Wei 转换为指定精度的单位
41
+ function formatBalance(balance, format, decimals = 18) {
42
+ return format ? formatUnits(balance, decimals) : balance.toString();
43
+ }
44
+ // ✅ 动态查询代币精度(带缓存)
45
+ const decimalsCache = new Map();
46
+ async function getTokenDecimals(tokenAddress, provider) {
47
+ const key = tokenAddress.toLowerCase();
48
+ if (decimalsCache.has(key)) {
49
+ return decimalsCache.get(key);
50
+ }
51
+ try {
52
+ const contract = new Contract(tokenAddress, I_ERC20_ABI, provider);
53
+ const decimals = await contract.decimals();
54
+ const result = Number(decimals);
55
+ decimalsCache.set(key, result);
56
+ return result;
57
+ }
58
+ catch {
59
+ return 18; // 默认 18 位精度
60
+ }
43
61
  }
44
62
  export async function inspectTokenLP(token, opts) {
45
63
  const provider = new JsonRpcProvider(opts.rpcUrl);
@@ -189,7 +207,9 @@ export async function inspectTokenLP(token, opts) {
189
207
  const [r0, r1] = await pair.getReserves();
190
208
  const reserveToken = t0.toLowerCase() === token.toLowerCase() ? r0 : r1;
191
209
  const reserveWBNB = t0.toLowerCase() === chainTokens.wrappedNative.toLowerCase() ? r0 : r1;
192
- v2.wbnbPair = { address: v2PairWbnb, reserveToken: formatBalance(reserveToken, shouldFormat), reserveWBNB: formatBalance(reserveWBNB, shouldFormat) };
210
+ // 动态查询 WBNB/WMON 精度
211
+ const wbnbDecimals = await getTokenDecimals(chainTokens.wrappedNative, provider);
212
+ v2.wbnbPair = { address: v2PairWbnb, reserveToken: formatBalance(reserveToken, shouldFormat, tokenDecimals ?? 18), reserveWBNB: formatBalance(reserveWBNB, shouldFormat, wbnbDecimals) };
193
213
  }
194
214
  catch {
195
215
  v2.wbnbPair = { address: v2PairWbnb, reserveToken: '0', reserveWBNB: '0' };
@@ -207,7 +227,9 @@ export async function inspectTokenLP(token, opts) {
207
227
  const [r0, r1] = await pair.getReserves();
208
228
  const reserveToken = t0.toLowerCase() === token.toLowerCase() ? r0 : r1;
209
229
  const reserveUSDT = t0.toLowerCase() === chainTokens.stableCoin.toLowerCase() ? r0 : r1;
210
- v2.usdtPair = { address: v2PairUsdt, reserveToken: formatBalance(reserveToken, shouldFormat), reserveUSDT: formatBalance(reserveUSDT, shouldFormat) };
230
+ // 动态查询稳定币精度
231
+ const stableDecimals = await getTokenDecimals(chainTokens.stableCoin, provider);
232
+ v2.usdtPair = { address: v2PairUsdt, reserveToken: formatBalance(reserveToken, shouldFormat, tokenDecimals ?? 18), reserveUSDT: formatBalance(reserveUSDT, shouldFormat, stableDecimals) };
211
233
  }
212
234
  catch {
213
235
  v2.usdtPair = { address: v2PairUsdt, reserveToken: '0', reserveUSDT: '0' };
@@ -328,7 +350,9 @@ export async function inspectTokenLP(token, opts) {
328
350
  console.log(`[V3 Debug] ${p.base} fee=${p.fee}: base balance decode error`, err);
329
351
  }
330
352
  idx++;
331
- v3Results.push({ base: p.base, fee: p.fee, pool: p.pool, tokenBalance: formatBalance(tb, shouldFormat), baseBalance: formatBalance(bb, shouldFormat) });
353
+ // 动态查询基础代币精度
354
+ const baseDecimals = await getTokenDecimals(p.baseAddress, provider);
355
+ v3Results.push({ base: p.base, fee: p.fee, pool: p.pool, tokenBalance: formatBalance(tb, shouldFormat, tokenDecimals ?? 18), baseBalance: formatBalance(bb, shouldFormat, baseDecimals) });
332
356
  }
333
357
  if (v3Results.length > 0)
334
358
  out.v3 = v3Results;
@@ -370,7 +394,9 @@ export async function inspectTokenLP(token, opts) {
370
394
  bb = await baseContract.balanceOf(pool);
371
395
  }
372
396
  catch { }
373
- v3Results.push({ base: basePair.symbol, fee, pool, tokenBalance: formatBalance(tb, shouldFormat), baseBalance: formatBalance(bb, shouldFormat) });
397
+ // 动态查询基础代币精度
398
+ const baseDecimals = await getTokenDecimals(baseAddr, provider);
399
+ v3Results.push({ base: basePair.symbol, fee, pool, tokenBalance: formatBalance(tb, shouldFormat, tokenDecimals ?? 18), baseBalance: formatBalance(bb, shouldFormat, baseDecimals) });
374
400
  }
375
401
  }
376
402
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "four-flap-meme-sdk",
3
- "version": "1.2.96",
3
+ "version": "1.2.97",
4
4
  "description": "SDK for Flap bonding curve and four.meme TokenManager",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",