/
chainMetadataTypes.ts
334 lines (316 loc) · 9.67 KB
/
chainMetadataTypes.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
/**
* The types defined here are the source of truth for chain metadata.
* ANY CHANGES HERE NEED TO BE REFLECTED IN HYPERLANE-BASE CONFIG PARSING.
*/
import { SafeParseReturnType, z } from 'zod';
import { ProtocolType } from '@hyperlane-xyz/utils';
import { ZChainName, ZNzUint, ZUint } from './customZodTypes.js';
export enum ExplorerFamily {
Etherscan = 'etherscan',
Blockscout = 'blockscout',
Routescan = 'routescan',
Other = 'other',
}
export enum ChainTechnicalStack {
ArbitrumNitro = 'arbitrumnitro',
Other = 'other',
}
// A type that also allows for literal values of the enum
export type ExplorerFamilyValue = `${ExplorerFamily}`;
export const RpcUrlSchema = z.object({
http: z
.string()
.url()
.describe('The HTTP URL of the RPC endpoint (preferably HTTPS).'),
concurrency: z
.number()
.int()
.positive()
.optional()
.describe('Maximum number of concurrent RPC requests.'),
webSocket: z
.string()
.optional()
.describe('The WSS URL if the endpoint also supports websockets.'),
pagination: z
.object({
maxBlockRange: ZNzUint.optional().describe(
'The maximum range between block numbers for which the RPC can query data',
),
minBlockNumber: ZUint.optional().describe(
'The absolute minimum block number that this RPC supports.',
),
maxBlockAge: ZNzUint.optional().describe(
'The relative different from latest block that this RPC supports.',
),
})
.optional()
.describe('Limitations on the block range/age that can be queried.'),
retry: z
.object({
maxRequests: ZNzUint.describe(
'The maximum number of requests to attempt before failing.',
),
baseRetryMs: ZNzUint.describe('The base retry delay in milliseconds.'),
})
.optional()
.describe(
'Default retry settings to be used by a provider such as MultiProvider.',
),
});
export type RpcUrl = z.infer<typeof RpcUrlSchema>;
export const NativeTokenSchema = z.object({
name: z.string(),
symbol: z.string(),
decimals: ZUint.lt(256),
denom: z.string().optional(),
});
export type NativeToken = z.infer<typeof NativeTokenSchema>;
/**
* A collection of useful properties and settings for chains using Hyperlane
* Specified as a Zod schema
*/
export const ChainMetadataSchemaObject = z.object({
name: ZChainName.describe(
'The unique string identifier of the chain, used as the key in ChainMap dictionaries.',
),
protocol: z
.nativeEnum(ProtocolType)
.describe(
'The type of protocol used by this chain. See ProtocolType for valid values.',
),
chainId: z
.union([ZNzUint, z.string()])
.describe(`The chainId of the chain. Uses EIP-155 for EVM chains`),
domainId: ZNzUint.optional().describe(
'The domainId of the chain, should generally default to `chainId`. Consumer of `ChainMetadata` should use this value if present, but otherwise fallback to `chainId`.',
),
displayName: z
.string()
.optional()
.describe('Human-readable name of the chain.'),
displayNameShort: z
.string()
.optional()
.describe(
'A shorter human-readable name of the chain for use in user interfaces.',
),
technicalStack: z
.nativeEnum(ChainTechnicalStack)
.optional()
.describe(
'The technical stack of the chain. See ChainTechnicalStack for valid values.',
),
logoURI: z
.string()
.optional()
.describe(
'A URI to a logo image for this chain for use in user interfaces.',
),
nativeToken: NativeTokenSchema.optional().describe(
'The metadata of the native token of the chain (e.g. ETH for Ethereum).',
),
rpcUrls: z
.array(RpcUrlSchema)
.nonempty()
.describe('The list of RPC endpoints for interacting with the chain.'),
restUrls: z
.array(RpcUrlSchema)
.describe('For cosmos chains only, a list of Rest API URLs')
.optional(),
grpcUrls: z
.array(RpcUrlSchema)
.describe('For cosmos chains only, a list of gRPC API URLs')
.optional(),
customGrpcUrls: z
.string()
.optional()
.describe(
'Specify a comma separated list of custom GRPC URLs to use for this chain. If not specified, the default GRPC urls will be used.',
),
blockExplorers: z
.array(
z.object({
name: z.string().describe('A human readable name for the explorer.'),
url: z.string().url().describe('The base URL for the explorer.'),
apiUrl: z
.string()
.url()
.describe('The base URL for requests to the explorer API.'),
apiKey: z
.string()
.optional()
.describe(
'An API key for the explorer (recommended for better reliability).',
),
family: z
.nativeEnum(ExplorerFamily)
.optional()
.describe(
'The type of the block explorer. See ExplorerFamily for valid values.',
),
}),
)
.optional()
.describe('A list of block explorers with data for this chain'),
blocks: z
.object({
confirmations: ZUint.describe(
'Number of blocks to wait before considering a transaction confirmed.',
),
reorgPeriod: ZUint.optional().describe(
'Number of blocks before a transaction has a near-zero chance of reverting.',
),
estimateBlockTime: z
.number()
.positive()
.finite()
.optional()
.describe('Rough estimate of time per block in seconds.'),
})
.optional()
.describe('Block settings for the chain/deployment.'),
transactionOverrides: z
.record(z.any())
.optional()
.describe('Properties to include when forming transaction requests.'),
gasCurrencyCoinGeckoId: z
.string()
.optional()
.describe('The ID on CoinGecko of the token used for gas payments.'),
gnosisSafeTransactionServiceUrl: z
.string()
.optional()
.describe('The URL of the gnosis safe transaction service.'),
bech32Prefix: z
.string()
.optional()
.describe('The human readable address prefix for the chains using bech32.'),
slip44: z.number().optional().describe('The SLIP-0044 coin type.'),
isTestnet: z
.boolean()
.optional()
.describe('Whether the chain is considered a testnet or a mainnet.'),
index: z
.object({
from: z
.number()
.optional()
.describe('The block to start any indexing from.'),
})
.optional()
.describe('Indexing settings for the chain.'),
});
// Add refinements to the object schema to conditionally validate certain fields
export const ChainMetadataSchema = ChainMetadataSchemaObject.refine(
(metadata) => {
if (
[ProtocolType.Ethereum, ProtocolType.Sealevel].includes(
metadata.protocol,
) &&
typeof metadata.chainId !== 'number'
)
return false;
else if (
metadata.protocol === ProtocolType.Cosmos &&
typeof metadata.chainId !== 'string'
)
return false;
else return true;
},
{ message: 'Invalid Chain Id', path: ['chainId'] },
)
.refine(
(metadata) => {
if (typeof metadata.chainId === 'string' && !metadata.domainId)
return false;
else return true;
},
{ message: 'Domain Id required', path: ['domainId'] },
)
.refine(
(metadata) => {
if (
metadata.protocol === ProtocolType.Cosmos &&
(!metadata.bech32Prefix || !metadata.slip44)
)
return false;
else return true;
},
{
message: 'Bech32Prefix and Slip44 required for Cosmos chains',
path: ['bech32Prefix', 'slip44'],
},
)
.refine(
(metadata) => {
if (
metadata.protocol === ProtocolType.Cosmos &&
(!metadata.restUrls || !metadata.grpcUrls)
)
return false;
else return true;
},
{
message: 'Rest and gRPC URLs required for Cosmos chains',
path: ['restUrls', 'grpcUrls'],
},
)
.refine(
(metadata) => {
if (
metadata.protocol === ProtocolType.Cosmos &&
metadata.nativeToken &&
!metadata.nativeToken.denom
)
return false;
else return true;
},
{
message: 'Denom values are required for Cosmos native tokens',
path: ['nativeToken', 'denom'],
},
)
.refine(
(metadata) => {
if (
metadata.technicalStack === ChainTechnicalStack.ArbitrumNitro &&
metadata.index?.from === undefined
) {
return false;
} else return true;
},
{
message: 'An index.from value is required for Arbitrum Nitro chains',
path: ['index', 'from'],
},
);
export type ChainMetadata<Ext = object> = z.infer<typeof ChainMetadataSchema> &
Ext;
export type BlockExplorer = Exclude<
ChainMetadata['blockExplorers'],
undefined
>[number];
export function safeParseChainMetadata(
c: ChainMetadata,
): SafeParseReturnType<ChainMetadata, ChainMetadata> {
return ChainMetadataSchema.safeParse(c);
}
export function isValidChainMetadata(c: ChainMetadata): boolean {
return ChainMetadataSchema.safeParse(c).success;
}
export function getDomainId(chainMetadata: ChainMetadata): number {
if (chainMetadata.domainId) return chainMetadata.domainId;
else if (typeof chainMetadata.chainId === 'number')
return chainMetadata.chainId;
else throw new Error('Invalid chain metadata, no valid domainId');
}
export function getChainIdNumber(chainMetadata: ChainMetadata): number {
if (typeof chainMetadata.chainId === 'number') return chainMetadata.chainId;
else throw new Error('ChainId is not a number, chain may be of Cosmos type');
}
export function getReorgPeriod(chainMetadata: ChainMetadata): number {
if (chainMetadata.blocks?.reorgPeriod !== undefined)
return chainMetadata.blocks.reorgPeriod;
else throw new Error('Chain has no reorg period');
}