2022-12-06 08:41:57 -08:00
|
|
|
import { Dispatch, SetStateAction, useEffect, useRef, useState } from 'react'
|
2022-12-05 09:19:59 -08:00
|
|
|
import mangoStore from '@store/mangoStore'
|
|
|
|
import klinecharts, { init, dispose } from 'klinecharts'
|
|
|
|
import axios from 'axios'
|
|
|
|
import { useViewport } from 'hooks/useViewport'
|
|
|
|
import usePrevious from '@components/shared/usePrevious'
|
|
|
|
import Modal from '@components/shared/Modal'
|
|
|
|
import Switch from '@components/forms/Switch'
|
2022-12-05 15:54:47 -08:00
|
|
|
import {
|
|
|
|
BASE_CHART_QUERY,
|
|
|
|
CHART_QUERY,
|
2022-12-08 06:11:09 -08:00
|
|
|
DEFAULT_MAIN_INDICATORS,
|
2022-12-05 15:54:47 -08:00
|
|
|
DEFAULT_SUB_INDICATOR,
|
|
|
|
HISTORY,
|
|
|
|
mainTechnicalIndicatorTypes,
|
|
|
|
MAIN_INDICATOR_CLASS,
|
|
|
|
ONE_DAY_SECONDS,
|
|
|
|
RES_NAME_TO_RES_VAL,
|
|
|
|
subTechnicalIndicatorTypes,
|
|
|
|
} from 'utils/kLineChart'
|
|
|
|
import { ArrowsPointingOutIcon } from '@heroicons/react/24/outline'
|
2022-12-06 08:41:57 -08:00
|
|
|
import Loading from '@components/shared/Loading'
|
2022-12-06 13:58:49 -08:00
|
|
|
import clsx from 'clsx'
|
2022-12-21 03:21:04 -08:00
|
|
|
import { API_URL, BE_API_KEY } from 'apis/birdeye/helpers'
|
2022-12-05 09:19:59 -08:00
|
|
|
|
2022-12-08 06:11:09 -08:00
|
|
|
const UPDATE_INTERVAL = 10000
|
|
|
|
|
2022-12-06 08:41:57 -08:00
|
|
|
type Props = {
|
|
|
|
setIsFullView?: Dispatch<SetStateAction<boolean>>
|
|
|
|
isFullView?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
const TradingViewChartKline = ({ setIsFullView, isFullView }: Props) => {
|
2022-12-05 09:19:59 -08:00
|
|
|
const { width } = useViewport()
|
|
|
|
const prevWidth = usePrevious(width)
|
2022-12-21 09:31:25 -08:00
|
|
|
const selectedMarket = mangoStore((s) => s.selectedMarket.current)
|
|
|
|
const selectedMarketName = selectedMarket?.name
|
2022-12-05 09:19:59 -08:00
|
|
|
const [isTechnicalModalOpen, setIsTechnicalModalOpen] = useState(false)
|
|
|
|
const [mainTechnicalIndicators, setMainTechnicalIndicators] = useState<
|
|
|
|
string[]
|
|
|
|
>([])
|
2022-12-05 15:54:47 -08:00
|
|
|
const [subTechnicalIndicators, setSubTechnicalIndicators] = useState<{
|
|
|
|
//indicatorName: class
|
|
|
|
[indicatorName: string]: string
|
|
|
|
}>({})
|
2022-12-06 08:41:57 -08:00
|
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
|
|
const [resolution, setResolution] = useState(RES_NAME_TO_RES_VAL['1H'])
|
2022-12-05 09:19:59 -08:00
|
|
|
const [chart, setChart] = useState<klinecharts.Chart | null>(null)
|
2022-12-06 08:41:57 -08:00
|
|
|
const previousChart = usePrevious(chart)
|
2022-12-05 09:19:59 -08:00
|
|
|
const [baseChartQuery, setQuery] = useState<BASE_CHART_QUERY | null>(null)
|
|
|
|
const clearTimerRef = useRef<NodeJS.Timeout | null>(null)
|
|
|
|
const fetchData = async (baseQuery: BASE_CHART_QUERY, from: number) => {
|
|
|
|
try {
|
2022-12-06 08:41:57 -08:00
|
|
|
setIsLoading(true)
|
2022-12-05 09:19:59 -08:00
|
|
|
const query: CHART_QUERY = {
|
|
|
|
...baseQuery,
|
2022-12-21 03:21:04 -08:00
|
|
|
time_from: from,
|
2022-12-05 09:19:59 -08:00
|
|
|
}
|
2022-12-21 03:21:04 -08:00
|
|
|
const response = await axios.get(`${API_URL}defi/ohlcv/pair`, {
|
2022-12-05 09:19:59 -08:00
|
|
|
params: query,
|
2022-12-21 03:21:04 -08:00
|
|
|
headers: {
|
|
|
|
'X-API-KEY': BE_API_KEY,
|
|
|
|
},
|
2022-12-05 09:19:59 -08:00
|
|
|
})
|
2022-12-21 03:21:04 -08:00
|
|
|
const newData = response.data.data.items as HISTORY[]
|
|
|
|
const dataSize = newData.length
|
2022-12-05 09:19:59 -08:00
|
|
|
const dataList = []
|
|
|
|
for (let i = 0; i < dataSize; i++) {
|
2022-12-21 03:21:04 -08:00
|
|
|
const row = newData[i]
|
2022-12-05 09:19:59 -08:00
|
|
|
const kLineModel = {
|
2022-12-21 03:21:04 -08:00
|
|
|
open: row.o,
|
|
|
|
low: row.l,
|
|
|
|
high: row.h,
|
|
|
|
close: row.c,
|
|
|
|
volume: row.v,
|
|
|
|
timestamp: row.unixTime * 1000,
|
2022-12-05 09:19:59 -08:00
|
|
|
}
|
|
|
|
dataList.push(kLineModel)
|
|
|
|
}
|
2022-12-06 08:41:57 -08:00
|
|
|
setIsLoading(false)
|
2022-12-05 09:19:59 -08:00
|
|
|
return dataList
|
|
|
|
} catch (e) {
|
2022-12-06 08:41:57 -08:00
|
|
|
setIsLoading(false)
|
2022-12-05 09:19:59 -08:00
|
|
|
console.log(e)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-08 06:11:09 -08:00
|
|
|
//update data every 10 secs
|
2022-12-05 09:19:59 -08:00
|
|
|
function updateData(
|
|
|
|
kLineChart: klinecharts.Chart,
|
|
|
|
baseQuery: BASE_CHART_QUERY
|
|
|
|
) {
|
|
|
|
if (clearTimerRef.current) {
|
|
|
|
clearInterval(clearTimerRef.current)
|
|
|
|
}
|
|
|
|
clearTimerRef.current = setTimeout(async () => {
|
|
|
|
if (kLineChart) {
|
2022-12-21 03:21:04 -08:00
|
|
|
const from = baseQuery.time_to - resolution.seconds
|
2022-12-05 09:19:59 -08:00
|
|
|
const newData = (await fetchData(baseQuery!, from))[0]
|
|
|
|
if (newData) {
|
2022-12-08 06:11:09 -08:00
|
|
|
newData.timestamp += UPDATE_INTERVAL
|
2022-12-05 09:19:59 -08:00
|
|
|
kLineChart.updateData(newData)
|
|
|
|
updateData(kLineChart, baseQuery)
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 06:11:09 -08:00
|
|
|
}, UPDATE_INTERVAL)
|
2022-12-05 09:19:59 -08:00
|
|
|
}
|
|
|
|
const fetchFreshData = async (daysToSubtractFromToday: number) => {
|
|
|
|
const from =
|
|
|
|
Math.floor(Date.now() / 1000) - ONE_DAY_SECONDS * daysToSubtractFromToday
|
|
|
|
const data = await fetchData(baseChartQuery!, from)
|
|
|
|
if (chart) {
|
|
|
|
chart.applyNewData(data)
|
|
|
|
//after we fetch fresh data start to update data every x seconds
|
|
|
|
updateData(chart, baseChartQuery!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-08 06:11:09 -08:00
|
|
|
//size change
|
2022-12-05 09:19:59 -08:00
|
|
|
useEffect(() => {
|
|
|
|
if (width !== prevWidth && chart) {
|
|
|
|
//wait for event que to be empty
|
|
|
|
//to have current width
|
|
|
|
setTimeout(() => {
|
2022-12-05 15:54:47 -08:00
|
|
|
chart?.resize()
|
2022-12-05 09:19:59 -08:00
|
|
|
}, 0)
|
|
|
|
}
|
|
|
|
}, [width])
|
|
|
|
|
2022-12-08 06:11:09 -08:00
|
|
|
//when base query change we refetch with fresh data
|
2022-12-05 09:19:59 -08:00
|
|
|
useEffect(() => {
|
|
|
|
if (chart && baseChartQuery) {
|
|
|
|
fetchFreshData(14)
|
|
|
|
//add callback to fetch more data when zoom out
|
|
|
|
chart.loadMore(() => {
|
2022-12-21 09:31:25 -08:00
|
|
|
try {
|
|
|
|
fetchFreshData(365)
|
|
|
|
} catch (e) {
|
|
|
|
console.log('Error fetching new data')
|
|
|
|
}
|
|
|
|
chart.loadMore(() => null)
|
2022-12-05 09:19:59 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [baseChartQuery])
|
|
|
|
|
|
|
|
//change query based on market and resolution
|
|
|
|
useEffect(() => {
|
|
|
|
if (selectedMarketName && resolution) {
|
|
|
|
setQuery({
|
2022-12-21 03:21:04 -08:00
|
|
|
type: resolution.val,
|
|
|
|
address: '8BnEgHoWFysVcuFFX7QztDmzuH8r5ZFvyP3sYwn1XTh6',
|
|
|
|
time_to: Math.floor(Date.now() / 1000),
|
2022-12-05 09:19:59 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [selectedMarketName, resolution])
|
|
|
|
|
2022-12-08 06:11:09 -08:00
|
|
|
//init default technical indicators after init of chart
|
|
|
|
useEffect(() => {
|
|
|
|
if (chart !== null && previousChart === null) {
|
|
|
|
if (DEFAULT_SUB_INDICATOR) {
|
|
|
|
const subId = chart.createTechnicalIndicator(
|
|
|
|
DEFAULT_SUB_INDICATOR,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
setSubTechnicalIndicators({ [DEFAULT_SUB_INDICATOR]: subId })
|
|
|
|
}
|
|
|
|
if (DEFAULT_MAIN_INDICATORS?.length) {
|
|
|
|
for (const type of DEFAULT_MAIN_INDICATORS) {
|
|
|
|
chart?.createTechnicalIndicator(type, true, {
|
|
|
|
id: MAIN_INDICATOR_CLASS,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
setMainTechnicalIndicators(DEFAULT_MAIN_INDICATORS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [chart !== null])
|
|
|
|
|
2022-12-05 09:19:59 -08:00
|
|
|
//init chart without data
|
|
|
|
useEffect(() => {
|
|
|
|
const initKline = async () => {
|
|
|
|
const style = getComputedStyle(document.body)
|
|
|
|
const gridColor = style.getPropertyValue('--bkg-3')
|
|
|
|
const kLineChart = init('update-k-line')
|
2022-12-05 15:54:47 -08:00
|
|
|
kLineChart.setStyleOptions({
|
2022-12-05 09:19:59 -08:00
|
|
|
grid: {
|
|
|
|
show: true,
|
|
|
|
horizontal: {
|
|
|
|
style: 'solid',
|
|
|
|
color: gridColor,
|
|
|
|
},
|
|
|
|
vertical: {
|
|
|
|
style: 'solid',
|
|
|
|
color: gridColor,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
candle: {
|
|
|
|
tooltip: {
|
|
|
|
labels: ['T: ', 'O: ', 'C: ', 'H: ', 'L: ', 'V: '],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
xAxis: {
|
|
|
|
axisLine: {
|
|
|
|
show: true,
|
|
|
|
color: gridColor,
|
|
|
|
size: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
yAxis: {
|
|
|
|
axisLine: {
|
|
|
|
show: true,
|
|
|
|
color: gridColor,
|
|
|
|
size: 1,
|
|
|
|
},
|
|
|
|
},
|
2022-12-05 15:54:47 -08:00
|
|
|
separator: {
|
|
|
|
size: 2,
|
|
|
|
color: gridColor,
|
|
|
|
},
|
2022-12-05 09:19:59 -08:00
|
|
|
})
|
|
|
|
setChart(kLineChart)
|
|
|
|
}
|
|
|
|
initKline()
|
|
|
|
return () => {
|
|
|
|
dispose('update-k-line')
|
|
|
|
}
|
|
|
|
}, [])
|
2022-12-08 06:11:09 -08:00
|
|
|
|
2022-12-05 09:19:59 -08:00
|
|
|
return (
|
2022-12-06 08:41:57 -08:00
|
|
|
<div
|
2022-12-06 13:58:49 -08:00
|
|
|
className={clsx(
|
|
|
|
'fixed h-full w-full',
|
|
|
|
isFullView &&
|
|
|
|
'left-[64px] top-0 right-0 bottom-0 bg-th-bkg-1 text-th-fgd-1'
|
|
|
|
)}
|
2022-12-06 08:41:57 -08:00
|
|
|
>
|
2022-12-05 15:54:47 -08:00
|
|
|
<div className="flex w-full">
|
2022-12-05 09:19:59 -08:00
|
|
|
{Object.keys(RES_NAME_TO_RES_VAL).map((key) => (
|
|
|
|
<div
|
2022-12-06 13:58:49 -08:00
|
|
|
className={clsx(
|
|
|
|
'cursor-pointer py-1 px-2',
|
|
|
|
resolution === RES_NAME_TO_RES_VAL[key] && 'text-th-active'
|
|
|
|
)}
|
2022-12-05 09:19:59 -08:00
|
|
|
key={key}
|
2022-12-06 08:41:57 -08:00
|
|
|
onClick={() => setResolution(RES_NAME_TO_RES_VAL[key])}
|
2022-12-05 09:19:59 -08:00
|
|
|
>
|
|
|
|
{key}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
<div
|
|
|
|
className="cursor-pointer py-1 px-2 "
|
|
|
|
onClick={() => setIsTechnicalModalOpen(true)}
|
|
|
|
>
|
|
|
|
Indicator
|
|
|
|
</div>
|
2022-12-06 08:41:57 -08:00
|
|
|
{setIsFullView && (
|
|
|
|
<div className="cursor-pointer py-1 px-2">
|
|
|
|
<ArrowsPointingOutIcon
|
|
|
|
onClick={() => setIsFullView(!isFullView)}
|
|
|
|
className="w-5"
|
|
|
|
></ArrowsPointingOutIcon>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-12-05 15:54:47 -08:00
|
|
|
<div className="py-1 px-2">
|
2022-12-06 08:41:57 -08:00
|
|
|
{isLoading && <Loading className="w-4"></Loading>}
|
2022-12-05 15:54:47 -08:00
|
|
|
</div>
|
2022-12-05 09:19:59 -08:00
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
style={{ height: 'calc(100% - 30px)', width: '100%' }}
|
|
|
|
id="update-k-line"
|
|
|
|
className="k-line-chart"
|
|
|
|
/>
|
|
|
|
<Modal
|
|
|
|
isOpen={isTechnicalModalOpen}
|
|
|
|
onClose={() => setIsTechnicalModalOpen(false)}
|
|
|
|
>
|
2022-12-21 14:33:26 -08:00
|
|
|
<div className="hide-scroll flex max-h-96 flex-col overflow-auto text-left">
|
2022-12-05 15:54:47 -08:00
|
|
|
<h2 className="py-4">Main Indicator</h2>
|
2022-12-05 09:19:59 -08:00
|
|
|
{mainTechnicalIndicatorTypes.map((type) => {
|
|
|
|
return (
|
|
|
|
<IndicatorSwitch
|
|
|
|
key={type}
|
|
|
|
type={type}
|
2022-12-05 15:54:47 -08:00
|
|
|
checked={!!mainTechnicalIndicators.find((x) => x === type)}
|
|
|
|
onChange={(check) => {
|
|
|
|
if (check) {
|
|
|
|
chart?.createTechnicalIndicator(type, true, {
|
|
|
|
id: MAIN_INDICATOR_CLASS,
|
|
|
|
})
|
|
|
|
setMainTechnicalIndicators([
|
|
|
|
...mainTechnicalIndicators,
|
|
|
|
type,
|
|
|
|
])
|
|
|
|
} else {
|
|
|
|
chart?.removeTechnicalIndicator(MAIN_INDICATOR_CLASS, type)
|
|
|
|
setMainTechnicalIndicators([
|
|
|
|
...mainTechnicalIndicators.filter((x) => x !== type),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}}
|
2022-12-05 09:19:59 -08:00
|
|
|
></IndicatorSwitch>
|
|
|
|
)
|
|
|
|
})}
|
2022-12-05 15:54:47 -08:00
|
|
|
<h2 className="py-4">Sub Indicator</h2>
|
2022-12-05 09:19:59 -08:00
|
|
|
{subTechnicalIndicatorTypes.map((type) => {
|
|
|
|
return (
|
|
|
|
<IndicatorSwitch
|
|
|
|
key={type}
|
|
|
|
type={type}
|
2022-12-05 15:54:47 -08:00
|
|
|
checked={
|
|
|
|
!!Object.keys(subTechnicalIndicators).find((x) => x === type)
|
|
|
|
}
|
|
|
|
onChange={(check) => {
|
|
|
|
if (check) {
|
|
|
|
const subId = chart?.createTechnicalIndicator(type, true)
|
|
|
|
setSubTechnicalIndicators({
|
|
|
|
...subTechnicalIndicators,
|
|
|
|
[type]: subId!,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
chart?.removeTechnicalIndicator(
|
|
|
|
subTechnicalIndicators[type],
|
|
|
|
type
|
|
|
|
)
|
|
|
|
const newItems = { ...subTechnicalIndicators }
|
|
|
|
delete newItems[type] // or whichever key you want
|
|
|
|
setSubTechnicalIndicators(newItems)
|
|
|
|
}
|
|
|
|
}}
|
2022-12-05 09:19:59 -08:00
|
|
|
></IndicatorSwitch>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</Modal>
|
2022-12-05 15:54:47 -08:00
|
|
|
</div>
|
2022-12-05 09:19:59 -08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const IndicatorSwitch = ({
|
|
|
|
type,
|
2022-12-05 15:54:47 -08:00
|
|
|
onChange,
|
|
|
|
checked,
|
2022-12-05 09:19:59 -08:00
|
|
|
}: {
|
|
|
|
type: string
|
2022-12-05 15:54:47 -08:00
|
|
|
onChange: (checked: boolean) => void
|
|
|
|
checked: boolean
|
2022-12-05 09:19:59 -08:00
|
|
|
}) => {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="flex justify-between border-t border-th-bkg-3 p-4 text-th-fgd-4"
|
|
|
|
key={type}
|
|
|
|
>
|
|
|
|
{type}
|
2022-12-05 15:54:47 -08:00
|
|
|
<Switch checked={checked} onChange={onChange} />
|
2022-12-05 09:19:59 -08:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TradingViewChartKline
|