mirror of
https://github.com/oceanprotocol/market.git
synced 2024-12-02 05:57:29 +01:00
add volume graph
This commit is contained in:
parent
d43920548f
commit
52437616f2
@ -6,7 +6,7 @@ import React, {
|
|||||||
useEffect,
|
useEffect,
|
||||||
useState
|
useState
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import { Line, defaults } from 'react-chartjs-2'
|
import { Line, defaults, Bar } from 'react-chartjs-2'
|
||||||
import {
|
import {
|
||||||
ChartData,
|
ChartData,
|
||||||
ChartDataSets,
|
ChartDataSets,
|
||||||
@ -28,7 +28,7 @@ import { fetchData, getQueryContext } from '@utils/subgraph'
|
|||||||
import styles from './Graph.module.css'
|
import styles from './Graph.module.css'
|
||||||
import Decimal from 'decimal.js'
|
import Decimal from 'decimal.js'
|
||||||
|
|
||||||
declare type GraphType = 'liquidity' | 'price'
|
declare type GraphType = 'liquidity' | 'price' | 'volume'
|
||||||
|
|
||||||
// Chart.js global defaults
|
// Chart.js global defaults
|
||||||
defaults.global.defaultFontFamily = `'Sharp Sans', -apple-system, BlinkMacSystemFont,
|
defaults.global.defaultFontFamily = `'Sharp Sans', -apple-system, BlinkMacSystemFont,
|
||||||
@ -108,7 +108,7 @@ function getOptions(locale: string, isDarkMode: boolean): ChartOptions {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const graphTypes = ['Liquidity', 'Price']
|
const graphTypes = ['Liquidity', 'Price', 'Volume']
|
||||||
|
|
||||||
const poolHistoryQuery = gql`
|
const poolHistoryQuery = gql`
|
||||||
query PoolHistory($id: String!) {
|
query PoolHistory($id: String!) {
|
||||||
@ -117,6 +117,7 @@ const poolHistoryQuery = gql`
|
|||||||
spotPrice
|
spotPrice
|
||||||
baseTokenLiquidity
|
baseTokenLiquidity
|
||||||
datatokenLiquidity
|
datatokenLiquidity
|
||||||
|
swapVolume
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
@ -168,35 +169,51 @@ export default function Graph(): ReactElement {
|
|||||||
}
|
}
|
||||||
LoggerInstance.log('Fired GraphData!')
|
LoggerInstance.log('Fired GraphData!')
|
||||||
|
|
||||||
const latestTimestamps = dataHistory.poolSnapshots.map((item) => {
|
const timestamps = dataHistory.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()}`
|
||||||
})
|
})
|
||||||
|
|
||||||
let baseTokenLiquidityCumulative = '0'
|
let baseTokenLiquidityCumulative = '0'
|
||||||
|
const liquidityHistory = dataHistory.poolSnapshots.map((item) => {
|
||||||
const latestLiquidityHistory = dataHistory.poolSnapshots.map((item) => {
|
|
||||||
baseTokenLiquidityCumulative = new Decimal(baseTokenLiquidityCumulative)
|
baseTokenLiquidityCumulative = new Decimal(baseTokenLiquidityCumulative)
|
||||||
.add(item.baseTokenLiquidity)
|
.add(item.baseTokenLiquidity)
|
||||||
.toString()
|
.toString()
|
||||||
|
|
||||||
return baseTokenLiquidityCumulative
|
return baseTokenLiquidityCumulative
|
||||||
})
|
})
|
||||||
|
|
||||||
const latestPriceHistory = dataHistory.poolSnapshots.map(
|
const priceHistory = dataHistory.poolSnapshots.map(
|
||||||
(item) => item.spotPrice
|
(item) => item.spotPrice
|
||||||
)
|
)
|
||||||
|
|
||||||
|
let volumeCumulative = '0'
|
||||||
|
const volumeHistory = dataHistory.poolSnapshots.map((item) => {
|
||||||
|
volumeCumulative = new Decimal(volumeCumulative)
|
||||||
|
.add(item.swapVolume)
|
||||||
|
.toString()
|
||||||
|
return baseTokenLiquidityCumulative
|
||||||
|
})
|
||||||
|
|
||||||
|
let data
|
||||||
|
switch (graphType) {
|
||||||
|
case 'price':
|
||||||
|
data = priceHistory.slice(0)
|
||||||
|
break
|
||||||
|
case 'volume':
|
||||||
|
data = volumeHistory.slice(0)
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
data = liquidityHistory.slice(0)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
setGraphData({
|
setGraphData({
|
||||||
labels: latestTimestamps.slice(0),
|
labels: timestamps.slice(0),
|
||||||
datasets: [
|
datasets: [
|
||||||
{
|
{
|
||||||
...lineStyle,
|
...lineStyle,
|
||||||
label: 'Liquidity (OCEAN)',
|
label: 'Liquidity (OCEAN)',
|
||||||
data:
|
data,
|
||||||
graphType === 'liquidity'
|
|
||||||
? latestLiquidityHistory.slice(0)
|
|
||||||
: latestPriceHistory.slice(0),
|
|
||||||
borderColor: `#8b98a9`,
|
borderColor: `#8b98a9`,
|
||||||
pointBackgroundColor: `#8b98a9`
|
pointBackgroundColor: `#8b98a9`
|
||||||
}
|
}
|
||||||
@ -238,7 +255,12 @@ export default function Graph(): ReactElement {
|
|||||||
</Button>
|
</Button>
|
||||||
))}
|
))}
|
||||||
</nav>
|
</nav>
|
||||||
<Line height={70} data={graphData} options={options} />
|
|
||||||
|
{graphType === 'volume' ? (
|
||||||
|
<Bar height={70} data={graphData} options={options} />
|
||||||
|
) : (
|
||||||
|
<Line height={70} data={graphData} options={options} />
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user