Skip to content

Commit 7561b34

Browse files
committed
fix 30d volume
1 parent 406a53f commit 7561b34

1 file changed

Lines changed: 18 additions & 109 deletions

File tree

app/routes/hydration-web/v1/stats.mjs

Lines changed: 18 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const SPOT_PRICE_ENDPOINT =
55
const UNIFIED_GRAPHQL_ENDPOINT =
66
"https://galacticcouncil.squids.live/hydration-pools:unified-prod/api/graphql";
77
const XCM_API_ENDPOINT = "https://dev-api.ocelloids.net/query/xcm";
8-
const DECIMALS_ENDPOINT = "https://hydration.dipdup.net/api/rest/asset?id=";
98

109
export default async (fastify, opts) => {
1110
fastify.route({
@@ -38,6 +37,7 @@ export default async (fastify, opts) => {
3837
stableAssetsData,
3938
accountsData,
4039
tvlData,
40+
volumeData,
4141
] = await Promise.all([
4242
gqlRequest(
4343
UNIFIED_GRAPHQL_ENDPOINT,
@@ -99,6 +99,18 @@ export default async (fastify, opts) => {
9999
}
100100
`
101101
),
102+
gqlRequest(
103+
SPOT_PRICE_ENDPOINT,
104+
gql`
105+
{
106+
platformTotalVolumesByPeriod(filter: { period: _30D_ }) {
107+
nodes {
108+
totalVolNorm
109+
}
110+
}
111+
}
112+
`
113+
),
102114
]);
103115

104116
const omnipoolIds = new Set(
@@ -121,113 +133,10 @@ export default async (fastify, opts) => {
121133
const assetsCount = allTradableAssets.size;
122134
const accountsCount = accountsData.accounts.totalCount;
123135

124-
const [omnipoolVolumeData, stableVolumeData, priceData] =
125-
await Promise.all([
126-
gqlRequest(
127-
UNIFIED_GRAPHQL_ENDPOINT,
128-
gql`
129-
query ($assetIds: [String!]) {
130-
omnipoolAssetHistoricalVolumesByPeriod(
131-
filter: { period: _1M_, assetIds: $assetIds }
132-
) {
133-
nodes {
134-
assetId
135-
assetVolume
136-
}
137-
}
138-
}
139-
`,
140-
{ assetIds: [...omnipoolIds] }
141-
),
142-
gqlRequest(
143-
UNIFIED_GRAPHQL_ENDPOINT,
144-
gql`
145-
query {
146-
stableswapHistoricalVolumesByPeriod(
147-
filter: { period: _1M_, poolIds: ["690", "102"] }
148-
) {
149-
nodes {
150-
poolId
151-
assetVolumes {
152-
assetRegistryId
153-
swapVolume
154-
}
155-
}
156-
}
157-
}
158-
`
159-
),
160-
gqlRequest(
161-
SPOT_PRICE_ENDPOINT,
162-
gql`
163-
query {
164-
assetHistoricalData(
165-
first: 1000
166-
orderBy: PARA_BLOCK_HEIGHT_DESC
167-
) {
168-
nodes {
169-
asset {
170-
assetRegistryId
171-
}
172-
assetSpotPriceHistoricalDataByAssetInHistDataId {
173-
nodes {
174-
priceNormalised
175-
}
176-
}
177-
}
178-
}
179-
}
180-
`
181-
),
182-
]);
183-
184-
const priceMap = new Map();
185-
for (const node of priceData.assetHistoricalData.nodes) {
186-
const id = node.asset?.assetRegistryId;
187-
const price =
188-
node.assetSpotPriceHistoricalDataByAssetInHistDataId.nodes[0]
189-
?.priceNormalised;
190-
if (id && price && !priceMap.has(id)) {
191-
priceMap.set(id, parseFloat(price));
192-
}
193-
}
194-
195-
const volumeAccumulator = new Map();
196-
for (const entry of omnipoolVolumeData
197-
.omnipoolAssetHistoricalVolumesByPeriod.nodes) {
198-
volumeAccumulator.set(
199-
entry.assetId,
200-
(volumeAccumulator.get(entry.assetId) || 0n) +
201-
BigInt(entry.assetVolume)
202-
);
203-
}
204-
for (const node of stableVolumeData.stableswapHistoricalVolumesByPeriod
205-
.nodes) {
206-
for (const asset of node.assetVolumes) {
207-
volumeAccumulator.set(
208-
asset.assetRegistryId,
209-
(volumeAccumulator.get(asset.assetRegistryId) || 0n) +
210-
BigInt(asset.swapVolume)
211-
);
212-
}
213-
}
214-
215-
let vol30d = 0;
216-
for (const [assetId, rawVolume] of volumeAccumulator.entries()) {
217-
try {
218-
const decimalRes = await fetch(`${DECIMALS_ENDPOINT}${assetId}`);
219-
const decimalJson = await decimalRes.json();
220-
const decimals = decimalJson.asset?.decimals;
221-
const price = priceMap.get(assetId);
222-
if (decimals == null || price == null) continue;
223-
const normalizedVolume = Number(rawVolume) / 10 ** decimals;
224-
vol30d += normalizedVolume * price;
225-
} catch (e) {
226-
request.log.warn(
227-
`Volume normalization failed for asset ${assetId}: ${e.message}`
228-
);
229-
}
230-
}
136+
// Get 30-day volume from the new GraphQL query
137+
const vol30d = Number(
138+
volumeData.platformTotalVolumesByPeriod.nodes[0]?.totalVolNorm || 0
139+
);
231140

232141
let xcmVol30d = 0;
233142
try {
@@ -260,7 +169,7 @@ export default async (fastify, opts) => {
260169

261170
const result = {
262171
tvl: Number(tvl),
263-
vol_30d: Number(vol30d.toFixed(12)),
172+
vol_30d: vol30d,
264173
xcm_vol_30d: Number(xcmVol30d.toFixed(12)),
265174
assets_count: assetsCount,
266175
accounts_count: accountsCount,

0 commit comments

Comments
 (0)