forked from DA0-DA0/dao-dao-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.ts
108 lines (94 loc) · 2.8 KB
/
query.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { IndexerFormulaType, WithChainId } from '@dao-dao/types'
export type QueryIndexerOptions = WithChainId<
{
formula: string
args?: Record<string, any>
block?: {
height: number | string
// Most formulas do not need the time, so make it optional.
timeUnixMs?: number | string
}
times?: {
startUnixMs: number
endUnixMs?: number
stepMs?: number
}
} & (
| {
type: `${IndexerFormulaType.Generic}`
address?: never
}
| {
type: `${Exclude<IndexerFormulaType, IndexerFormulaType.Generic>}`
address: string
}
)
>
// NO INDEXER ON SECRET NETWORK
// const indexerBatchClient = new BatchClient(INDEXER_URL + '/batch')
// export const queryIndexer = async <T = any>({
// type,
// address = '_',
// formula,
// args,
// block,
// times,
// chainId,
// }: QueryIndexerOptions): Promise<T | undefined> => {
// if (!chainIsIndexed(chainId)) {
// throw new Error(CommonError.NoIndexerForChain)
// }
// // Filter out undefined args.
// if (args) {
// args = Object.entries(args).reduce((acc, [key, value]) => {
// if (value !== undefined) {
// acc[key] = value
// }
// return acc
// }, {} as Record<string, any>)
// }
// const params = new URLSearchParams({
// ...args,
// ...(block && {
// block: `${block.height}:${block.timeUnixMs ?? 1}`,
// }),
// ...(times && {
// times: `${BigInt(times.startUnixMs).toString()}..${
// times.endUnixMs ? BigInt(times.endUnixMs).toString() : ''
// }`,
// ...(times.stepMs && {
// timeStep: times.stepMs.toString(),
// }),
// }),
// })
// const url = `/${chainId}/${type}/${address}/${formula}?${params.toString()}`
// const { status, body } = await indexerBatchClient.execute({
// url,
// })
// if (status >= 300) {
// throw new Error(
// `Error querying indexer for ${type}/${address}/${formula}: ${status} ${body}`.trim()
// )
// }
// if (status === 204) {
// // If no content is returned, return undefined. This will happen if the
// // formula computed succesfully and outputted nothing (undefined or null).
// return undefined
// }
// return JSON.parse(body)
// }
// export const queryIndexerUpStatus = async ({
// chainId,
// }: WithChainId<{}>): Promise<IndexerUpStatus> => {
// if (!chainIsIndexed(chainId)) {
// throw new Error(CommonError.NoIndexerForChain)
// }
// const upResponse = await fetch([INDEXER_URL, chainId, 'up'].join('/'))
// let upStatus: IndexerUpStatus
// if (upResponse.status === 200 || upResponse.status === 412) {
// upStatus = await upResponse.json()
// } else {
// throw new Error(`Error querying indexer up status: ${upResponse.status}`)
// }
// return upStatus
// }