diff --git a/dexs/gmx/index.ts b/dexs/gmx/index.ts index 7dde465a89..71ad1f43b1 100644 --- a/dexs/gmx/index.ts +++ b/dexs/gmx/index.ts @@ -24,6 +24,15 @@ const historicalDataDerivatives = gql` } } ` +const historicalOI = gql` + query get_trade_stats($period: String!, $id: String!) { + tradingStats(where: {period: $period, id: $id}) { + id + longOpenInterest + shortOpenInterest + } + } +` interface IGraphResponse { volumeStats: Array<{ @@ -34,6 +43,13 @@ interface IGraphResponse { swap: string, }> } +interface IGraphResponseOI { + tradingStats: Array<{ + id: string, + longOpenInterest: string, + shortOpenInterest: string, + }> +} const getFetch = (query: string)=> (chain: string): Fetch => async (timestamp: number) => { const dayTimestamp = getUniqStartOfTodayTimestamp(new Date((timestamp * 1000))) @@ -47,9 +63,27 @@ const getFetch = (query: string)=> (chain: string): Fetch => async (timestamp: n id: 'total', period: 'total', }) + let dailyOpenInterest = 0; + let dailyLongOpenInterest = 0; + let dailyShortOpenInterest = 0; + + if (query === historicalDataDerivatives) { + const tradingStats: IGraphResponseOI = await request(endpoints[chain], historicalOI, { + id: chain === CHAIN.ARBITRUM + ? String(dayTimestamp) + : String(dayTimestamp) + ':daily', + period: 'daily', + }); + dailyOpenInterest = Number(tradingStats.tradingStats[0].longOpenInterest) + Number(tradingStats.tradingStats[0].shortOpenInterest); + dailyLongOpenInterest = Number(tradingStats.tradingStats[0].longOpenInterest); + dailyShortOpenInterest = Number(tradingStats.tradingStats[0].shortOpenInterest); + } return { timestamp: dayTimestamp, + dailyLongOpenInterest: dailyLongOpenInterest ? String(dailyLongOpenInterest * 10 ** -30) : undefined, + dailyShortOpenInterest: dailyShortOpenInterest ? String(dailyShortOpenInterest * 10 ** -30) : undefined, + dailyOpenInterest: dailyOpenInterest ? String(dailyOpenInterest * 10 ** -30) : undefined, dailyVolume: dailyData.volumeStats.length == 1 ? String(Number(Object.values(dailyData.volumeStats[0]).reduce((sum, element) => String(Number(sum) + Number(element)))) * 10 ** -30)