From 313343846e9305a74e2e17850b3759d5a5f187ed Mon Sep 17 00:00:00 2001 From: Krunal Amin Date: Wed, 1 May 2024 11:17:48 -0500 Subject: [PATCH] number fixes plus track mux op and ftm volume --- dexs/unidex/index.ts | 71 +++++++++++----------------- dexs/unidex/unidex-agg-perp/index.ts | 48 ++++++++++++------- 2 files changed, 60 insertions(+), 59 deletions(-) diff --git a/dexs/unidex/index.ts b/dexs/unidex/index.ts index 6849460ae8..270ecd6df0 100644 --- a/dexs/unidex/index.ts +++ b/dexs/unidex/index.ts @@ -3,13 +3,10 @@ import { CHAIN } from "../../helpers/chains"; import { getTimestampAtStartOfDayUTC } from "../../utils/date"; import { Chain } from "@defillama/sdk/build/general"; import request, { gql } from "graphql-request"; -import { adapteraggderivative } from './unidex-agg-perp/index' -import { adapter_dexs_agg } from './unidex-dexs-agg/index' - -type TChainIDs = { - [key in Chain]?: number; -}; +import { adapteraggderivative } from './unidex-agg-perp/index'; +import { adapter_dexs_agg } from './unidex-dexs-agg/index'; +type TChainIDs = { [key in Chain]?: number; }; const chainIDs: TChainIDs = { [CHAIN.FANTOM]: 250, [CHAIN.ARBITRUM]: 42161, @@ -22,36 +19,38 @@ const chainIDs: TChainIDs = { interface IDayProduct { cumulativeVolumeUsd: number; - _id: string; + chainId: number; } const fetch = (chain: Chain) => { return async (timestamp: number): Promise => { const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp); - const graphQuery = gql` query MyQuery { - DayProducts(limit: 0, filter: {date: ${todaysTimestamp}}) { + DayProducts(limit: 0, filter: { date: ${todaysTimestamp} }) { cumulativeVolumeUsd - _id + chainId } } `; - - const endpoint = "https://arkiverbackup.moltennetwork.com/graphql"; + const endpoint = "https://arkiver.moltennetwork.com/graphql"; const response = await request(endpoint, graphQuery); const dayProducts: IDayProduct[] = response.DayProducts; - const chainID = chainIDs[chain]; - let dailyVolumeUSD = 0; - + const volumeByChain: { [chainId: number]: number } = {}; dayProducts.forEach((product) => { - const productChainID = parseInt(product._id.split(":")[2]); - if (productChainID === chainID) { - dailyVolumeUSD += product.cumulativeVolumeUsd; + const chainId = product.chainId; + if (chainId === 360) { + // Combine volume for chainID 360 with chainID 42161 + volumeByChain[42161] = (volumeByChain[42161] || 0) + product.cumulativeVolumeUsd; + } else { + volumeByChain[chainId] = (volumeByChain[chainId] || 0) + product.cumulativeVolumeUsd; } }); + const chainID = chainIDs[chain]; + const dailyVolumeUSD = chainID !== undefined ? volumeByChain[chainID] || 0 : 0; + return { dailyVolume: dailyVolumeUSD.toString(), timestamp: todaysTimestamp, @@ -60,8 +59,7 @@ const fetch = (chain: Chain) => { }; const methodology = { - dailyVolume: - "Sum of cumulativeVolumeUsd for all products on the specified chain for the given day", + dailyVolume: "Sum of cumulativeVolumeUsd for all products on the specified chain for the given day", }; const adapter: any = { @@ -69,51 +67,37 @@ const adapter: any = { [CHAIN.OPTIMISM]: { fetch: fetch(CHAIN.OPTIMISM), start: 1687422746, - meta: { - methodology, - }, + meta: { methodology }, }, [CHAIN.ERA]: { fetch: fetch(CHAIN.ERA), start: 1687422746, - meta: { - methodology, - }, + meta: { methodology }, }, [CHAIN.ARBITRUM]: { fetch: fetch(CHAIN.ARBITRUM), start: 1687422746, - meta: { - methodology, - }, + meta: { methodology }, }, [CHAIN.BASE]: { fetch: fetch(CHAIN.BASE), start: 1687422746, - meta: { - methodology, - }, + meta: { methodology }, }, [CHAIN.FANTOM]: { fetch: fetch(CHAIN.FANTOM), start: 1687422746, - meta: { - methodology, - }, + meta: { methodology }, }, [CHAIN.METIS]: { fetch: fetch(CHAIN.METIS), start: 1687898060, - meta: { - methodology, - }, + meta: { methodology }, }, [CHAIN.EVMOS]: { fetch: fetch(CHAIN.EVMOS), start: 1700104066, - meta: { - methodology, - }, + meta: { methodology }, }, }, }; @@ -124,5 +108,6 @@ const adapterbreakdown: BreakdownAdapter = { "unidex-agg-derivative": adapteraggderivative["adapter"], "unidex-dexs-agg": adapter_dexs_agg["adapter"], } -} -export default adapterbreakdown; +}; + +export default adapterbreakdown; \ No newline at end of file diff --git a/dexs/unidex/unidex-agg-perp/index.ts b/dexs/unidex/unidex-agg-perp/index.ts index 581cc67b80..3fa5fbec14 100644 --- a/dexs/unidex/unidex-agg-perp/index.ts +++ b/dexs/unidex/unidex-agg-perp/index.ts @@ -25,7 +25,7 @@ const chainIDs: TChainIDs = { interface IDayProduct { cumulativeVolumeUsd: number; - _id: string; + chainId: number; } const fetchReferralVolume = async (timestamp: number): Promise => { @@ -53,7 +53,7 @@ const fetchReferralVolume = async (timestamp: number): Promise => { }; -const fetchMuxReferralVolume = async (timestamp: number): Promise => { +const fetchMuxReferralVolume = async (chain: Chain, timestamp: number): Promise => { const startOfDayTimestamp = getTimestampAtStartOfDayUTC(timestamp); const endOfDayTimestamp = startOfDayTimestamp + 86400; // Add one day's worth of seconds for the end of the day @@ -77,7 +77,22 @@ const fetchMuxReferralVolume = async (timestamp: number): Promise => { timestamp_lte: endOfDayTimestamp.toString() }; - const referralEndpoint = 'https://api.thegraph.com/subgraphs/name/mux-world/mux-referral-arb'; + let referralEndpoint = ''; + + switch (chain) { + case CHAIN.ARBITRUM: + referralEndpoint = 'https://api.thegraph.com/subgraphs/name/mux-world/mux-referral-arb'; + break; + case CHAIN.OPTIMISM: + referralEndpoint = 'https://api.thegraph.com/subgraphs/name/mux-world/mux-referral-op'; + break; + case CHAIN.FANTOM: + referralEndpoint = 'https://api.thegraph.com/subgraphs/name/mux-world/mux-referral-ftm'; + break; + default: + return 0; // Return 0 for unsupported chains + } + const referralRes = await request(referralEndpoint, referralQuery, variables); // Sum up the volumes @@ -98,9 +113,6 @@ const fetchMuxReferralVolume = async (timestamp: number): Promise => { - - - const fetch = (chain: Chain) => { return async (timestamp: number): Promise => { const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp); @@ -109,7 +121,7 @@ const fetch = (chain: Chain) => { query MyQuery { DayProducts(filter: {date: ${todaysTimestamp}}) { cumulativeVolumeUsd - _id + chainId } } `; @@ -118,19 +130,23 @@ const fetch = (chain: Chain) => { const response = await request(endpoint, graphQuery); const dayProducts: IDayProduct[] = response.DayProducts; - const chainID = chainIDs[chain]; - let dailyVolumeUSD = 0; - + const volumeByChain: { [chainId: number]: number } = {}; dayProducts.forEach((product) => { - const productChainID = parseInt(product._id.split(':')[2]); - if (productChainID === chainID) { - dailyVolumeUSD += product.cumulativeVolumeUsd; + const chainId = product.chainId; + if (chainId === 360) { + // Combine volume for chainID 360 with chainID 42161 + volumeByChain[42161] = (volumeByChain[42161] || 0) + product.cumulativeVolumeUsd; + } else { + volumeByChain[chainId] = (volumeByChain[chainId] || 0) + product.cumulativeVolumeUsd; } }); - if (chain === CHAIN.ARBITRUM) { + const chainID = chainIDs[chain]; + let dailyVolumeUSD = chainID !== undefined ? volumeByChain[chainID] || 0 : 0; + + if (chain === CHAIN.ARBITRUM || chain === CHAIN.OPTIMISM || chain === CHAIN.FANTOM) { const referralVolumeUSD = await fetchReferralVolume(timestamp); - const muxReferralVolumeUSD = await fetchMuxReferralVolume(timestamp); // errror + const muxReferralVolumeUSD = await fetchMuxReferralVolume(chain, timestamp); dailyVolumeUSD += referralVolumeUSD + muxReferralVolumeUSD; } @@ -203,4 +219,4 @@ const adapteraggderivative: any = { export { adapteraggderivative -} +} \ No newline at end of file