-
Notifications
You must be signed in to change notification settings - Fork 49
/
index.ts
133 lines (117 loc) · 3.59 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { ErrorInvalidNetwork, ErrorUnexpectedResult } from '../errors'
import { toNetwork } from '../utils'
import {
Address,
DaoData,
Network,
Networkish,
OptimisticGameData,
OptimisticQueueData,
QueryResult,
} from '../types'
import * as queries from './queries'
import TheGraphWrapper from './TheGraphWrapper'
export type ConnectorTheGraphConfig = {
network: Networkish
subgraphUrl?: string
verbose?: boolean
}
function getSubgraphUrl(network: Network): string | null {
if (network.chainId === 1) {
return 'placeholder'
}
if (network.chainId === 4) {
return 'https://api.thegraph.com/subgraphs/name/0xgabi/aragon-govern-rinkeby-staging'
}
if (network.chainId === 100) {
return 'placeholder'
}
return null
}
class ConnectorTheGraph {
#gql: TheGraphWrapper
readonly config: ConnectorTheGraphConfig
readonly network: Network
constructor(config: ConnectorTheGraphConfig) {
this.config = config
this.network = toNetwork(config.network)
const orgSubgraphUrl = config.subgraphUrl || getSubgraphUrl(this.network)
if (!orgSubgraphUrl) {
throw new ErrorInvalidNetwork(
`The chainId ${this.network.chainId} is not supported ` +
`by the TheGraph connector.`
)
}
this.#gql = new TheGraphWrapper(orgSubgraphUrl, {
verbose: config.verbose,
})
}
private async fetchResult<T>(
queryAndParams:
| [any]
| [
any, // TODO: find or define the type returned by gql()
{ [key: string]: any }
],
callback: (data: { [key: string]: T }) => T,
errorMessage: string
) {
const [query, params] = queryAndParams
try {
const result = await this.#gql.performQuery(query, params)
return callback(result.data) as T
} catch (err) {
throw new ErrorUnexpectedResult(errorMessage)
}
}
async dao(address: Address): Promise<DaoData | null> {
return this.fetchResult<DaoData | null>(
[queries.DAO, { dao: address.toLowerCase() }],
(data) => data.govern ?? null,
`Unexpected result when fetching the dao ${address}.`
)
}
async daos(): Promise<DaoData[]> {
return this.fetchResult<DaoData[]>(
[queries.DAOS],
(data) => data.governs ?? [],
`Unexpected result when fetching the daos.`
)
}
async queue(address: Address): Promise<OptimisticQueueData | null> {
return this.fetchResult<OptimisticQueueData | null>(
[queries.QUEUE, { queue: address.toLowerCase() }],
(data) => data.optimisticQueue ?? null,
`Unexpected result when fetching the queue ${address}.`
)
}
async queues(): Promise<OptimisticQueueData[]> {
return this.fetchResult<OptimisticQueueData[]>(
[queries.QUEUES],
(data) => data.optimisticQueues ?? [],
`Unexpected result when fetching the queue.`
)
}
async game(name: string): Promise<OptimisticGameData | null> {
return this.fetchResult<OptimisticGameData | null>(
[queries.GAME, { name }],
(data) => data.optimisticGame ?? null,
`Unexpected result when fetching the game ${name}.`
)
}
async games(): Promise<OptimisticGameData[]> {
return this.fetchResult<OptimisticGameData[]>(
[queries.GAMES],
(data) => data.optimisticGames ?? [],
`Unexpected result when fetching the games.`
)
}
async queuesForDao(dao: Address): Promise<OptimisticGameData[]> {
return this.fetchResult<OptimisticGameData[]>(
[queries.QUEUES_BY_DAO, { dao }],
(data) => data.optimisticGame ?? [],
`Unexpected result when fetching the queues for dao ${dao}.`
)
}
}
export default ConnectorTheGraph