|
1 | 1 | import { gql, request as gqlRequest } from "graphql-request"; |
2 | | -import { ApiPromise, WsProvider } from "@polkadot/api"; |
3 | 2 |
|
4 | | -const DECIMALS_ENDPOINT = "https://hydration.dipdup.net/api/rest/asset?id="; |
5 | 3 | const SPOT_PRICE_ENDPOINT = |
6 | 4 | "https://galacticcouncil.squids.live/hydration-pools:whale-prod/api/graphql"; |
7 | 5 | const UNIFIED_GRAPHQL_ENDPOINT = |
8 | 6 | "https://galacticcouncil.squids.live/hydration-pools:unified-prod/api/graphql"; |
9 | | -const RPC_ENDPOINT = "wss://hydration-rpc.n.dwellir.com"; |
10 | 7 | const XCM_API_ENDPOINT = "https://dev-api.ocelloids.net/query/xcm"; |
| 8 | +const DECIMALS_ENDPOINT = "https://hydration.dipdup.net/api/rest/asset?id="; |
11 | 9 |
|
12 | 10 | export default async (fastify, opts) => { |
13 | 11 | fastify.route({ |
@@ -39,6 +37,7 @@ export default async (fastify, opts) => { |
39 | 37 | xykPoolsData, |
40 | 38 | stableAssetsData, |
41 | 39 | accountsData, |
| 40 | + tvlData, |
42 | 41 | ] = await Promise.all([ |
43 | 42 | gqlRequest( |
44 | 43 | UNIFIED_GRAPHQL_ENDPOINT, |
@@ -88,6 +87,18 @@ export default async (fastify, opts) => { |
88 | 87 | } |
89 | 88 | ` |
90 | 89 | ), |
| 90 | + gqlRequest( |
| 91 | + SPOT_PRICE_ENDPOINT, |
| 92 | + gql` |
| 93 | + { |
| 94 | + platformTotalTvl { |
| 95 | + nodes { |
| 96 | + totalTvlDecoratedNorm |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + ` |
| 101 | + ), |
91 | 102 | ]); |
92 | 103 |
|
93 | 104 | const omnipoolIds = new Set( |
@@ -243,38 +254,11 @@ export default async (fastify, opts) => { |
243 | 254 | request.log.warn("Failed to fetch xcm volume", e); |
244 | 255 | } |
245 | 256 |
|
246 | | - const provider = new WsProvider(RPC_ENDPOINT); |
247 | | - const api = await ApiPromise.create({ provider }); |
248 | | - |
249 | | - let tvl = 0; |
250 | | - for (const assetId of priceMap.keys()) { |
251 | | - try { |
252 | | - const decimalRes = await fetch(`${DECIMALS_ENDPOINT}${assetId}`); |
253 | | - const decimalJson = await decimalRes.json(); |
254 | | - const decimals = decimalJson.asset?.decimals; |
255 | | - if (decimals == null) continue; |
256 | | - |
257 | | - let issuance; |
258 | | - if (assetId === "0") { |
259 | | - issuance = await api.query.balances.totalIssuance(); |
260 | | - } else { |
261 | | - issuance = await api.query.tokens.totalIssuance(assetId); |
262 | | - } |
263 | | - |
264 | | - const issuanceFloat = Number(issuance.toBigInt()) / 10 ** decimals; |
265 | | - const tvlUsd = issuanceFloat * priceMap.get(assetId); |
266 | | - if (assetId !== "1") tvl += tvlUsd; |
267 | | - } catch (e) { |
268 | | - request.log.warn( |
269 | | - `TVL computation failed for asset ${assetId}: ${e.message}` |
270 | | - ); |
271 | | - } |
272 | | - } |
273 | | - |
274 | | - await api.disconnect(); |
| 257 | + // Get TVL from the new GraphQL query |
| 258 | + const tvl = tvlData.platformTotalTvl.nodes[0]?.totalTvlDecoratedNorm || 0; |
275 | 259 |
|
276 | 260 | const result = { |
277 | | - tvl: Number(tvl.toFixed(12)), |
| 261 | + tvl: Number(tvl), |
278 | 262 | vol_30d: Number(vol30d.toFixed(12)), |
279 | 263 | xcm_vol_30d: Number(xcmVol30d.toFixed(12)), |
280 | 264 | assets_count: assetsCount, |
|
0 commit comments