This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmarket-service-manager.ts
60 lines (56 loc) · 1.93 KB
/
market-service-manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import {
HistoryData,
MarketCapResult,
MarketData,
MarketDataArgs,
PriceHistoryArgs,
PriceHistoryType
} from '@shapeshiftoss/types'
import { FindAllMarketArgs } from '@shapeshiftoss/types/src'
import { MarketProviders } from './market-providers'
export const findAll = async (args?: FindAllMarketArgs): Promise<MarketCapResult> => {
let result: MarketCapResult | null = null
// Go through market providers listed above and look for market data for all assets.
// Once data is found, exit the loop and return result. If no data is found for any
// provider, throw an error.
for (let i = 0; i < MarketProviders.length && !result; i++) {
try {
result = await MarketProviders[i].findAll(args)
} catch (e) {
console.info(e)
}
}
if (!result) throw new Error('Cannot find market service provider for market data.')
return result
}
export const findByCaip19 = async ({ caip19 }: MarketDataArgs) => {
let result: MarketData | null = null
// Loop through market providers and look for asset market data. Once found, exit loop.
for (let i = 0; i < MarketProviders.length && !result; i++) {
try {
result = await MarketProviders[i].findByCaip19({ caip19 })
} catch (e) {
// Swallow error, not every asset will be with every provider.
continue
}
}
if (!result) return null
return result
}
export const findPriceHistoryByCaip19: PriceHistoryType = async ({
caip19,
timeframe
}: PriceHistoryArgs): Promise<HistoryData[]> => {
let result: HistoryData[] | null = null
// Loop through market providers and look for asset price history data. Once found, exit loop.
for (let i = 0; i < MarketProviders.length && !result?.length; i++) {
try {
result = await MarketProviders[i].findPriceHistoryByCaip19({ caip19, timeframe })
} catch (e) {
// Swallow error, not every asset will be with every provider.
continue
}
}
if (!result) return []
return result
}