-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathentity.ts
248 lines (238 loc) · 6.03 KB
/
entity.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import { QueryClient, queryOptions, skipToken } from '@tanstack/react-query'
import {
accountQueries,
chainQueries,
contractQueries,
cw1WhitelistExtraQueries,
polytoneQueries,
profileQueries,
} from '@dao-dao/state/query'
import { Entity, EntityType } from '@dao-dao/types'
import {
getChainForChainId,
getFallbackImage,
getImageUrlForChainId,
isValidWalletAddress,
} from '@dao-dao/utils'
import { daoQueries } from './dao'
/**
* Fetch entity information for any address on any chain.
*/
export const fetchEntityInfo = async (
queryClient: QueryClient,
{
chainId,
address,
ignoreEntities,
}: {
chainId: string
address: string
/**
* Prevent infinite loop if cw1-whitelist nests itself.
*/
ignoreEntities?: string[]
}
): Promise<Entity> => {
const { bech32Prefix } = getChainForChainId(chainId)
// Check if address is module account.
const moduleName = await queryClient
.fetchQuery(
chainQueries.moduleName({
chainId,
address,
})
)
.catch(() => null)
if (moduleName) {
const entity: Entity = {
type: EntityType.Module,
chainId,
address,
name: moduleName,
imageUrl: getImageUrlForChainId(chainId),
}
return entity
}
const [
daoInfo,
entityFromPolytoneProxy,
walletProfile,
cw1WhitelistAdminEntities,
cryptographicMultisigEntities,
] = await Promise.all([
// Attempt to load DAO.
queryClient
.fetchQuery(
contractQueries.isDao(queryClient, {
chainId,
address,
})
)
.then((isDao) =>
isDao
? queryClient.fetchQuery(
daoQueries.info(queryClient, {
chainId,
coreAddress: address,
})
)
: undefined
)
.catch(() => undefined),
// Attempt to load polytone proxy.
queryClient
.fetchQuery(
contractQueries.isPolytoneProxy(queryClient, {
chainId,
address,
})
)
.then(async (isPolytoneProxy): Promise<Entity | undefined> => {
if (!isPolytoneProxy) {
return
}
const controller = await queryClient.fetchQuery(
polytoneQueries.reverseLookupProxy(queryClient, {
chainId,
address,
})
)
return {
...(await queryClient.fetchQuery(
entityQueries.info(queryClient, {
chainId: controller.chainId,
address: controller.remoteAddress,
})
)),
polytoneProxy: {
chainId,
address,
},
}
})
.catch(() => undefined),
// Attempt to load wallet profile.
isValidWalletAddress(address, bech32Prefix)
? queryClient.fetchQuery(
profileQueries.unified(queryClient, {
chainId,
address,
})
)
: undefined,
// Attempt to load cw1-whitelist admins.
queryClient
.fetchQuery(
cw1WhitelistExtraQueries.adminsIfCw1Whitelist(queryClient, {
chainId,
address,
})
)
.then((admins): Promise<Entity[]> | undefined => {
if (!admins) {
return
}
return Promise.all(
admins.map((admin) =>
ignoreEntities?.includes(admin)
? // Placeholder entity to prevent infinite loop.
{
chainId,
type: EntityType.Wallet,
address: admin,
name: null,
imageUrl: getFallbackImage(admin),
}
: queryClient.fetchQuery(
entityQueries.info(queryClient, {
chainId,
address: admin,
// Add address to ignore list to prevent infinite loops.
ignoreEntities: [...(ignoreEntities || []), address],
})
)
)
)
})
.catch(() => undefined),
// Attempt to load cryptographic multisig entities.
queryClient
.fetchQuery(
accountQueries.cryptographicMultisig({
chainId,
address,
})
)
.then(({ config }) =>
Promise.all(
config.members.map((member) =>
queryClient.fetchQuery(
entityQueries.info(queryClient, {
chainId,
address: member.address,
// Add address to ignore list to prevent infinite loops.
ignoreEntities: [...(ignoreEntities || []), address],
})
)
)
)
)
.catch(() => undefined),
])
if (daoInfo) {
return {
type: EntityType.Dao,
daoInfo,
chainId,
address,
name: daoInfo.name,
imageUrl: daoInfo.imageUrl || getFallbackImage(address),
}
} else if (entityFromPolytoneProxy) {
return entityFromPolytoneProxy
} else if (cw1WhitelistAdminEntities) {
return {
type: EntityType.Cw1Whitelist,
chainId,
address,
name: null,
imageUrl: getFallbackImage(address),
entities: cw1WhitelistAdminEntities,
}
} else if (cryptographicMultisigEntities) {
return {
type: EntityType.CryptographicMultisig,
chainId,
address,
name: null,
imageUrl: getFallbackImage(address),
entities: cryptographicMultisigEntities,
}
} else {
// Default to wallet.
return {
type: EntityType.Wallet,
chainId,
address,
name: walletProfile?.name || null,
imageUrl: walletProfile?.imageUrl || getFallbackImage(address),
profile: walletProfile,
}
}
}
export const entityQueries = {
/**
* Fetch entity.
*/
info: (
queryClient: QueryClient,
// If undefined, query will be disabled.
options?: Parameters<typeof fetchEntityInfo>[1]
) =>
queryOptions({
queryKey: ['entity', 'info', options],
queryFn: options
? () => fetchEntityInfo(queryClient, options)
: skipToken,
}),
}