seperate query

This commit is contained in:
Adrian Brzeziński 2023-01-02 20:17:27 +01:00
parent 173bf8b716
commit b695e69bd0
1 changed files with 49 additions and 32 deletions

View File

@ -47,6 +47,52 @@ const configurationData = {
// return data.data.tokens // return data.data.tokens
// } // }
export const queryBars = async (
tokenAddress: string,
resolution: string,
periodParams: {
countBack: number
firstDataRequest: boolean
from: number
to: number
}
): Promise<Bar[]> => {
const { from, to } = periodParams
const urlParameters = {
address: tokenAddress,
type: parseResolution(resolution),
time_from: from,
time_to: to,
}
const query = Object.keys(urlParameters)
.map(
(name: string) =>
`${name}=${encodeURIComponent((urlParameters as any)[name])}`
)
.join('&')
const data = await makeApiRequest(`defi/ohlcv/pair?${query}`)
if (!data.success || data.data.items.length === 0) {
return []
}
let bars: Bar[] = []
for (const bar of data.data.items) {
if (bar.unixTime >= from && bar.unixTime < to) {
bars = [
...bars,
{
time: bar.unixTime * 1000,
low: bar.l,
high: bar.h,
open: bar.o,
close: bar.c,
},
]
}
}
return bars
}
export default { export default {
onReady: (callback: (configuration: DatafeedConfiguration) => void) => { onReady: (callback: (configuration: DatafeedConfiguration) => void) => {
console.log('[onReady]: Method call') console.log('[onReady]: Method call')
@ -119,7 +165,6 @@ export default {
console.log('[resolveSymbol]: Symbol resolved', symbolAddress) console.log('[resolveSymbol]: Symbol resolved', symbolAddress)
onSymbolResolvedCallback(symbolInfo) onSymbolResolvedCallback(symbolInfo)
}, },
getBars: async ( getBars: async (
symbolInfo: SymbolInfo, symbolInfo: SymbolInfo,
resolution: string, resolution: string,
@ -137,44 +182,16 @@ export default {
) => void, ) => void,
onErrorCallback: (e: any) => void onErrorCallback: (e: any) => void
) => { ) => {
const { from, to, firstDataRequest } = periodParams
console.log('[getBars]: Method call', symbolInfo, resolution, from, to)
const urlParameters = {
address: symbolInfo.address,
type: parseResolution(resolution),
time_from: from,
time_to: to,
}
const query = Object.keys(urlParameters)
.map(
(name: string) =>
`${name}=${encodeURIComponent((urlParameters as any)[name])}`
)
.join('&')
try { try {
const data = await makeApiRequest(`defi/ohlcv/pair?${query}`) const { firstDataRequest } = periodParams
if (!data.success || data.data.items.length === 0) { const bars = await queryBars(symbolInfo.address, resolution, periodParams)
if (!bars || bars.length === 0) {
// "noData" should be set if there is no data in the requested period. // "noData" should be set if there is no data in the requested period.
onHistoryCallback([], { onHistoryCallback([], {
noData: true, noData: true,
}) })
return return
} }
let bars: Bar[] = []
data.data.items.forEach((bar: any) => {
if (bar.unixTime >= from && bar.unixTime < to) {
bars = [
...bars,
{
time: bar.unixTime * 1000,
low: bar.l,
high: bar.h,
open: bar.o,
close: bar.c,
},
]
}
})
if (firstDataRequest) { if (firstDataRequest) {
lastBarsCache.set(symbolInfo.address, { lastBarsCache.set(symbolInfo.address, {