-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-chains.js
More file actions
161 lines (134 loc) Β· 4.5 KB
/
Copy pathinit-chains.js
File metadata and controls
161 lines (134 loc) Β· 4.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
#!/usr/bin/env node
const DatabaseManager = require('./database');
const ChainListFetcher = require('./chainListFetcher');
class ChainInitializer {
constructor() {
this.db = new DatabaseManager();
}
async initializeChains() {
console.log('π Starting chain initialization...');
try {
// Check if chains already exist
const existingChains = this.db.getAllChains();
if (existingChains.length > 0) {
console.log(`β
Found ${existingChains.length} existing chains in database`);
console.log('Chains already initialized. Use --force to reinitialize.');
return;
}
console.log('π‘ Fetching chains from ChainList...');
const chainFetcher = new ChainListFetcher();
const chains = await chainFetcher.getProcessedChains();
console.log(`π Processing ${chains.length} chains from ChainList...`);
let addedCount = 0;
let skippedCount = 0;
for (const chain of chains) {
try {
this.db.addChain(chain);
addedCount++;
console.log(`β
Added: ${chain.name} (${chain.symbol})`);
} catch (error) {
if (error.message.includes('UNIQUE constraint failed')) {
skippedCount++;
console.log(`βοΈ Skipped: ${chain.name} (already exists)`);
} else {
console.error(`β Error adding ${chain.name}:`, error.message);
}
}
}
console.log(`\nπ Chain initialization complete!`);
console.log(`β
Added: ${addedCount} chains`);
console.log(`βοΈ Skipped: ${skippedCount} chains`);
} catch (error) {
console.error('β Failed to initialize chains from ChainList:', error.message);
console.log('π Falling back to basic default chains...');
await this.addFallbackChains();
}
}
async addFallbackChains() {
fs.copyFile('sync_checker.db.example', 'sync_checker.db', (err) => {
if (err) throw err;
});
console.log(`β
Added ${addedCount} fallback chains`);
}
async forceReinitialize() {
console.log('π Force reinitializing chains...');
// Clear existing chains
const existingChains = this.db.getAllChains();
for (const chain of existingChains) {
try {
this.db.deleteChain(chain.id);
console.log(`ποΈ Deleted: ${chain.name}`);
} catch (error) {
console.log(`β οΈ Could not delete ${chain.name}: ${error.message}`);
}
}
// Reinitialize
await this.initializeChains();
}
async listChains() {
const chains = this.db.getAllChains();
console.log(`\nπ Current chains in database (${chains.length} total):`);
console.log('β'.repeat(80));
chains.forEach(chain => {
const type = chain.is_testnet ? 'Testnet' : 'Mainnet';
const chainId = chain.chain_id ? ` (ID: ${chain.chain_id})` : '';
console.log(`β’ ${chain.name}${chainId} - ${chain.symbol} [${type}]`);
console.log(` RPC: ${chain.rpc_method} | Block Time: ${chain.block_time}s`);
if (chain.description) {
console.log(` ${chain.description}`);
}
console.log('');
});
}
close() {
this.db.close();
}
}
// CLI interface
async function main() {
const args = process.argv.slice(2);
const command = args[0] || 'init';
const initializer = new ChainInitializer();
try {
switch (command) {
case 'init':
await initializer.initializeChains();
break;
case 'force':
await initializer.forceReinitialize();
break;
case 'list':
await initializer.listChains();
break;
case 'help':
console.log(`
π Chain Initializer
Usage: node init-chains.js [command]
Commands:
init Initialize chains from ChainList (default)
force Force reinitialize (clears existing chains)
list List all chains in database
help Show this help message
Examples:
node init-chains.js # Initialize chains
node init-chains.js force # Force reinitialize
node init-chains.js list # List current chains
`);
break;
default:
console.log(`β Unknown command: ${command}`);
console.log('Use "node init-chains.js help" for usage information');
process.exit(1);
}
} catch (error) {
console.error('β Error:', error.message);
process.exit(1);
} finally {
initializer.close();
}
}
// Run if called directly
if (require.main === module) {
main();
}
module.exports = ChainInitializer;