From 2641df4c07f56d45db16dd326186c9318d2d960c Mon Sep 17 00:00:00 2001 From: Enrique Ortiz Date: Wed, 2 Sep 2020 18:35:55 -0400 Subject: [PATCH] Fix uniswap price stats --- src/hooks/useUniswapAntPrice.js | 41 +++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/src/hooks/useUniswapAntPrice.js b/src/hooks/useUniswapAntPrice.js index a35f9d9..3cc042a 100644 --- a/src/hooks/useUniswapAntPrice.js +++ b/src/hooks/useUniswapAntPrice.js @@ -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 } } @@ -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 }