1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-09-28 03:58:59 +02:00
market/src/providers/Prices.tsx

61 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-10-28 10:32:56 +01:00
import React, {
useState,
ReactElement,
createContext,
useContext,
ReactNode
} from 'react'
import { fetchData } from '../utils'
import useSWR from 'swr'
import { useSiteMetadata } from '../hooks/useSiteMetadata'
import { Logger } from '@oceanprotocol/lib'
interface PricesValue {
[key: string]: number
2020-10-28 10:32:56 +01:00
}
const initialData: PricesValue = {
eur: 0.0,
usd: 0.0,
eth: 0.0,
btc: 0.0
2020-10-28 10:32:56 +01:00
}
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}`
2020-10-28 10:32:56 +01:00
const [prices, setPrices] = useState(initialData)
const onSuccess = async (data: { [tokenId]: { [key: string]: number } }) => {
2020-10-28 10:32:56 +01:00
if (!data) return
Logger.log(`Got new prices. ${JSON.stringify(data[tokenId])}`)
setPrices(data[tokenId])
2020-10-28 10:32:56 +01:00
}
// Fetch new prices periodically with swr
useSWR(url, fetchData, {
Swap tokens (#204) * swap Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * validation and calculation Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * refactor Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove unused effect Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * fix interval Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * increase refresh timer, remove optional params Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * make inputs show up without wallet * style fixes * restyling * styling * more styling * fix refresh price Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove test effect * fixes, get data as early as possible from DDO and initial state * refactor * refactor * refactor * label tweaks * copy * typo * prototype output * remove price header * ouput swap fee * fix * spacing * copy * refactor pool transaction titles * copy * update math Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * use messaging tweaks * tab tweaks, output refactor * fix dark mode selection style * prototype output * method tweaks * slippage to 1%, added warnig banner Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * form tweaks * error fix * empty inputs by default * longer intervals * maxOcean validation fix * slippage tolerance UI * modified slippage UI * refactor, refresh ocean user balance * move typings/models around * typing fix * fixed output values Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * bump oceanlib Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro> * remove console.log * remove placeholder * tweak * non-web3 browser tweak Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2020-11-16 16:21:15 +01:00
refreshInterval: 60000, // 60 sec.
onSuccess
})
2020-10-28 10:32:56 +01:00
return (
<PricesContext.Provider value={{ prices }}>
{children}
</PricesContext.Provider>
)
}
// Helper hook to access the provider values
const usePrices = (): PricesValue => useContext(PricesContext)
export { PricesProvider, usePrices, PricesValue }