1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-06-28 00:27:49 +02:00
market/src/@context/Prices.tsx
Matthias Kretschmann b8c2b01b54
Pool stats updates and pool context provider (#1057)
* remove datatokens from liquidity stats, multiply base token

* naming: Pool Creator Statistics → Owner Liquidity

* remove all the noise

* more pool stats data

* simplify user liquidity data structure, remove datatoken calculations

* chart tweaks, new calculation for liquidity

* tweaks

* todo

* frontpage error fixes

* account switch fixes

* comment out fees

* pool context provider

* double pool share

* move subgraph-related methods into context provider

* typing fix
2022-02-07 14:58:47 +00:00

67 lines
1.5 KiB
TypeScript

import React, {
useState,
ReactElement,
createContext,
useContext,
ReactNode
} from 'react'
import { fetchData } from '@utils/fetch'
import useSWR from 'swr'
import { useSiteMetadata } from '@hooks/useSiteMetadata'
import { LoggerInstance } from '@oceanprotocol/lib'
interface Prices {
[key: string]: number
}
interface PricesValue {
prices: Prices
}
const initialData: Prices = {
eur: 0.0,
usd: 0.0,
eth: 0.0,
btc: 0.0
}
const refreshInterval = 120000 // 120 sec.
const PricesContext = createContext(null)
export default function PricesProvider({
children
}: {
children: ReactNode
}): ReactElement {
const { appConfig } = useSiteMetadata()
const tokenId = 'ocean-protocol'
const currencies = appConfig.currencies.join(',') // comma-separated list
const url = `https://api.coingecko.com/api/v3/simple/price?ids=${tokenId}&vs_currencies=${currencies}`
const [prices, setPrices] = useState(initialData)
const onSuccess = async (data: { [tokenId]: Prices }) => {
if (!data) return
LoggerInstance.log('[prices] Got new OCEAN spot prices.', data[tokenId])
setPrices(data[tokenId])
}
// Fetch new prices periodically with swr
useSWR(url, fetchData, {
refreshInterval,
onSuccess
})
return (
<PricesContext.Provider value={{ prices }}>
{children}
</PricesContext.Provider>
)
}
// Helper hook to access the provider values
const usePrices = (): PricesValue => useContext(PricesContext)
export { PricesProvider, usePrices }