mango-v4-ui/apis/birdeye/helpers.ts

65 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-01-11 10:27:35 -08:00
export const NEXT_PUBLIC_BIRDEYE_API_KEY =
process.env.NEXT_PUBLIC_BIRDEYE_API_KEY ||
2023-02-08 03:34:46 -08:00
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2NzM0NTE4MDF9.KTEqB1hrmZTMzk19rZNx9aesh2bIHj98Cb8sg5Ikz-Y'
2022-12-15 15:33:31 -08:00
2022-12-21 03:21:04 -08:00
export const API_URL = 'https://public-api.birdeye.so/'
export const socketUrl = `wss://public-api.birdeye.so/socket?x-api-key=${NEXT_PUBLIC_BIRDEYE_API_KEY}`
2022-12-15 15:33:31 -08:00
// Make requests to CryptoCompare API
export async function makeApiRequest(path: string) {
try {
2022-12-21 03:21:04 -08:00
const response = await fetch(`${API_URL}${path}`, {
2022-12-15 15:33:31 -08:00
headers: {
2023-01-11 10:27:35 -08:00
'X-API-KEY': NEXT_PUBLIC_BIRDEYE_API_KEY,
2022-12-15 15:33:31 -08:00
},
})
return response.json()
} catch (error: any) {
throw new Error(`CryptoCompare request error: ${error.status}`)
}
}
const RESOLUTION_MAPPING: Record<string, string> = {
'1': '1m',
'3': '3m',
'5': '5m',
'15': '15m',
'30': '30m',
'60': '1H',
'120': '2H',
'240': '4H',
'1D': '1D',
'1W': '1W',
}
export function parseResolution(resolution: string) {
if (!resolution || !RESOLUTION_MAPPING[resolution])
return RESOLUTION_MAPPING[0]
return RESOLUTION_MAPPING[resolution]
}
export function getNextBarTime(lastBar: any, resolution = '1D') {
if (!lastBar) return
const lastCharacter = resolution.slice(-1)
let nextBarTime
switch (true) {
case lastCharacter === 'W':
nextBarTime = 7 * 24 * 60 * 60 * 1000 + lastBar.time
break
case lastCharacter === 'D':
nextBarTime = 1 * 24 * 60 * 60 * 1000 + lastBar.time
break
default:
nextBarTime = 1 * 60 * 1000 + lastBar.time
break
}
return nextBarTime
}