@continuumdao/ctm-mpc-defi 0.2.26 → 0.2.27

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.
@@ -2890,6 +2890,28 @@ async function fetchUniswapV4PoolMeta(args) {
2890
2890
  );
2891
2891
  return data.pool ?? null;
2892
2892
  }
2893
+ var LATEST_SWAP_GQL = `
2894
+ query UniswapV4LatestSwap($pool: String!) {
2895
+ swaps(
2896
+ where: { pool: $pool }
2897
+ orderBy: timestamp
2898
+ orderDirection: desc
2899
+ first: 1
2900
+ ) {
2901
+ token0Price
2902
+ sqrtPriceX96
2903
+ }
2904
+ }`;
2905
+ async function fetchUniswapV4LatestSwap(args) {
2906
+ const pool = normalizePoolId(args.poolReference);
2907
+ const data = await postUniswapV4SubgraphGql(
2908
+ args.chainId,
2909
+ LATEST_SWAP_GQL,
2910
+ { pool },
2911
+ args.theGraphApiKey
2912
+ );
2913
+ return data.swaps?.[0] ?? null;
2914
+ }
2893
2915
 
2894
2916
  // src/protocols/evm/uniswap-v4/bitqueryApiKey.ts
2895
2917
  var BITQUERY_API_KEY_ENV = "BITQUERY_API_KEY";
@@ -3178,7 +3200,7 @@ function parseDecimal(raw) {
3178
3200
  const n = Number(raw);
3179
3201
  return Number.isFinite(n) && n > 0 ? n : null;
3180
3202
  }
3181
- function priceFromSwapRow(row) {
3203
+ function priceFromUniswapV4SwapRow(row) {
3182
3204
  const fromToken0 = parseDecimal(row.token0Price);
3183
3205
  if (fromToken0 != null) return fromToken0;
3184
3206
  const sqrt = row.sqrtPriceX96?.trim();
@@ -3200,7 +3222,7 @@ function bucketUniswapV4SwapsIntoCandles(args) {
3200
3222
  const buckets = /* @__PURE__ */ new Map();
3201
3223
  for (const swap of args.swaps) {
3202
3224
  const ts = Number(swap.timestamp);
3203
- const price = priceFromSwapRow(swap);
3225
+ const price = priceFromUniswapV4SwapRow(swap);
3204
3226
  if (!Number.isFinite(ts) || price == null) continue;
3205
3227
  const bucket = bucketStartSec(Math.floor(ts), args.bucketSec);
3206
3228
  const vol = parseDecimal(swap.amountUSD) ?? 0;
@@ -3541,6 +3563,74 @@ async function uniswapV4FetchOhlcv(args) {
3541
3563
  };
3542
3564
  }
3543
3565
 
3566
+ // src/protocols/evm/uniswap-v4/livePrice.ts
3567
+ function applyPriceQuote(price, quote) {
3568
+ if (!Number.isFinite(price) || price <= 0) return null;
3569
+ if (quote === "token1PerToken0") {
3570
+ const inverted = 1 / price;
3571
+ return Number.isFinite(inverted) && inverted > 0 ? inverted : null;
3572
+ }
3573
+ return price;
3574
+ }
3575
+ function bitqueryFilterFromPoolReference(poolReference) {
3576
+ const ref = poolReference.trim();
3577
+ if (!ref) return null;
3578
+ if (ref.startsWith("symbol:")) {
3579
+ return { kind: "symbol", symbol: ref.slice("symbol:".length).trim() };
3580
+ }
3581
+ if (ref.startsWith("0x")) {
3582
+ return { kind: "address", address: ref };
3583
+ }
3584
+ return null;
3585
+ }
3586
+ async function fetchUniswapV4BitqueryLatestClose(args) {
3587
+ const filter = bitqueryFilterFromPoolReference(args.poolReference);
3588
+ if (!filter) return null;
3589
+ const apiKey = resolveBitqueryApiKey(args.bitqueryApiKey);
3590
+ if (!apiKey) return null;
3591
+ const interval = args.interval?.trim() || "15m";
3592
+ const endTimeMs = Date.now();
3593
+ const startTimeMs = endTimeMs - 6 * 3600 * 1e3;
3594
+ const { candles } = await fetchRobinhoodBitqueryOhlcv({
3595
+ filter,
3596
+ interval,
3597
+ startTimeMs,
3598
+ endTimeMs,
3599
+ apiKey,
3600
+ limit: 5
3601
+ });
3602
+ if (candles.length === 0) return null;
3603
+ const last = candles[candles.length - 1];
3604
+ const close = Number(last.close);
3605
+ return Number.isFinite(close) && close > 0 ? close : null;
3606
+ }
3607
+ async function fetchUniswapV4ChartLivePrice(args) {
3608
+ const chainId = Math.floor(Number(args.chainId));
3609
+ if (!Number.isFinite(chainId) || chainId <= 0) return null;
3610
+ const poolReference = args.poolReference.trim();
3611
+ if (!poolReference) return null;
3612
+ const dataSource = String(args.dataSource ?? "").trim().toLowerCase();
3613
+ if (dataSource === "bitquery" || isRobinhoodOhlcvChain(chainId)) {
3614
+ return fetchUniswapV4BitqueryLatestClose({
3615
+ poolReference,
3616
+ interval: args.interval,
3617
+ bitqueryApiKey: args.bitqueryApiKey
3618
+ });
3619
+ }
3620
+ if (!uniswapV4SubgraphSupportedChainIds().includes(chainId)) {
3621
+ return null;
3622
+ }
3623
+ const row = await fetchUniswapV4LatestSwap({
3624
+ chainId,
3625
+ poolReference,
3626
+ theGraphApiKey: resolveTheGraphApiKey(args.theGraphApiKey)
3627
+ });
3628
+ if (!row) return null;
3629
+ const raw = priceFromUniswapV4SwapRow(row);
3630
+ if (raw == null) return null;
3631
+ return applyPriceQuote(raw, args.priceQuote);
3632
+ }
3633
+
3544
3634
  // src/protocols/evm/uniswap-v4/limitOrder.ts
3545
3635
  function trimAddr3(a) {
3546
3636
  return a.trim();
@@ -4099,6 +4189,8 @@ exports.extractUniswapLpTokenAmounts = extractUniswapLpTokenAmounts;
4099
4189
  exports.extractUniswapLpTransaction = extractUniswapLpTransaction;
4100
4190
  exports.fetchEthereumAddressForKeyGen = fetchEthereumAddressForKeyGen;
4101
4191
  exports.fetchRobinhoodBitqueryOhlcv = fetchRobinhoodBitqueryOhlcv;
4192
+ exports.fetchUniswapV4ChartLivePrice = fetchUniswapV4ChartLivePrice;
4193
+ exports.fetchUniswapV4LatestSwap = fetchUniswapV4LatestSwap;
4102
4194
  exports.fetchUniswapV4PoolDayDatas = fetchUniswapV4PoolDayDatas;
4103
4195
  exports.fetchUniswapV4PoolHourDatas = fetchUniswapV4PoolHourDatas;
4104
4196
  exports.fetchUniswapV4PoolMeta = fetchUniswapV4PoolMeta;