-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathxcmgar
executable file
·162 lines (153 loc) · 6.45 KB
/
xcmgar
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
#!/usr/bin/env node
const { Command } = require('commander');
const endpoints = require("./endpoints");
const xcmgarTool = require("./xcmgarTool");
const xcmgarFileMngr = require("./xcmgarFileManager");
const fs = require('fs');
const XCMGlobalAssetRegistryManager = require("./xcmgarManager");
class XCMGlobalAssetRegistryCLI {
async registry(opt) {
//console.log(o)
let { relaychain, paraID } = opt;
let targetedRelaychain = relaychain
let targetedParaID = paraID
let isDryRun = (opt.dry)? true : false
let initType = (opt.serial)? 'serial' : 'parallel'
switch (relaychain) {
case 'kusama':
case 'polkadot':
case 'rococo':
case 'moonbase':
break;
default:
console.log(`Unsupported relaychain ${relaychain}.`)
return;
}
var xcmgarm = new XCMGlobalAssetRegistryManager();
await xcmgarm.initPublicEndpointsMap(targetedRelaychain)
let supportedChains = xcmgarm.getSupportedChains()
let supportedChainKeys = Object.keys(supportedChains)
let supportedChainHelper = xcmgarm.printChainhelper()
console.log(`[${targetedRelaychain}] SupportedChains`, supportedChainHelper)
if(targetedParaID != 'all') {
let singleChainkey = `${targetedRelaychain}-${targetedParaID}`
supportedChainKeys = [singleChainkey]
}
xcmgarm.loadCachedRegistry(targetedRelaychain)
let failedChainkeys = (initType == 'serial')? await xcmgarm.serialCrawlerInit(supportedChainKeys) : await xcmgarm.batchCrawlerInit(supportedChainKeys)
console.log(`failedChains[${failedChainkeys.length}]`, failedChainkeys)
let reachableChainkeys = supportedChainKeys.filter(x => !failedChainkeys.includes(x))
console.log(`reachableChainkeys[${reachableChainkeys.length}]`, reachableChainkeys)
//console.log(`supportedChainKeys[${supportedChainKeys.length}]`, supportedChainKeys)
for (const chainkey of supportedChainKeys){
if (failedChainkeys.includes(chainkey)){
xcmgarm.useCachedAsset(chainkey)
xcmgarm.useCachedXcmAsset(chainkey)
}else{
let crawler = await xcmgarm.getCrawler(chainkey)
if (crawler){
await xcmgarm.crawlRegistry(crawler)
}
}
}
xcmgarm.sortXcmAssetMap()
xcmgarm.sortChainXcmAssetMap()
xcmgarm.sortChainAssetMap()
let chainAssetMap = xcmgarm.getChainAssetMap()
let xcmAssetMap = xcmgarm.getXcmAssetMap()
console.log(`AssetMap`, chainAssetMap)
console.log(`XcmAssetMap`, xcmAssetMap)
if (!isDryRun){
if(targetedParaID == 'all') {
await xcmgarm.updateXcmRegistry()
}
await xcmgarm.updateLocalAsset()
await xcmgarm.updateLocalMultilocation()
}
}
async updateEndpoints(opt) {
var polkadotEndpoint = {
chainkey: 'polkadot-0',
relaychain: 'polkadot',
endpoints: ['wss://rpc.polkadot.io']
}
var kusamaEndpoint = {
chainkey: 'kusama-0',
relaychain: 'kusama',
endpoints: ['wss://kusama-rpc.polkadot.io']
}
var xcmgarm = new XCMGlobalAssetRegistryManager();
let { relaychain } = opt;
let isDryRun = (opt.dry)? true : false
let relaychains = [relaychain]
let relayEndpoints = []
switch (relaychain) {
case 'all':
relayEndpoints = [polkadotEndpoint, kusamaEndpoint]
break;
case 'polkadot':
relayEndpoints = [polkadotEndpoint]
break;
case 'kusama':
relayEndpoints = [kusamaEndpoint]
break;
default:
console.log(`Unsupported relaychain ${relaychain}.`)
return
}
if (relaychain == 'all'){
relaychains = ['polkadot', 'kusama']
}
let validParachainList = await xcmgarm.fetchParaIDs(relayEndpoints)
await xcmgarm.updatePublicEndpoints(relaychains, validParachainList, !isDryRun)
}
async generateData(opt) {
let isURL = (opt.url)? true : false
let isMeta = (opt.metadata)? true : false
if (isMeta) await xcmgarFileMngr.generateMeta()
if (isURL) await xcmgarFileMngr.generateURL()
}
}
async function main() {
let cli_version = '0.0.1'
const program = new Command();
program
.name('xcmgar')
.description('XCM Global Asset Registry. Repo: https://github.com/colorfulnotion/xcm-global-registry')
.version(`${cli_version}-commit.${xcmgarTool.commitHash()}`);
program.command('registry')
.description('Fetch on-chain Asset Registry and XCM MultiLocation Registry')
.option('-r, --relaychain <relaychain>', 'relaychain (polkadot or kusama)', 'polkadot')
.option('-p, --paraID <paraID>', `Targeted paraID (relaychain itself is identified using paraID=0). If not specified, crawl for all reachable parachains.`, 'all')
.option('-s, --serial', `Serial the registry endpoints init. By default all endpoints are initilized in parallel`, false)
.option('-d, --dry', `dry run the cmd without updating`, false)
.action(async (opt) => {
let xcmgarcli = new XCMGlobalAssetRegistryCLI();
await xcmgarcli.registry(opt);
});
program.command('endpoint')
.description('Update public endpoints')
.option('-r, --relaychain <relaychain>', 'Targeted relaychain (polkadot or kusama). If not specified, crawl both polkadot and kusama.', 'all')
.option('-d, --dry', `dry run the cmd without updating`, false)
.action(async (opt) => {
let xcmgarcli = new XCMGlobalAssetRegistryCLI();
await xcmgarcli.updateEndpoints(opt);
});
program.command('generate')
.description('generate metadata from cached results')
.option('-u, --url', `generate xcmgar.json containing all records from {xcmRegistry, assets, xcAssets}`, false)
.option('-m, --metadata', `generate xcmgarUrl.json containing all urls from {xcmRegistry, assets, xcAssets}`, false)
.action(async (opt) => {
let xcmgarcli = new XCMGlobalAssetRegistryCLI();
await xcmgarcli.generateData(opt);
});
await program.parseAsync(process.argv);
}
main()
.then(() => {
process.exit(0);
})
.catch((e) => {
console.error('ERROR', e);
process.exit(1);
});