Skip to content

Commit 687eab5

Browse files
authored
chore: cleanup
chore: cleanup
2 parents 92d5c48 + 5573e8d commit 687eab5

File tree

3 files changed

+172
-125
lines changed

3 files changed

+172
-125
lines changed

src/createRegistry.ts

Lines changed: 89 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -15,76 +15,23 @@ import {
1515
testRelayWestend,
1616
} from '@polkadot/apps-config';
1717
import type { EndpointOption } from '@polkadot/apps-config/endpoints/types';
18-
import fs from 'fs';
1918

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';
8230

8331
/**
8432
* @const MAX_RETRIES Maximum amount of connection attempts
8533
* @const WS_DISCONNECT_TIMEOUT_SECONDS time to wait between attempts, in seconds
8634
*/
87-
8835
const MAX_RETRIES = 5;
8936
const WS_DISCONNECT_TIMEOUT_SECONDS = 3;
9037

@@ -108,7 +55,7 @@ const fetchChainInfo = async (
10855
const foreignAssetsPallet = api.registry.metadata.pallets.filter(
10956
(pallet) => pallet.name.toString().toLowerCase() === 'foreignassets'
11057
)[0];
111-
58+
11259
const { tokenSymbol } = await api.rpc.system.properties();
11360
const { specName } = await api.rpc.state.getRuntimeVersion();
11461
const tokens = tokenSymbol.isSome
@@ -143,7 +90,9 @@ const fetchChainInfo = async (
14390
poolPairsInfo,
14491
specName: specNameStr,
14592
assetsPalletInstance: assetsPallet ? assetsPallet.index.toString() : null,
146-
foreignAssetsPalletInstance: foreignAssetsPallet ? foreignAssetsPallet.index.toString(): null,
93+
foreignAssetsPalletInstance: foreignAssetsPallet
94+
? foreignAssetsPallet.index.toString()
95+
: null,
14796
};
14897
} else {
14998
return null;
@@ -202,51 +151,11 @@ const createChainRegistryFromRelay = async (
202151
}
203152
};
204153

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+
*/
250159
const fetchSystemParachainAssetInfo = async (
251160
api: ApiPromise
252161
): Promise<AssetsInfo> => {
@@ -272,15 +181,21 @@ const fetchSystemParachainAssetInfo = async (
272181
return assetsInfo;
273182
};
274183

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+
*/
275190
const fetchSystemParachainForeignAssetInfo = async (
276191
api: ApiPromise
277192
): Promise<ForeignAssetsInfo> => {
278193
const foreignAssetsInfo: ForeignAssetsInfo = {};
279194

280195
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) {
284199
const foreignAssetData = assetStorageKeyData.toHuman();
285200

286201
if (foreignAssetData) {
@@ -319,6 +234,12 @@ const fetchSystemParachainForeignAssetInfo = async (
319234
return foreignAssetsInfo;
320235
};
321236

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+
*/
322243
const fetchSystemParachainAssetConversionPoolInfo = async (
323244
api: ApiPromise
324245
): Promise<PoolPairsInfo> => {
@@ -366,7 +287,6 @@ const fetchSystemParachainAssetConversionPoolInfo = async (
366287
* @param endpointOpts Endpoint we are going to fetch the info from
367288
* @param paraIds Registry we want to add the info to
368289
*/
369-
370290
const fetchParaIds = async (
371291
chain: string,
372292
endpointOpts: EndpointOption,
@@ -387,18 +307,19 @@ const fetchParaIds = async (
387307
return paraIds;
388308
};
389309

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+
*/
396317
const getApi = async (endpointOpts: EndpointOption, isRelay?: boolean) => {
397318
const { providers, paraId } = endpointOpts;
398319

399-
// If no providers are present return an empty object
320+
// If no providers are present return null.
400321
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.
402323
if (!paraId && !isRelay) return null;
403324

404325
const endpoints = Object.values(providers).filter(
@@ -416,7 +337,6 @@ const getApi = async (endpointOpts: EndpointOption, isRelay?: boolean) => {
416337
*
417338
* @param endpoints Endpoint we are going to try to connect to.
418339
*/
419-
420340
const startApi = async (
421341
endpoints: string[]
422342
): Promise<ApiPromise | undefined> => {
@@ -452,7 +372,6 @@ const startApi = async (
452372
*
453373
* @param wsEndpoints Endpoint we are going to fetch the info from
454374
*/
455-
456375
const getProvider = async (wsEndpoints: string[]) => {
457376
console.log('Getting endpoint providers');
458377

@@ -496,6 +415,51 @@ const getProvider = async (wsEndpoints: string[]) => {
496415
}
497416
};
498417

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+
499463
main()
500464
.catch((err) => console.error(err))
501465
.finally(() => process.exit());

src/types.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2023 Parity Technologies (UK) Ltd.
2+
3+
export type TokenRegistry = {
4+
polkadot: {};
5+
kusama: {};
6+
westend: {};
7+
};
8+
9+
export type PoolPairsInfo = {
10+
[key: string]: {
11+
lpToken: string;
12+
pairInfo: string;
13+
};
14+
};
15+
16+
export type ChainName = 'polkadot' | 'kusama' | 'westend';
17+
18+
export type ForeignAssetMetadata = {
19+
deposit: string;
20+
name: string;
21+
symbol: string;
22+
decimals: string;
23+
isFrozen: boolean;
24+
};
25+
26+
export interface AssetsInfo {
27+
[key: string]: string;
28+
}
29+
30+
export interface ForeignAssetsInfo {
31+
[key: string]: {
32+
symbol: string;
33+
name: string;
34+
multiLocation: string;
35+
};
36+
}
37+
38+
export interface PoolInfo {
39+
lpToken: string;
40+
}
41+
42+
export interface ParaIds {
43+
[key: string]: number[];
44+
}

src/util.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2023 Parity Technologies (UK) Ltd.
2+
3+
import fs from 'fs';
4+
5+
import type { TokenRegistry } from './types';
6+
7+
/**
8+
* Write Json to a file path.
9+
*
10+
* @param path Path to write the json file too
11+
* @param data Data that will be written to file.
12+
*/
13+
export const writeJson = (path: string, data: TokenRegistry): void => {
14+
fs.writeFileSync(path, JSON.stringify(data, null, 2));
15+
fs.appendFileSync(path, '\n', 'utf-8');
16+
};
17+
18+
/**
19+
* This create a loader for the terminal.
20+
*/
21+
export const twirlTimer = () => {
22+
const P = ['\\', '|', '/', '-'];
23+
let x = 0;
24+
return setInterval(function () {
25+
process.stdout.write('\r' + P[x++]);
26+
x &= 3;
27+
}, 250);
28+
};
29+
30+
/**
31+
* Sleep utility.
32+
*
33+
* @param ms Milliseconds
34+
*/
35+
export const sleep = (ms: number): Promise<void> => {
36+
return new Promise((resolve) => {
37+
setTimeout(() => resolve(), ms);
38+
});
39+
};

0 commit comments

Comments
 (0)