Skip to content

Commit

Permalink
Merge pull request #71 from jediswaplabs/iamoskvin/update_global_data
Browse files Browse the repository at this point in the history
Iamoskvin/update global data
  • Loading branch information
iamoskvin authored May 8, 2024
2 parents 3e18197 + 89b73db commit 56aec2e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
14 changes: 6 additions & 8 deletions src/apollo/queries.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gql from 'graphql-tag'
import { apiTimeframeOptions } from '../constants/index.js'

const TokenFields = `
fragment TokenFields on Token {
Expand Down Expand Up @@ -44,14 +45,11 @@ const PoolFields = `

export const HISTORICAL_GLOBAL_DATA = () => {
const queryString = ` query jediswapFactories {
factoriesDayData(first: 2, orderBy: "dayId", orderByDirection: "desc") {
totalValueLockedUSD
volumeUSD
feesUSD
dayId
id
}
}`
factoriesData {
${apiTimeframeOptions.oneDay}
${apiTimeframeOptions.twoDays}
}
}`
return gql(queryString)
}

Expand Down
2 changes: 1 addition & 1 deletion src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const getDefaultTokensListUrl = () => {

export const getApiUrl = () => {
if (isTestnetEnvironment()) {
return 'https://api.v2.goerli.jediswap.xyz/graphql'
return 'https://api.v2.sepolia.jediswap.xyz/graphql'
}
return 'https://api.v2.jediswap.xyz/graphql'
}
Expand Down
46 changes: 31 additions & 15 deletions src/contexts/GlobalData.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import utc from 'dayjs/plugin/utc'
import { jediSwapClient } from '../apollo/client'

import { useTimeframe, useWhitelistedTokens } from './Application'
import { getPercentChange, getTimeframe, convertDateToUnixFormat } from '../utils'
import { getPercentChange, getTimeframe, convertDateToUnixFormat, get2DayPercentChange } from '../utils'

import { GLOBAL_CHART } from '../apollo/queries'
import { HISTORICAL_GLOBAL_DATA } from '../apollo/queries'

import weekOfYear from 'dayjs/plugin/weekOfYear'
import { useAllPairData } from './PairData.js'
import { useAllTokenData } from './TokenData.js'
import { apiTimeframeOptions } from '../constants/index.js'

const UPDATE = 'UPDATE'
const UPDATE_TXNS = 'UPDATE_TXNS'
Expand Down Expand Up @@ -193,25 +194,40 @@ export default function Provider({ children }) {
* 24 hour USD changes.
*/
async function getGlobalData() {
const dataFinal = {}
let dataFinal = {}
try {
// data for each day , historic data used for % changes
let historyResult = await jediSwapClient.query({
// let historyResult = await jediSwapClient.query({
// query: HISTORICAL_GLOBAL_DATA(),
// fetchPolicy: 'cache-first',
// })
// console.log('historyResult', historyResult)
// const todayData = historyResult?.data?.factoriesDayData?.[0]
// const yesterdayData = historyResult?.data?.factoriesDayData?.[1]

// if (todayData && yesterdayData) {
// dataFinal.totalValueLockedUSD = todayData.totalValueLockedUSD
// dataFinal.liquidityChangeUSD = getPercentChange(todayData.totalValueLockedUSD, yesterdayData.totalValueLockedUSD)

// dataFinal.totalVolumeUSD = todayData.volumeUSD
// dataFinal.volumeChangeUSD = getPercentChange(todayData.volumeUSD, yesterdayData.volumeUSD)

// dataFinal.totalFeesUSD = todayData.feesUSD
// dataFinal.feesChangeUSD = getPercentChange(todayData.feesUSD, yesterdayData.feesUSD)
// }
const historicalData = await jediSwapClient.query({
query: HISTORICAL_GLOBAL_DATA(),
fetchPolicy: 'cache-first',
})
const todayData = historyResult?.data?.factoriesDayData?.[0]
const yesterdayData = historyResult?.data?.factoriesDayData?.[1]

if (todayData && yesterdayData) {
dataFinal.totalValueLockedUSD = todayData.totalValueLockedUSD
dataFinal.liquidityChangeUSD = getPercentChange(todayData.totalValueLockedUSD, yesterdayData.totalValueLockedUSD)

dataFinal.totalVolumeUSD = todayData.volumeUSD
dataFinal.volumeChangeUSD = getPercentChange(todayData.volumeUSD, yesterdayData.volumeUSD)

dataFinal.totalFeesUSD = todayData.feesUSD
dataFinal.feesChangeUSD = getPercentChange(todayData.feesUSD, yesterdayData.feesUSD)
const oneDayData = historicalData.data.factoriesData[0][apiTimeframeOptions.oneDay]
const twoDaysData = historicalData.data.factoriesData[0][apiTimeframeOptions.twoDays]
dataFinal = {
totalValueLockedUSD: oneDayData.totalValueLockedUSD,
liquidityChangeUSD: getPercentChange(oneDayData.totalValueLockedUSD, oneDayData.totalValueLockedUSDFirst),
totalVolumeUSD: oneDayData.volumeUSD,
volumeChangeUSD: get2DayPercentChange(oneDayData.volumeUSD, twoDaysData.volumeUSD),
totalFeesUSD: oneDayData.feesUSD,
feesChangeUSD: get2DayPercentChange(oneDayData.feesUSD, twoDaysData.feesUSD),
}
} catch (e) {
console.log(e)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const isTestnetEnvironment = () => {
return false
}
const host = new URL(String(window.location))?.host || ''
return host === 'info.v2.goerli.jediswap.xyz' || host === 'info.v2.testnet.jediswap.xyz'
return host === 'info.v2.sepolia.jediswap.xyz' || host === 'info.sepolia.jediswap.xyz'
}

export const isStagingEnvironment = () => {
Expand Down

0 comments on commit 56aec2e

Please sign in to comment.