Repository navigation
Expand file tree
/
Copy pathchainMetadataTypes.ts
More file actions
528 lines (471 loc) · 14.5 KB
/
Copy pathchainMetadataTypes.ts
File metadata and controls
528 lines (471 loc) · 14.5 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/**
* 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 { z } from 'zod';
import { ProtocolType, objMerge } from '@hyperlane-xyz/utils';
import type { ChainMap } from '../types.js';
import {
ZChainName,
ZNzUint,
ZUint,
forwardCompatibleEnum,
} from './customZodTypes.js';
export enum EthJsonRpcBlockParameterTag {
Earliest = 'earliest',
Latest = 'latest',
Safe = 'safe',
Finalized = 'finalized',
Pending = 'pending',
}
export enum ExplorerFamily {
Etherscan = 'etherscan',
Blockscout = 'blockscout',
Routescan = 'routescan',
Voyager = 'voyager',
ZkSync = 'zksync',
RadixDashboard = 'radixdashboard',
TronScan = 'tronscan',
Other = 'other',
Unknown = 'unknown',
}
export enum ChainTechnicalStack {
ArbitrumNitro = 'arbitrumnitro',
OpStack = 'opstack',
PolygonCDK = 'polygoncdk',
PolkadotSubstrate = 'polkadotsubstrate',
Seismic = 'seismic',
ZkSync = 'zksync',
Other = 'other',
Unknown = 'unknown',
}
export enum ChainStatus {
Live = 'live',
Disabled = 'disabled',
}
export enum ChainDisabledReason {
// chain is having issues with the RPC url
BadRpc = 'badrpc',
// chain is not being used anymore
Deprecated = 'deprecated',
// chain is not public or launched yet
Private = 'private',
// chain is not available due to upgrades or maintenance
Unavailable = 'unavailable',
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.',
),
public: z
.boolean()
.optional()
.describe('Flag if the RPC is publicly available.'),
});
export type RpcUrl = z.infer<typeof RpcUrlSchema>;
export const BlockExplorerSchema = z.object({
name: z.string().describe('A human readable name for the explorer.'),
url: z.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: forwardCompatibleEnum(ExplorerFamily, ExplorerFamily.Unknown)
.optional()
.describe(
'The type of the block explorer. See ExplorerFamily for valid values.',
),
});
export type BlockExplorer = z.infer<typeof BlockExplorerSchema>;
export const NativeTokenSchema = z.object({
name: z.string(),
symbol: z.string(),
decimals: ZUint.lt(256),
denom: z.string().optional(),
});
export const GasPriceSchema = z.object({
denom: z.string(),
amount: z.string(),
});
export const DisabledChainSchema = z.object({
status: z
.literal(ChainStatus.Disabled)
.describe(
'The status that represents the chain availability. See ChainStatus for valid values.',
),
reasons: z
.array(z.enum(ChainDisabledReason))
.min(1)
.describe('List of reasons explaining why the chain is disabled.'),
});
export const EnabledChainSchema = z.object({
status: z
.literal(ChainStatus.Live)
.describe(
'The status that represents the chain availability. See ChainStatus for valid values.',
),
});
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({
maxSupportedTransactionVersion: z
.union([z.literal(0), z.literal(1)])
.optional()
.describe('Highest supported SVM transaction version; defaults to 0.'),
sealevelV1TransactionsEnabled: z
.boolean()
.optional()
.describe(
'Enable v1 submission only after this SVM activates the feature; defaults to false.',
),
sealevelTransactionVersion: z
.union([z.literal(0), z.literal(1)])
.optional()
.describe(
'Default Sealevel signer transaction version; defaults to 0. V1 requires sealevelV1TransactionsEnabled and maxSupportedTransactionVersion 1.',
),
availability: z
.union([DisabledChainSchema, EnabledChainSchema])
.optional()
.describe(
'Specifies if the chain is available and the reasons why it is disabled.',
),
bech32Prefix: z
.string()
.optional()
.describe('The human readable address prefix for the chains using bech32.'),
blockExplorers: z
.array(BlockExplorerSchema)
.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: z
.union([ZUint, z.string()])
.optional()
.describe(
'Number of blocks before a transaction has a near-zero chance of reverting or block tag.',
),
estimateBlockTime: z
.number()
.positive()
.finite()
.optional()
.describe('Rough estimate of time per block in seconds.'),
})
.optional()
.describe('Block settings for the chain/deployment.'),
bypassBatchSimulation: z
.boolean()
.optional()
.describe('Whether to bypass batch simulation for this chain.'),
chainId: z
.union([ZUint, z.string()])
.describe(`The chainId of the chain. Uses EIP-155 for EVM chains`),
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.',
),
deployer: z
.object({
name: z.string().describe('The name of the deployer.'),
email: z
.string()
.email()
.optional()
.describe('The email address of the deployer.'),
url: z.url().optional().describe('The URL of the deployer.'),
})
.optional()
.describe(
'Identity information of the deployer of a Hyperlane instance to this chain',
),
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.',
),
domainId: ZNzUint.describe(
'The domainId of the chain, should generally default to `chainId`. Consumer of `ChainMetadata` should use this value or `name` as a unique identifier.',
),
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.'),
gnosisSafeApiKey: z
.string()
.optional()
.describe('The API key for the gnosis safe transaction service.'),
gatewayUrls: z
.array(RpcUrlSchema)
.describe('For Radix chains, a list of Gateway API URLs.')
.optional(),
grpcUrls: z
.array(RpcUrlSchema)
.describe('For cosmos chains only, a list of gRPC API URLs')
.optional(),
index: z
.object({
from: z
.number()
.optional()
.describe('The block to start any indexing from.'),
})
.optional()
.describe('Indexing settings for the chain.'),
isTestnet: z
.boolean()
.optional()
.describe('Whether the chain is considered a testnet or a mainnet.'),
logoURI: z
.string()
.optional()
.describe(
'A URI to a logo image for this chain for use in user interfaces.',
),
name: ZChainName.describe(
'The unique string identifier of the chain, used as the key in ChainMap dictionaries.',
),
nativeToken: NativeTokenSchema.optional().describe(
'The metadata of the native token of the chain (e.g. ETH for Ethereum).',
),
protocol: forwardCompatibleEnum(ProtocolType, ProtocolType.Unknown).describe(
'The type of protocol used by this chain. See ProtocolType for valid values.',
),
restUrls: z
.array(RpcUrlSchema)
.describe('For cosmos chains only, a list of Rest API URLs')
.optional(),
rpcUrls: z
.array(RpcUrlSchema)
.min(1)
.describe('The list of RPC endpoints for interacting with the chain.'),
slip44: z.number().optional().describe('The SLIP-0044 coin type.'),
technicalStack: forwardCompatibleEnum(
ChainTechnicalStack,
ChainTechnicalStack.Unknown,
)
.optional()
.describe(
'The technical stack of the chain. See ChainTechnicalStack for valid values.',
),
transactionOverrides: z
.record(z.string(), z.unknown())
.optional()
.describe('Properties to include when forming transaction requests.'),
gasPrice: GasPriceSchema.optional().describe(
'The gas price of Cosmos chains.',
),
});
// Passthrough allows for extra fields to remain in the object (such as extensions consumers may want like `mailbox`)
const ChainMetadataSchemaExtensible = z.looseObject(
ChainMetadataSchemaObject.shape,
);
// Add refinements to the object schema to conditionally validate certain fields
export const ChainMetadataSchema = ChainMetadataSchemaExtensible.refine(
(metadata) => {
if (
[ProtocolType.Ethereum, ProtocolType.Sealevel].includes(
metadata.protocol,
) &&
typeof metadata.chainId !== 'number'
)
return false;
else if (
(metadata.protocol === ProtocolType.Cosmos ||
metadata.protocol === ProtocolType.CosmosNative) &&
typeof metadata.chainId !== 'string'
)
return false;
else return true;
},
{ message: 'Invalid Chain Id', path: ['chainId'] },
)
.refine(
(metadata) =>
!metadata.sealevelV1TransactionsEnabled ||
metadata.maxSupportedTransactionVersion === 1,
{
message: 'V1 sending requires maxSupportedTransactionVersion 1',
path: ['maxSupportedTransactionVersion'],
},
)
.refine(
(metadata) =>
metadata.sealevelTransactionVersion !== 1 ||
metadata.sealevelV1TransactionsEnabled === true,
{
message: 'V1 default requires sealevelV1TransactionsEnabled',
path: ['sealevelTransactionVersion'],
},
)
.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.protocol === ProtocolType.CosmosNative) &&
(!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.protocol === ProtocolType.CosmosNative) &&
(!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.protocol === ProtocolType.CosmosNative) &&
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 ChainMetadataSchemaObject
> &
Ext;
const CompiledChainMetadataSchema = z.compile(ChainMetadataSchema);
export function safeParseChainMetadata(
c: ChainMetadata,
): z.ZodSafeParseResult<ChainMetadata> {
return CompiledChainMetadataSchema.safeParse(c);
}
export function isValidChainMetadata(c: ChainMetadata): boolean {
return z.validate(CompiledChainMetadataSchema, c);
}
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): string | number {
if (chainMetadata.blocks?.reorgPeriod !== undefined)
return chainMetadata.blocks.reorgPeriod;
else throw new Error('Chain has no reorg period');
}
export function mergeChainMetadata(
base: ChainMetadata,
overrides: Partial<ChainMetadata> | undefined,
): ChainMetadata {
return objMerge<ChainMetadata>(base, overrides || {}, 10, true);
}
export function mergeChainMetadataMap(
base: ChainMap<ChainMetadata>,
overrides: ChainMap<Partial<ChainMetadata> | undefined> | undefined,
): ChainMap<ChainMetadata> {
return objMerge<ChainMap<ChainMetadata>>(base, overrides || {}, 10, true);
}