four-flap-meme-sdk 2.2.18 → 2.2.19

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.
@@ -15,6 +15,7 @@ const V3_PAIR_ABI = [
15
15
  'function slot0() view returns (uint160 sqrtPriceX96,int24 tick,uint16 observationIndex,uint16 observationCardinality,uint16 observationCardinalityNext,uint8 feeProtocol,bool unlocked)',
16
16
  'function token0() view returns (address)',
17
17
  'function token1() view returns (address)',
18
+ 'function getReserves() view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast)',
18
19
  ];
19
20
  /** BSC:lpFeeProfile → Infinity PoolKey 的 fee / parameters(与链上 Initialize 一致) */
20
21
  const BSC_INFINITY_POOL_KEY_BY_PROFILE = {
@@ -133,6 +134,15 @@ function getCachedRpcProvider(rpcUrl) {
133
134
  }
134
135
  return p;
135
136
  }
137
+ async function getTokenDecimalsSafe(provider, address) {
138
+ try {
139
+ const token = new Contract(address, ERC20_ABI, provider);
140
+ return Number(await token.decimals());
141
+ }
142
+ catch {
143
+ return 18;
144
+ }
145
+ }
136
146
  /**
137
147
  * Infinity CL 池子 WBNB:取多档卖出 quote 的最大值(贴近 GMGN「池内 WBNB」)
138
148
  * 说明:不是 Vault 总 WBNB(全协议),也不是 L*sqrtP 虚拟储备(会高估到 ~5.8)。
@@ -208,39 +218,65 @@ async function getGraduatedV3PairMetrics(params) {
208
218
  const quoteAddr = params.quoteTokenAddress.toLowerCase() === ZERO_ADDRESS
209
219
  ? params.wrappedNativeAddress
210
220
  : params.quoteTokenAddress;
211
- const [t0, t1, slot0] = await Promise.all([
221
+ const [t0, t1] = await Promise.all([
212
222
  pool.token0(),
213
223
  pool.token1(),
214
- pool.slot0(),
215
224
  ]);
216
- const [bal0, bal1] = await Promise.all([
217
- new Contract(t0, ERC20_ABI, provider).balanceOf(params.poolAddress),
218
- new Contract(t1, ERC20_ABI, provider).balanceOf(params.poolAddress),
225
+ let bal0;
226
+ let bal1;
227
+ try {
228
+ [bal0, bal1] = await Promise.all([
229
+ new Contract(t0, ERC20_ABI, provider).balanceOf(params.poolAddress),
230
+ new Contract(t1, ERC20_ABI, provider).balanceOf(params.poolAddress),
231
+ ]);
232
+ }
233
+ catch {
234
+ try {
235
+ const reserves = await pool.getReserves();
236
+ bal0 = BigInt(reserves.reserve0 ?? reserves[0]);
237
+ bal1 = BigInt(reserves.reserve1 ?? reserves[1]);
238
+ }
239
+ catch {
240
+ return null;
241
+ }
242
+ }
243
+ let sqrtPriceX96 = null;
244
+ try {
245
+ const slot0 = await pool.slot0();
246
+ sqrtPriceX96 = BigInt(Array.isArray(slot0) ? slot0[0] : slot0.sqrtPriceX96);
247
+ }
248
+ catch {
249
+ sqrtPriceX96 = null;
250
+ }
251
+ const [dec0, dec1] = await Promise.all([
252
+ getTokenDecimalsSafe(provider, t0),
253
+ getTokenDecimalsSafe(provider, t1),
219
254
  ]);
220
- const sqrtPriceX96 = BigInt(Array.isArray(slot0) ? slot0[0] : slot0.sqrtPriceX96);
221
- const spot = v3SpotPriceQuotePerToken({
222
- sqrtPriceX96,
223
- tokenAddress: params.tokenAddress,
224
- token0: t0,
225
- token1: t1,
226
- quoteTokenAddress: quoteAddr,
227
- });
255
+ const spot = sqrtPriceX96 && sqrtPriceX96 > 0n
256
+ ? v3SpotPriceQuotePerToken({
257
+ sqrtPriceX96,
258
+ tokenAddress: params.tokenAddress,
259
+ token0: t0,
260
+ token1: t1,
261
+ quoteTokenAddress: quoteAddr,
262
+ })
263
+ : null;
228
264
  const tokenIs0 = t0.toLowerCase() === tokenLower;
229
- const poolTokenAmount = formatEther(tokenIs0 ? bal0 : bal1);
230
- const poolBNBAmount = formatEther(tokenIs0 ? bal1 : bal0);
265
+ const poolTokenAmount = formatUnits(tokenIs0 ? bal0 : bal1, tokenIs0 ? dec0 : dec1);
266
+ const poolQuoteAmount = formatUnits(tokenIs0 ? bal1 : bal0, tokenIs0 ? dec1 : dec0);
231
267
  let price = spot;
232
268
  if (!price && parseFloat(poolTokenAmount) > 0) {
233
- price = String(parseFloat(poolBNBAmount) / parseFloat(poolTokenAmount));
269
+ price = String(parseFloat(poolQuoteAmount) / parseFloat(poolTokenAmount));
234
270
  }
235
271
  if (!price) {
236
272
  price = await quotePortalPrice(params.portal, params.tokenAddress, params.quoteTokenAddress, params.wrappedNativeAddress);
237
273
  }
238
- if (!price && poolTokenAmount === '0' && poolBNBAmount === '0')
274
+ if (!price && poolTokenAmount === '0' && poolQuoteAmount === '0')
239
275
  return null;
240
276
  return {
241
277
  price: price ?? '0',
242
278
  progress: '100',
243
- poolBNBAmount,
279
+ poolBNBAmount: poolQuoteAmount,
244
280
  poolTokenAmount,
245
281
  dexKind: 'PANCAKE_V3',
246
282
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "four-flap-meme-sdk",
3
- "version": "2.2.18",
3
+ "version": "2.2.19",
4
4
  "description": "SDK for Flap bonding curve and four.meme TokenManager",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",