@@ -15,76 +15,23 @@ import {
15
15
testRelayWestend ,
16
16
} from '@polkadot/apps-config' ;
17
17
import type { EndpointOption } from '@polkadot/apps-config/endpoints/types' ;
18
- import fs from 'fs' ;
19
18
20
- type TokenRegistry = {
21
- polkadot : { } ;
22
- kusama : { } ;
23
- westend : { } ;
24
- } ;
25
-
26
- type ChainName = 'polkadot' | 'kusama' | 'westend' ;
27
-
28
- /**
29
- * Write Json to a file path.
30
- *
31
- * @param path Path to write the json file too
32
- * @param data Data that will be written to file.
33
- */
34
- const writeJson = ( path : string , data : TokenRegistry ) : void => {
35
- fs . writeFileSync ( path , JSON . stringify ( data , null , 2 ) ) ;
36
- fs . appendFileSync ( path , '\n' , 'utf-8' ) ;
37
- } ;
38
-
39
- const twirlTimer = function ( ) {
40
- const P = [ '\\' , '|' , '/' , '-' ] ;
41
- let x = 0 ;
42
- return setInterval ( function ( ) {
43
- process . stdout . write ( '\r' + P [ x ++ ] ) ;
44
- x &= 3 ;
45
- } , 250 ) ;
46
- } ;
47
-
48
- interface AssetsInfo {
49
- [ key : string ] : string ;
50
- }
51
-
52
- type ForeignAssetMetadata = {
53
- deposit : string ;
54
- name : string ;
55
- symbol : string ;
56
- decimals : string ;
57
- isFrozen : boolean ;
58
- } ;
59
-
60
- interface ForeignAssetsInfo {
61
- [ key : string ] : {
62
- symbol : string ;
63
- name : string ;
64
- multiLocation : string ;
65
- } ;
66
- }
67
-
68
- interface PoolInfo {
69
- lpToken : string ;
70
- }
71
-
72
- type PoolPairsInfo = {
73
- [ key : string ] : {
74
- lpToken : string ;
75
- pairInfo : string ;
76
- } ;
77
- } ;
78
-
79
- interface ParaIds {
80
- [ key : string ] : number [ ] ;
81
- }
19
+ import type {
20
+ AssetsInfo ,
21
+ ChainName ,
22
+ ForeignAssetMetadata ,
23
+ ForeignAssetsInfo ,
24
+ ParaIds ,
25
+ PoolInfo ,
26
+ PoolPairsInfo ,
27
+ TokenRegistry ,
28
+ } from './types' ;
29
+ import { sleep , twirlTimer , writeJson } from './util' ;
82
30
83
31
/**
84
32
* @const MAX_RETRIES Maximum amount of connection attempts
85
33
* @const WS_DISCONNECT_TIMEOUT_SECONDS time to wait between attempts, in seconds
86
34
*/
87
-
88
35
const MAX_RETRIES = 5 ;
89
36
const WS_DISCONNECT_TIMEOUT_SECONDS = 3 ;
90
37
@@ -108,7 +55,7 @@ const fetchChainInfo = async (
108
55
const foreignAssetsPallet = api . registry . metadata . pallets . filter (
109
56
( pallet ) => pallet . name . toString ( ) . toLowerCase ( ) === 'foreignassets'
110
57
) [ 0 ] ;
111
-
58
+
112
59
const { tokenSymbol } = await api . rpc . system . properties ( ) ;
113
60
const { specName } = await api . rpc . state . getRuntimeVersion ( ) ;
114
61
const tokens = tokenSymbol . isSome
@@ -143,7 +90,9 @@ const fetchChainInfo = async (
143
90
poolPairsInfo,
144
91
specName : specNameStr ,
145
92
assetsPalletInstance : assetsPallet ? assetsPallet . index . toString ( ) : null ,
146
- foreignAssetsPalletInstance : foreignAssetsPallet ? foreignAssetsPallet . index . toString ( ) : null ,
93
+ foreignAssetsPalletInstance : foreignAssetsPallet
94
+ ? foreignAssetsPallet . index . toString ( )
95
+ : null ,
147
96
} ;
148
97
} else {
149
98
return null ;
@@ -202,51 +151,11 @@ const createChainRegistryFromRelay = async (
202
151
}
203
152
} ;
204
153
205
- const main = async ( ) => {
206
- const registry = {
207
- polkadot : { } ,
208
- kusama : { } ,
209
- westend : { } ,
210
- } ;
211
-
212
- const paraIds : ParaIds = { } ;
213
-
214
- const polkadotEndpoints = [ prodParasPolkadot , prodParasPolkadotCommon ] ;
215
- const kusamaEndpoints = [ prodParasKusama , prodParasKusamaCommon ] ;
216
- const westendEndpoints = [ testParasWestend , testParasWestendCommon ] ;
217
-
218
- // Set the Parachains Ids to the corresponding registry
219
- await fetchParaIds ( 'polkadot' , prodRelayPolkadot , paraIds ) ;
220
- await fetchParaIds ( 'kusama' , prodRelayKusama , paraIds ) ;
221
- await fetchParaIds ( 'westend' , testRelayWestend , paraIds ) ;
222
-
223
- // Set the relay chain info to the registry
224
- await createChainRegistryFromRelay ( 'polkadot' , prodRelayPolkadot , registry ) ;
225
- await createChainRegistryFromRelay ( 'kusama' , prodRelayKusama , registry ) ;
226
- await createChainRegistryFromRelay ( 'westend' , testRelayWestend , registry ) ;
227
-
228
- // Set the paras info to the registry
229
- for ( const endpoints of polkadotEndpoints ) {
230
- await createChainRegistryFromParas (
231
- 'polkadot' ,
232
- endpoints ,
233
- registry ,
234
- paraIds
235
- ) ;
236
- }
237
-
238
- for ( const endpoints of kusamaEndpoints ) {
239
- await createChainRegistryFromParas ( 'kusama' , endpoints , registry , paraIds ) ;
240
- }
241
-
242
- for ( const endpoints of westendEndpoints ) {
243
- await createChainRegistryFromParas ( 'westend' , endpoints , registry , paraIds ) ;
244
- }
245
-
246
- const path = __dirname + '/../registry.json' ;
247
- writeJson ( path , registry ) ;
248
- } ;
249
-
154
+ /**
155
+ * Fetch Asset info for system parachains.
156
+ *
157
+ * @param api
158
+ */
250
159
const fetchSystemParachainAssetInfo = async (
251
160
api : ApiPromise
252
161
) : Promise < AssetsInfo > => {
@@ -272,15 +181,21 @@ const fetchSystemParachainAssetInfo = async (
272
181
return assetsInfo ;
273
182
} ;
274
183
184
+ /**
185
+ * This will fetch all the foreign asset entries in storage and return an object
186
+ * with each key as the id, and then the info as the nested keys.
187
+ *
188
+ * @param api ApiPromise
189
+ */
275
190
const fetchSystemParachainForeignAssetInfo = async (
276
191
api : ApiPromise
277
192
) : Promise < ForeignAssetsInfo > => {
278
193
const foreignAssetsInfo : ForeignAssetsInfo = { } ;
279
194
280
195
if ( api . query . foreignAssets !== undefined ) {
281
- for ( const [
282
- assetStorageKeyData ,
283
- ] of await api . query . foreignAssets . asset . entries ( ) ) {
196
+ const assetEntries = await api . query . foreignAssets . asset . entries ( ) ;
197
+
198
+ for ( const [ assetStorageKeyData ] of assetEntries ) {
284
199
const foreignAssetData = assetStorageKeyData . toHuman ( ) ;
285
200
286
201
if ( foreignAssetData ) {
@@ -319,6 +234,12 @@ const fetchSystemParachainForeignAssetInfo = async (
319
234
return foreignAssetsInfo ;
320
235
} ;
321
236
237
+ /**
238
+ * Fetch asset conversion pool info from storage. This will return an object where
239
+ * the keys are the token, and the objects within contain info about the token.
240
+ *
241
+ * @param api ApiPromise
242
+ */
322
243
const fetchSystemParachainAssetConversionPoolInfo = async (
323
244
api : ApiPromise
324
245
) : Promise < PoolPairsInfo > => {
@@ -366,7 +287,6 @@ const fetchSystemParachainAssetConversionPoolInfo = async (
366
287
* @param endpointOpts Endpoint we are going to fetch the info from
367
288
* @param paraIds Registry we want to add the info to
368
289
*/
369
-
370
290
const fetchParaIds = async (
371
291
chain : string ,
372
292
endpointOpts : EndpointOption ,
@@ -387,18 +307,19 @@ const fetchParaIds = async (
387
307
return paraIds ;
388
308
} ;
389
309
390
- const sleep = ( ms : number ) : Promise < void > => {
391
- return new Promise ( ( resolve ) => {
392
- setTimeout ( ( ) => resolve ( ) , ms ) ;
393
- } ) ;
394
- } ;
395
-
310
+ /**
311
+ * This will attempt to retrieve an active api that has succesfully connected to a node.
312
+ * It will return null if no connection is made.
313
+ *
314
+ * @param endpointOpts
315
+ * @param isRelay
316
+ */
396
317
const getApi = async ( endpointOpts : EndpointOption , isRelay ?: boolean ) => {
397
318
const { providers, paraId } = endpointOpts ;
398
319
399
- // If no providers are present return an empty object
320
+ // If no providers are present return null.
400
321
if ( Object . keys ( endpointOpts . providers ) . length === 0 ) return null ;
401
- // If a paraId is not present return an empty object;
322
+ // If a paraId is not present return null.
402
323
if ( ! paraId && ! isRelay ) return null ;
403
324
404
325
const endpoints = Object . values ( providers ) . filter (
@@ -416,7 +337,6 @@ const getApi = async (endpointOpts: EndpointOption, isRelay?: boolean) => {
416
337
*
417
338
* @param endpoints Endpoint we are going to try to connect to.
418
339
*/
419
-
420
340
const startApi = async (
421
341
endpoints : string [ ]
422
342
) : Promise < ApiPromise | undefined > => {
@@ -452,7 +372,6 @@ const startApi = async (
452
372
*
453
373
* @param wsEndpoints Endpoint we are going to fetch the info from
454
374
*/
455
-
456
375
const getProvider = async ( wsEndpoints : string [ ] ) => {
457
376
console . log ( 'Getting endpoint providers' ) ;
458
377
@@ -496,6 +415,51 @@ const getProvider = async (wsEndpoints: string[]) => {
496
415
}
497
416
} ;
498
417
418
+ const main = async ( ) => {
419
+ const registry = {
420
+ polkadot : { } ,
421
+ kusama : { } ,
422
+ westend : { } ,
423
+ } ;
424
+
425
+ const paraIds : ParaIds = { } ;
426
+
427
+ const polkadotEndpoints = [ prodParasPolkadot , prodParasPolkadotCommon ] ;
428
+ const kusamaEndpoints = [ prodParasKusama , prodParasKusamaCommon ] ;
429
+ const westendEndpoints = [ testParasWestend , testParasWestendCommon ] ;
430
+
431
+ // Set the Parachains Ids to the corresponding registry
432
+ await fetchParaIds ( 'polkadot' , prodRelayPolkadot , paraIds ) ;
433
+ await fetchParaIds ( 'kusama' , prodRelayKusama , paraIds ) ;
434
+ await fetchParaIds ( 'westend' , testRelayWestend , paraIds ) ;
435
+
436
+ // Set the relay chain info to the registry
437
+ await createChainRegistryFromRelay ( 'polkadot' , prodRelayPolkadot , registry ) ;
438
+ await createChainRegistryFromRelay ( 'kusama' , prodRelayKusama , registry ) ;
439
+ await createChainRegistryFromRelay ( 'westend' , testRelayWestend , registry ) ;
440
+
441
+ // Set the paras info to the registry
442
+ for ( const endpoints of polkadotEndpoints ) {
443
+ await createChainRegistryFromParas (
444
+ 'polkadot' ,
445
+ endpoints ,
446
+ registry ,
447
+ paraIds
448
+ ) ;
449
+ }
450
+
451
+ for ( const endpoints of kusamaEndpoints ) {
452
+ await createChainRegistryFromParas ( 'kusama' , endpoints , registry , paraIds ) ;
453
+ }
454
+
455
+ for ( const endpoints of westendEndpoints ) {
456
+ await createChainRegistryFromParas ( 'westend' , endpoints , registry , paraIds ) ;
457
+ }
458
+
459
+ const path = __dirname + '/../registry.json' ;
460
+ writeJson ( path , registry ) ;
461
+ } ;
462
+
499
463
main ( )
500
464
. catch ( ( err ) => console . error ( err ) )
501
465
. finally ( ( ) => process . exit ( ) ) ;
0 commit comments