-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathclient.ts
341 lines (308 loc) · 8.67 KB
/
client.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { Event, StargateClient } from '@cosmjs/stargate'
import {
Comet38Client,
HttpBatchClient,
HttpBatchClientOptions,
Tendermint34Client,
Tendermint37Client,
connectComet,
} from '@cosmjs/tendermint-rpc'
import {
OmniFlix,
bitsong,
cosmos,
cosmwasm,
feemarket,
ibc,
interchain_security,
juno,
kujira,
neutron,
noble,
osmosis,
} from '@dao-dao/types/protobuf'
import { getLcdForChainId, getRpcForChainId, isSecretNetwork } from './chain'
import { retry } from './network'
import { SecretCosmWasmClient } from './secret'
type HandleConnect<T, P> = (chainId: string, ...args: P[]) => Promise<T>
type ChainClientRouterOptions<T, P> = {
/**
* The connection handler that returns the client for a given chain ID.
*/
handleConnect: HandleConnect<T, P>
}
/*
* This is a client wrapper that preserves singletons of connected clients for
* many chains.
*
* @example
* ```
* export const stargateClientRouter = new ChainClientRouter({
* handleConnect: (chainId: string) => StargateClient.connect(
* getRpcForChainId(chainId)
* ),
* })
*
* const client = await stargateClientRouter.connect(CHAIN_ID);
*
* const queryResponse = await client.queryContractSmart(...);
* ```
*/
class ChainClientRouter<T, P> {
private readonly handleConnect: HandleConnect<T, P>
private instances: Record<string, T> = {}
constructor({ handleConnect }: ChainClientRouterOptions<T, P>) {
this.handleConnect = handleConnect
}
/*
* Connect to the chain and return the client or return an existing instance
* of the client.
*/
async connect(chainId: string, ...args: P[]): Promise<T> {
if (!this.instances[chainId]) {
const instance = await this.handleConnect(chainId, ...args)
this.instances[chainId] = instance
}
return this.instances[chainId]
}
}
const httpBatchClientOptions: Partial<HttpBatchClientOptions> = {
// Some RPCs set a batch size limit of 10.
batchSizeLimit: 10,
}
/**
* Router for connecting to CosmWasmClient for the appropriate chain.
*
* Uses SecretCosmWasmClient for Secret Network mainnet and testnet.
*
* Defaults to CosmWasmClient for all other chains.
*/
export const cosmWasmClientRouter = new ChainClientRouter({
handleConnect: async (chainId: string) =>
retry(10, async (attempt) => {
const rpc = getRpcForChainId(chainId, attempt - 1)
const httpClient = new HttpBatchClient(rpc, httpBatchClientOptions)
const tmClient = await (
(
await connectComet(rpc)
).constructor as
| typeof Tendermint34Client
| typeof Tendermint37Client
| typeof Comet38Client
).create(httpClient)
return isSecretNetwork(chainId)
? await SecretCosmWasmClient.secretCreate(tmClient, {
chainId,
url: getLcdForChainId(chainId, attempt - 1),
})
: await CosmWasmClient.create(tmClient)
}),
})
/**
* Router for connecting to SecretCosmWasmClient.
*/
export const secretCosmWasmClientRouter = new ChainClientRouter({
handleConnect: async (chainId: string) =>
retry(10, async (attempt) => {
const rpc = getRpcForChainId(chainId, attempt - 1)
const httpClient = new HttpBatchClient(rpc, httpBatchClientOptions)
const tmClient = await (
(
await connectComet(rpc)
).constructor as
| typeof Tendermint34Client
| typeof Tendermint37Client
| typeof Comet38Client
).create(httpClient)
return await SecretCosmWasmClient.secretCreate(tmClient, {
chainId,
url: getLcdForChainId(chainId, attempt - 1),
})
}),
})
/*
* Router for connecting to `StargateClient`.
*/
export const stargateClientRouter = new ChainClientRouter({
handleConnect: async (chainId: string) =>
retry(10, async (attempt) => {
const rpc = getRpcForChainId(chainId, attempt - 1)
const httpClient = new HttpBatchClient(rpc)
const tmClient = await (
(
await connectComet(rpc)
).constructor as
| typeof Tendermint34Client
| typeof Tendermint37Client
| typeof Comet38Client
).create(httpClient)
return await StargateClient.create(tmClient)
}),
})
/*
* Factory function to make routers that connect to an RPC client with specific
* protobufs loaded.
*/
const makeProtoRpcClientRouter = <T, K extends keyof T>(
protoBundle: {
ClientFactory: {
createRPCQueryClient: ({
rpcEndpoint,
}: {
rpcEndpoint: string
}) => Promise<T>
}
},
key: K
): ChainClientRouter<T[K], never> =>
new ChainClientRouter({
handleConnect: async (chainId: string) =>
retry(
10,
async (attempt) =>
(
await protoBundle.ClientFactory.createRPCQueryClient({
rpcEndpoint: getRpcForChainId(chainId, attempt - 1),
})
)[key]
),
})
/*
* Router for connecting to an RPC client with Cosmos protobufs.
*/
export const cosmosProtoRpcClientRouter = makeProtoRpcClientRouter(
cosmos,
'cosmos'
)
/*
* Router for connecting to an RPC client with IBC protobufs.
*/
export const ibcProtoRpcClientRouter = makeProtoRpcClientRouter(ibc, 'ibc')
/*
* Router for connecting to an RPC client with interchain security protobufs.
*/
export const interchainSecurityProtoRpcClientRouter = makeProtoRpcClientRouter(
interchain_security,
'interchain_security'
)
/*
* Router for connecting to an RPC client with CosmWasm protobufs.
*/
export const cosmwasmProtoRpcClientRouter = makeProtoRpcClientRouter(
cosmwasm,
'cosmwasm'
)
/*
* Router for connecting to an RPC client with Osmosis protobufs.
*/
export const osmosisProtoRpcClientRouter = makeProtoRpcClientRouter(
osmosis,
'osmosis'
)
/*
* Router for connecting to an RPC client with Noble protobufs.
*/
export const nobleProtoRpcClientRouter = makeProtoRpcClientRouter(
noble,
'noble'
)
/*
* Router for connecting to an RPC client with Neutron protobufs.
*/
export const neutronProtoRpcClientRouter = makeProtoRpcClientRouter(
neutron,
'neutron'
)
/*
* Router for connecting to an RPC client with Juno protobufs.
*/
export const junoProtoRpcClientRouter = makeProtoRpcClientRouter(juno, 'juno')
/*
* Router for connecting to an RPC client with Kujira protobufs.
*/
export const kujiraProtoRpcClientRouter = makeProtoRpcClientRouter(
kujira,
'kujira'
)
/*
* Router for connecting to an RPC client with OmniFlix protobufs.
*/
export const omniflixProtoRpcClientRouter = makeProtoRpcClientRouter(
OmniFlix,
'OmniFlix'
)
/*
* Router for connecting to an RPC client with feemarket protobufs.
*/
export const feemarketProtoRpcClientRouter = makeProtoRpcClientRouter(
feemarket,
'feemarket'
)
/*
* Router for connecting to an RPC client with BitSong protobufs.
*/
export const bitsongProtoRpcClientRouter = new ChainClientRouter({
handleConnect: async (chainId: string) =>
retry(
10,
async (attempt) =>
(
await bitsong.ClientFactory.createRPCQueryClient({
rpcEndpoint: getRpcForChainId(chainId, attempt - 1),
})
).bitsong
),
})
/**
* Get CosmWasmClient for the appropriate chain.
*
* Uses SecretCosmWasmClient for Secret Network mainnet and testnet.
*
* Defaults to CosmWasmClient for all other chains.
*/
export const getCosmWasmClientForChainId = async (
chainId: string
): Promise<CosmWasmClient> =>
isSecretNetwork(chainId)
? await secretCosmWasmClientRouter.connect(chainId)
: await cosmWasmClientRouter.connect(chainId)
/**
* In response events from a transaction with a wasm event, gets the attribute
* key for a given contract address.
*/
export const findWasmAttributeValue = (
chainId: string,
events: readonly Event[],
contractAddress: string,
attributeKey: string
): string | undefined => {
const wasmEvent = events.find(
({ type, attributes }) =>
type === 'wasm' &&
attributes.some(
({ key, value }) =>
key ===
(isSecretNetwork(chainId)
? 'contract_address'
: '_contract_address') && value === contractAddress
) &&
attributes.some(({ key }) => key === attributeKey)
)
return wasmEvent?.attributes.find(({ key }) => key === attributeKey)!.value
}
/**
* In response events from a transaction, gets the first attribute value for an
* attribute key in the first event of a given type.
*/
export const findEventsAttributeValue = (
events: readonly Event[],
eventType: string,
attributeKey: string
): string | undefined =>
events.flatMap(
({ type, attributes }) =>
(type === eventType &&
attributes.find(({ key }) => key === attributeKey)) ||
[]
)[0]?.value