Skip to content

Commit

Permalink
Merge pull request #900 from DefiLlama/gmx-oi
Browse files Browse the repository at this point in the history
add oi gmx
  • Loading branch information
dtmkeng authored Oct 18, 2023
2 parents ae296bf + 2bb105b commit 337c729
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions dexs/gmx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<{
Expand All @@ -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)))
Expand All @@ -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)
Expand Down

0 comments on commit 337c729

Please sign in to comment.