mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
simplify and refactor pool charts
This commit is contained in:
parent
25204b2393
commit
5401390c44
@ -1,5 +1,11 @@
|
|||||||
/* eslint-disable camelcase */
|
/* eslint-disable camelcase */
|
||||||
import React, { ChangeEvent, ReactElement, useEffect, useState } from 'react'
|
import React, {
|
||||||
|
ChangeEvent,
|
||||||
|
ReactElement,
|
||||||
|
useCallback,
|
||||||
|
useEffect,
|
||||||
|
useState
|
||||||
|
} from 'react'
|
||||||
import { Line, defaults } from 'react-chartjs-2'
|
import { Line, defaults } from 'react-chartjs-2'
|
||||||
import {
|
import {
|
||||||
ChartData,
|
ChartData,
|
||||||
@ -103,15 +109,9 @@ function getOptions(locale: string, isDarkMode: boolean): ChartOptions {
|
|||||||
|
|
||||||
const graphTypes = ['Liquidity', 'Price']
|
const graphTypes = ['Liquidity', 'Price']
|
||||||
|
|
||||||
const poolHistory = gql`
|
const poolHistoryQuery = gql`
|
||||||
query PoolHistory($id: String!, $block: Int) {
|
query PoolHistory($id: String!) {
|
||||||
poolSnapshots(
|
poolSnapshots(first: 1000, where: { pool: $id }, orderBy: date) {
|
||||||
first: 1000
|
|
||||||
where: { pool: $id }
|
|
||||||
block: { number_gte: $block }
|
|
||||||
orderBy: date
|
|
||||||
subgraphError: deny
|
|
||||||
) {
|
|
||||||
date
|
date
|
||||||
spotPrice
|
spotPrice
|
||||||
baseTokenLiquidity
|
baseTokenLiquidity
|
||||||
@ -122,57 +122,36 @@ const poolHistory = gql`
|
|||||||
|
|
||||||
export default function Graph(): ReactElement {
|
export default function Graph(): ReactElement {
|
||||||
const { locale } = useUserPreferences()
|
const { locale } = useUserPreferences()
|
||||||
|
const { price, ddo } = useAsset()
|
||||||
const darkMode = useDarkMode(false, darkModeConfig)
|
const darkMode = useDarkMode(false, darkModeConfig)
|
||||||
const [options, setOptions] = useState<ChartOptions>()
|
const [options, setOptions] = useState<ChartOptions>()
|
||||||
const [graphType, setGraphType] = useState<GraphType>('liquidity')
|
const [graphType, setGraphType] = useState<GraphType>('liquidity')
|
||||||
|
|
||||||
const { price, ddo } = useAsset()
|
|
||||||
|
|
||||||
const [lastBlock, setLastBlock] = useState<number>(0)
|
|
||||||
const [priceHistory, setPriceHistory] = useState([])
|
|
||||||
const [error, setError] = useState<Error>()
|
const [error, setError] = useState<Error>()
|
||||||
const [liquidityHistory, setLiquidityHistory] = useState([])
|
|
||||||
const [timestamps, setTimestamps] = useState([])
|
|
||||||
const [isLoading, setIsLoading] = useState(true)
|
const [isLoading, setIsLoading] = useState(true)
|
||||||
const [dataHistory, setDataHistory] = useState<PoolHistory>()
|
const [dataHistory, setDataHistory] = useState<PoolHistory>()
|
||||||
const [graphData, setGraphData] = useState<ChartData>()
|
const [graphData, setGraphData] = useState<ChartData>()
|
||||||
const [graphFetchInterval, setGraphFetchInterval] = useState<NodeJS.Timeout>()
|
const [graphFetchInterval, setGraphFetchInterval] = useState<NodeJS.Timeout>()
|
||||||
|
|
||||||
async function getPoolHistory() {
|
const getPoolHistory = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
const queryContext = getQueryContext(ddo.chainId)
|
|
||||||
const queryVariables = {
|
|
||||||
id: price.address.toLowerCase(),
|
|
||||||
block: lastBlock
|
|
||||||
}
|
|
||||||
|
|
||||||
const queryResult: OperationResult<PoolHistory> = await fetchData(
|
const queryResult: OperationResult<PoolHistory> = await fetchData(
|
||||||
poolHistory,
|
poolHistoryQuery,
|
||||||
queryVariables,
|
{ id: price.address.toLowerCase() },
|
||||||
queryContext
|
getQueryContext(ddo.chainId)
|
||||||
)
|
)
|
||||||
setDataHistory(queryResult?.data)
|
setDataHistory(queryResult?.data)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetchData: ', error.message)
|
console.error('Error fetchData: ', error.message)
|
||||||
setError(error)
|
setError(error)
|
||||||
}
|
}
|
||||||
}
|
}, [ddo?.chainId, price?.address])
|
||||||
|
|
||||||
function refetchGraph() {
|
const refetchGraph = useCallback(async () => {
|
||||||
if (!graphFetchInterval) {
|
if (graphFetchInterval) return
|
||||||
setGraphFetchInterval(
|
|
||||||
setInterval(function () {
|
|
||||||
getPoolHistory()
|
|
||||||
}, REFETCH_INTERVAL)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
useEffect(() => {
|
const newInterval = setInterval(() => getPoolHistory(), REFETCH_INTERVAL)
|
||||||
return () => {
|
setGraphFetchInterval(newInterval)
|
||||||
clearInterval(graphFetchInterval)
|
}, [getPoolHistory, graphFetchInterval])
|
||||||
}
|
|
||||||
}, [graphFetchInterval])
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
LoggerInstance.log('Fired GraphOptions!')
|
LoggerInstance.log('Fired GraphOptions!')
|
||||||
@ -180,68 +159,51 @@ export default function Graph(): ReactElement {
|
|||||||
setOptions(options)
|
setOptions(options)
|
||||||
}, [locale, darkMode.value])
|
}, [locale, darkMode.value])
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
getPoolHistory()
|
|
||||||
}, [lastBlock])
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
async function init() {
|
async function init() {
|
||||||
const data: PoolHistory = dataHistory
|
if (!dataHistory) {
|
||||||
if (!data) {
|
|
||||||
await getPoolHistory()
|
await getPoolHistory()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
LoggerInstance.log('Fired GraphData!')
|
LoggerInstance.log('Fired GraphData!')
|
||||||
|
|
||||||
const latestTimestamps = [
|
const latestTimestamps = [
|
||||||
...timestamps,
|
...dataHistory.poolSnapshots.map((item) => {
|
||||||
...data.poolSnapshots.map((item) => {
|
|
||||||
const date = new Date(item.date * 1000)
|
const date = new Date(item.date * 1000)
|
||||||
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`
|
return `${date.toLocaleDateString()} ${date.toLocaleTimeString()}`
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
setTimestamps(latestTimestamps)
|
|
||||||
|
|
||||||
const latestLiquidityHistory = [
|
const latestLiquidityHistory = [
|
||||||
...liquidityHistory,
|
...dataHistory.poolSnapshots.map((item) => item.baseTokenLiquidity)
|
||||||
...data.poolSnapshots.map((item) => item.baseTokenLiquidity)
|
|
||||||
]
|
]
|
||||||
|
|
||||||
setLiquidityHistory(latestLiquidityHistory)
|
|
||||||
|
|
||||||
const latestPriceHistory = [
|
const latestPriceHistory = [
|
||||||
...priceHistory,
|
...dataHistory.poolSnapshots.map((item) => item.datatokenLiquidity)
|
||||||
...data.poolSnapshots.map((item) => item.datatokenLiquidity)
|
|
||||||
]
|
]
|
||||||
|
|
||||||
setPriceHistory(latestPriceHistory)
|
setGraphData({
|
||||||
|
labels: latestTimestamps.slice(0),
|
||||||
if (data.poolSnapshots.length > 0) {
|
datasets: [
|
||||||
const newBlock = data.poolSnapshots[data.poolSnapshots.length - 1].block
|
{
|
||||||
if (newBlock === lastBlock) return
|
...lineStyle,
|
||||||
setLastBlock(data.poolSnapshots[data.poolSnapshots.length - 1].block)
|
label: 'Liquidity (OCEAN)',
|
||||||
} else {
|
data:
|
||||||
setGraphData({
|
graphType === 'liquidity'
|
||||||
labels: latestTimestamps.slice(0),
|
? latestLiquidityHistory.slice(0)
|
||||||
datasets: [
|
: latestPriceHistory.slice(0),
|
||||||
{
|
borderColor: `#8b98a9`,
|
||||||
...lineStyle,
|
pointBackgroundColor: `#8b98a9`
|
||||||
label: 'Liquidity (OCEAN)',
|
}
|
||||||
data:
|
]
|
||||||
graphType === 'liquidity'
|
})
|
||||||
? latestLiquidityHistory.slice(0)
|
setIsLoading(false)
|
||||||
: latestPriceHistory.slice(0),
|
refetchGraph()
|
||||||
borderColor: `#8b98a9`,
|
|
||||||
pointBackgroundColor: `#8b98a9`
|
|
||||||
}
|
|
||||||
]
|
|
||||||
})
|
|
||||||
setIsLoading(false)
|
|
||||||
refetchGraph()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
init()
|
init()
|
||||||
}, [dataHistory, graphType])
|
|
||||||
|
return () => clearInterval(graphFetchInterval)
|
||||||
|
}, [dataHistory, graphType, graphFetchInterval, getPoolHistory, refetchGraph])
|
||||||
|
|
||||||
function handleGraphTypeSwitch(e: ChangeEvent<HTMLButtonElement>) {
|
function handleGraphTypeSwitch(e: ChangeEvent<HTMLButtonElement>) {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user