Skip to content

Commit

Permalink
Fix uniswap price stats
Browse files Browse the repository at this point in the history
  • Loading branch information
Evalir committed Sep 2, 2020
1 parent 16408e6 commit 2641df4
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions src/hooks/useUniswapAntPrice.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ import gql from 'graphql-tag'
import { GraphQLWrapper } from '@aragon/connect-thegraph'
import * as Sentry from '@sentry/browser'

const RETRY_EVERY = 3000

const UNISWAP_URL = 'https://api.thegraph.com/subgraphs/name/lutter/uniswap-v2'
const DAI_ANT_PAIR_ID = '0x7286656f87b60e7560ef0a0e53cfa41f450ef993'
const ANT_ETH_PAIR = '0xfa19de406e8f5b9100e4dd5cad8a503a6d686efe'
const DAI_ETH_PAIR = '0xa478c2975ab1ea89e8196811f51a7b7ade33eb11'

const UNISWAP_PAIR_QUERY = gql`
const ANT_PRICE_QUERY = gql`
query {
pair(id: "${DAI_ANT_PAIR_ID}") {
pair(id: "${ANT_ETH_PAIR}") {
token1Price
}
}
`
const ETH_PRICE_QUERY = gql`
query {
pair(id: "${DAI_ETH_PAIR}") {
token0Price
}
}
Expand All @@ -20,23 +30,36 @@ export function useUniswapAntPrice() {

useEffect(() => {
let cancelled = false
let retryTimer
async function getData() {
try {
const results = await wrapper.performQuery(UNISWAP_PAIR_QUERY)
const antResults = await wrapper.performQuery(ANT_PRICE_QUERY)
const ethResults = await wrapper.performQuery(ETH_PRICE_QUERY)
const { pair: antPair } = antResults.data
const { pair: ethPair } = ethResults.data
const antToEthPrice = antPair.token1Price
const daiToEthPrice = ethPair.token0Price
const antPrice = Number(antToEthPrice) * Number(daiToEthPrice)

const { pair } = results.data
if (!cancelled) {
setAntPrice(pair.token0Price)
setAntPrice(antPrice)
}
} catch (err) {
Sentry.captureException(err)
console.log(err)
retryTimer = setTimeout(getData, RETRY_EVERY)
if (process.env.NODE_ENV === 'production') {
Sentry.captureException(err)
}
}
}

getData()

return () => (cancelled = true)
})
return () => {
cancelled = true
clearTimeout(retryTimer)
}
}, [wrapper])

return antPrice
}

0 comments on commit 2641df4

Please sign in to comment.