-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchainListFetcher.js
More file actions
181 lines (160 loc) · 5.77 KB
/
chainListFetcher.js
File metadata and controls
181 lines (160 loc) · 5.77 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
const fetch = require('node-fetch');
class ChainListFetcher {
constructor() {
this.chainListUrl = 'https://chainlist.org/rpcs.json';
}
async fetchChainData() {
try {
console.log('Fetching chain data from ChainList...');
const response = await fetch(this.chainListUrl);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const chains = await response.json();
console.log(`Fetched ${chains.length} chains from ChainList`);
return chains;
} catch (error) {
console.error('Failed to fetch chain data:', error.message);
return [];
}
}
// Filter and process chains for our use case
processChains(chains) {
const processedChains = [];
// Define common RPC methods for different chain types
const rpcMethods = {
'eth_blockNumber': ['ETH', 'MATIC', 'BNB', 'AVAX', 'FTM', 'ARB', 'OP', 'BASE'],
'getblockcount': ['BTC'],
'eth_getBlockByNumber': ['ETH', 'MATIC', 'BNB', 'AVAX', 'FTM', 'ARB', 'OP', 'BASE'],
'getblockchaininfo': ['BTC']
};
// Popular chains we want to prioritize
const popularChains = [
'Ethereum Mainnet', 'Bitcoin', 'Polygon', 'Binance Smart Chain',
'Avalanche', 'Fantom', 'Arbitrum One', 'Optimism', 'Base',
'Solana', 'Cardano', 'Polkadot', 'Cosmos', 'Chainlink',
'Polygon Mumbai', 'Ethereum Sepolia', 'Arbitrum Sepolia'
];
chains.forEach(chain => {
// Skip testnets unless they're popular ones
if (chain.testnet && !popularChains.some(popular => chain.name.includes(popular))) {
return;
}
// Skip chains without RPC endpoints
if (!chain.rpc || chain.rpc.length === 0) {
return;
}
// Determine RPC method based on chain type
let rpcMethod = 'eth_blockNumber'; // Default for EVM chains
let responsePath = 'result';
let httpMethod = 'POST';
let blockTime = 15; // Default block time
// Bitcoin-like chains
if (chain.name.toLowerCase().includes('bitcoin') ||
chain.chainId === 0 ||
chain.nativeCurrency?.symbol === 'BTC') {
rpcMethod = 'getblockcount';
responsePath = 'result';
blockTime = 600; // 10 minutes
}
// Solana
else if (chain.name.toLowerCase().includes('solana') ||
chain.nativeCurrency?.symbol === 'SOL') {
rpcMethod = 'getSlot';
responsePath = 'result';
blockTime = 0.4; // ~400ms
}
// Cardano
else if (chain.name.toLowerCase().includes('cardano') ||
chain.nativeCurrency?.symbol === 'ADA') {
rpcMethod = 'getCurrentSlot';
responsePath = 'result';
blockTime = 20; // 20 seconds
}
// EVM chains with different block times
else if (chain.name.toLowerCase().includes('polygon') ||
chain.nativeCurrency?.symbol === 'MATIC') {
blockTime = 2;
}
else if (chain.name.toLowerCase().includes('binance') ||
chain.nativeCurrency?.symbol === 'BNB') {
blockTime = 3;
}
else if (chain.name.toLowerCase().includes('avalanche') ||
chain.nativeCurrency?.symbol === 'AVAX') {
blockTime = 2;
}
else if (chain.name.toLowerCase().includes('fantom') ||
chain.nativeCurrency?.symbol === 'FTM') {
blockTime = 1;
}
else if (chain.name.toLowerCase().includes('arbitrum') ||
chain.nativeCurrency?.symbol === 'ARB') {
blockTime = 0.25;
}
else if (chain.name.toLowerCase().includes('optimism') ||
chain.nativeCurrency?.symbol === 'OP') {
blockTime = 2;
}
else if (chain.name.toLowerCase().includes('base') ||
chain.nativeCurrency?.symbol === 'ETH') {
blockTime = 2;
}
// Get the first public RPC endpoint
const publicRpc = chain.rpc.find(rpc =>
rpc.url &&
!rpc.url.includes('localhost') &&
!rpc.url.includes('127.0.0.1') &&
!rpc.url.includes('demo') &&
!rpc.url.includes('api_key')
);
if (!publicRpc) {
return; // Skip if no public RPC
}
const processedChain = {
name: chain.name,
symbol: chain.nativeCurrency?.symbol || 'UNKNOWN',
rpc_method: rpcMethod,
default_params: '[]',
response_path: responsePath,
http_method: httpMethod,
block_time: blockTime,
description: `${chain.name} (Chain ID: ${chain.chainId})`,
chain_id: chain.chainId,
rpc_urls: chain.rpc.map(rpc => rpc.url).slice(0, 5), // Limit to 5 URLs
explorer: chain.explorers?.[0]?.url || '',
is_testnet: Boolean(chain.testnet), // Ensure boolean
icon: chain.icon || '',
short_name: chain.shortName || ''
};
processedChains.push(processedChain);
});
// Sort by popularity and remove duplicates
const uniqueChains = [];
const seenNames = new Set();
// First add popular chains
popularChains.forEach(popularName => {
const chain = processedChains.find(c =>
c.name.toLowerCase().includes(popularName.toLowerCase()) ||
popularName.toLowerCase().includes(c.name.toLowerCase())
);
if (chain && !seenNames.has(chain.name)) {
uniqueChains.push(chain);
seenNames.add(chain.name);
}
});
// Then add remaining chains
processedChains.forEach(chain => {
if (!seenNames.has(chain.name)) {
uniqueChains.push(chain);
seenNames.add(chain.name);
}
});
return uniqueChains.slice(0, 50); // Limit to 50 chains
}
async getProcessedChains() {
const rawChains = await this.fetchChainData();
return this.processChains(rawChains);
}
}
module.exports = ChainListFetcher;