Skip to content

Commit

Permalink
update new api for coingecko (#2125)
Browse files Browse the repository at this point in the history
* update new api for coingecko

* add missing tooltip

* update auth access token
  • Loading branch information
XiaoYhun authored Jul 28, 2023
1 parent f2100ed commit 1489e06
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ export const BLOCKED_PRICE_IMPACT_NON_DEGEN: Percent = new Percent(JSBI.BigInt(1

export const BUNDLE_ID = '1'

export const COINGECKO_BFF_API_URL = `${ENV.BFF_API}/v1/coingecko/api/v3`
export const COINGECKO_API_URL = 'https://api.coingecko.com/api/v3'

export const KNC_COINGECKO_ID = 'kyber-network-crystal'

export const ETHER_ADDRESS = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'
Expand Down
39 changes: 22 additions & 17 deletions src/hooks/useBasicChartData.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { KyberOauth2Api } from '@kybernetwork/oauth2'
import { ChainId, Token, WETH } from '@kyberswap/ks-sdk-core'
import axios from 'axios'
import { getUnixTime, subHours } from 'date-fns'
import { useMemo } from 'react'
import useSWR from 'swr'

import { AGGREGATOR_API, PRICE_CHART_API } from 'constants/env'
import { COINGECKO_API_URL } from 'constants/index'
import { NETWORKS_INFO } from 'constants/networks'
import { useActiveWeb3React } from 'hooks'

import useCoingeckoAPI from './useCoingeckoAPI'

export enum LiveDataTimeframeEnum {
HOUR = '1H',
FOUR_HOURS = '4H',
Expand Down Expand Up @@ -37,6 +39,7 @@ const getTimeFrameHours = (timeFrame: LiveDataTimeframeEnum) => {
}
}
const generateCoingeckoUrl = (
coingeckoAPI: string,
chainId: ChainId,
address: string | undefined,
timeFrame: LiveDataTimeframeEnum | 'live',
Expand All @@ -46,7 +49,7 @@ const generateCoingeckoUrl = (
timeFrame === 'live' ? timeTo - 1000 : getUnixTime(subHours(new Date(), getTimeFrameHours(timeFrame)))
const cgkId = NETWORKS_INFO[chainId].coingeckoNetworkId
if (!cgkId) return ''
return `${COINGECKO_API_URL}/coins/${cgkId}/contract/${address}/market_chart/range?vs_currency=usd&from=${timeFrom}&to=${timeTo}`
return `${coingeckoAPI}/coins/${cgkId}/contract/${address}/market_chart/range?vs_currency=usd&from=${timeFrom}&to=${timeTo}`
}
const getClosestPrice = (prices: any[], time: number) => {
let closestIndex = 0
Expand All @@ -67,32 +70,30 @@ const fetchKyberDataSWR = async (url: string) => {
}

const fetchKyberDataSWRWithHeader = async (url: string) => {
const res = await axios
.get(url, {
timeout: 5000,
headers: {
'accept-version': 'Latest',
},
})
.catch(error => {
throw error
})
const res = await KyberOauth2Api.get(url, {
timeout: 5000,
headers: {
'accept-version': 'Latest',
},
}).catch(error => {
throw error
})

if (res.status === 204) {
throw new Error('No content')
}
return res.data
}

const fetchCoingeckoDataSWR = async ([tokenAddresses, chainIds, timeFrame]: [
const fetchCoingeckoDataSWR = async ([tokenAddresses, chainIds, timeFrame, coingeckoAPI]: [
tokenAddresses: string[],
chainIds: ChainId[],
timeFrame: any,
coingeckoAPI: string,
]): Promise<any> => {
return await Promise.all(
[tokenAddresses[0], tokenAddresses[1]].map((address, i) =>
axios
.get(generateCoingeckoUrl(chainIds[i], address, timeFrame), { timeout: 5000 })
KyberOauth2Api.get(generateCoingeckoUrl(coingeckoAPI, chainIds[i], address, timeFrame), { timeout: 5000 })
.then(res => {
if (res.status === 204) {
throw new Error('No content')
Expand All @@ -113,6 +114,7 @@ export default function useBasicChartData(
timeFrame: LiveDataTimeframeEnum,
): { data: ChartData[]; loading: boolean; error: any } {
const { chainId, isEVM, networkInfo } = useActiveWeb3React()
const coingeckoAPI = useCoingeckoAPI()

const isReverse = useMemo(() => {
if (!tokens || !tokens[0] || !tokens[1] || tokens[0].equals(tokens[1]) || tokens[0].chainId !== tokens[1].chainId)
Expand All @@ -135,8 +137,9 @@ export default function useBasicChartData(
isValidating: coingeckoLoading,
} = useSWR(
tokenAddresses[0] && tokenAddresses[1]
? [tokenAddresses, [tokens[0]?.chainId, tokens[1]?.chainId], timeFrame]
? [tokenAddresses, [tokens[0]?.chainId, tokens[1]?.chainId], timeFrame, coingeckoAPI]
: null,

fetchCoingeckoDataSWR,
{
shouldRetryOnError: false,
Expand Down Expand Up @@ -210,7 +213,9 @@ export default function useBasicChartData(
)

const { data: liveCoingeckoData } = useSWR(
isKyberDataNotValid && coingeckoData ? [tokenAddresses, [tokens[0]?.chainId, tokens[1]?.chainId], 'live'] : null,
isKyberDataNotValid && coingeckoData
? [tokenAddresses, [tokens[0]?.chainId, tokens[1]?.chainId], 'live', coingeckoAPI]
: null,
fetchCoingeckoDataSWR,
{
refreshInterval: 60000,
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/useCoingeckoAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { COINGECKO_API_URL, COINGECKO_BFF_API_URL } from 'constants/index'
import { useSessionInfo } from 'state/authen/hooks'

export default function useCoingeckoAPI() {
const { authenticationSuccess } = useSessionInfo()
return authenticationSuccess ? COINGECKO_BFF_API_URL : COINGECKO_API_URL
}
24 changes: 19 additions & 5 deletions src/hooks/useTokenInfo.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { KyberOauth2Api } from '@kybernetwork/oauth2'
import { Token, WETH } from '@kyberswap/ks-sdk-core'
import useSWR from 'swr'

import { COINGECKO_API_URL } from 'constants/index'
import { NETWORKS_INFO } from 'constants/networks'
import { useActiveWeb3React } from 'hooks'

import useCoingeckoAPI from './useCoingeckoAPI'

export interface TokenInfo {
price: number
marketCap: number
Expand All @@ -21,18 +23,30 @@ export interface TokenInfo {
export default function useTokenInfo(token: Token | undefined): { data: TokenInfo; loading: boolean; error: any } {
const { isSolana, chainId: currentChain } = useActiveWeb3React()
const chainId = token?.chainId || currentChain

const fetcher = (url: string) => (url ? fetch(url).then(r => r.json()) : Promise.reject({ data: {}, error: '' }))
const coingeckoAPI = useCoingeckoAPI()
const fetcher = (url: string) =>
url
? KyberOauth2Api.get(url)
.then(res => {
if (res.status === 204) {
throw new Error('No content')
}
return res.data
})
.catch(error => {
throw error
})
: Promise.reject({ data: {}, error: '' })

const tokenAddress = isSolana ? token?.address || '' : (token?.address || '').toLowerCase()

let url = ''

if (tokenAddress.toLowerCase() === WETH[chainId].address.toLowerCase()) {
// If the token is native token, we have to use different endpoint
url = `${COINGECKO_API_URL}/coins/${NETWORKS_INFO[chainId].coingeckoNativeTokenId}`
url = `${coingeckoAPI}/coins/${NETWORKS_INFO[chainId].coingeckoNativeTokenId}`
} else if (tokenAddress) {
url = `${COINGECKO_API_URL}/coins/${NETWORKS_INFO[chainId].coingeckoNetworkId}/contract/${tokenAddress}`
url = `${coingeckoAPI}/coins/${NETWORKS_INFO[chainId].coingeckoNetworkId}/contract/${tokenAddress}`
}

const { data, error } = useSWR(url, fetcher, {
Expand Down
11 changes: 4 additions & 7 deletions src/pages/SwapV3/HeaderRightMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ export default function HeaderRightMenu({
type={TutorialType.SWAP}
customIcon={
<StyledActionButtonSwapForm onClick={() => mixpanelHandler(MIXPANEL_TYPE.SWAP_TUTORIAL_CLICK)}>
<TutorialIcon />
<MouseoverTooltip text={t`Tutorial`} placement="top" width="fit-content" disableTooltip={isMobile}>
<TutorialIcon />
</MouseoverTooltip>
</StyledActionButtonSwapForm>
}
/>
Expand All @@ -130,12 +132,7 @@ export default function HeaderRightMenu({
}}
aria-label="Swap Settings"
>
<MouseoverTooltip
text={<Trans>Settings</Trans>}
placement="top"
width="fit-content"
disableTooltip={isMobile}
>
<MouseoverTooltip text={t`Settings`} placement="top" width="fit-content" disableTooltip={isMobile}>
<TransactionSettingsIconWrapper id={TutorialIds.BUTTON_SETTING_SWAP_FORM}>
<TransactionSettingsIcon fill={theme.subText} />
</TransactionSettingsIconWrapper>
Expand Down

0 comments on commit 1489e06

Please sign in to comment.