-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateChains.js
164 lines (143 loc) · 4.82 KB
/
updateChains.js
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
// updateChains.js
import { promises as fs } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import axios from 'axios';
import dotenv from 'dotenv';
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const DATA_DIR = path.join(__dirname, 'data');
const IBC_DATA_DIR = path.join(DATA_DIR, 'ibc');
// Load config file
const configPath = path.join(__dirname, 'config.json');
const CONFIG = JSON.parse(await fs.readFile(configPath, 'utf8'));
const axiosInstance = axios.create({
headers: {
Authorization: `token ${process.env.GITHUB_PAT}`,
Accept: 'application/vnd.github.v3+json',
},
});
async function ensureDirectories() {
await fs.mkdir(DATA_DIR, { recursive: true });
await fs.mkdir(IBC_DATA_DIR, { recursive: true });
}
async function getLastCommitDate(filePath) {
try {
const response = await axiosInstance.get(
`https://api.github.com/repos/${CONFIG.github.owner}/${CONFIG.github.repo}/commits`,
{
params: { path: filePath, per_page: 1 },
}
);
return new Date(response.data[0].commit.committer.date);
} catch (error) {
if (error.response && error.response.status === 404) {
console.warn(`Warning: ${filePath} not found in the repository.`);
return null;
}
throw error;
}
}
async function fetchIBCData() {
try {
const ibcDir = await axiosInstance.get(
`https://api.github.com/repos/${CONFIG.github.owner}/${CONFIG.github.repo}/contents/_IBC`
);
for (const item of ibcDir.data) {
if (item.type === 'file' && item.name.endsWith('.json')) {
const fileContent = await axiosInstance.get(item.download_url);
const [chain1, chain2] = item.name
.replace('.json', '')
.split('-')
.sort();
const fileName = `${chain1}-${chain2}.json`;
await fs.writeFile(
path.join(IBC_DATA_DIR, fileName),
JSON.stringify(fileContent.data, null, 2)
);
console.log(`Registered IBC channels from ${fileName}.`);
}
}
} catch (error) {
console.error('Error fetching IBC data:', error.message);
throw error;
}
}
async function updateChainData(forceUpdate = false) {
await ensureDirectories();
try {
const dirs = await axiosInstance.get(
`https://api.github.com/repos/${CONFIG.github.owner}/${CONFIG.github.repo}/contents`
);
for (const item of dirs.data) {
if (
item.type === 'dir' &&
!/^[._]/.test(item.name) &&
item.name !== 'testnets'
) {
const remoteFilePath = `${item.name}/chain.json`;
const localFilePath = path.join(DATA_DIR, `${item.name}.json`);
let shouldUpdate = forceUpdate;
if (!shouldUpdate) {
try {
const stats = await fs.stat(localFilePath);
const localModTime = stats.mtime;
const remoteLastCommit = await getLastCommitDate(remoteFilePath);
if (remoteLastCommit && remoteLastCommit > localModTime) {
shouldUpdate = true;
}
} catch (error) {
// File doesn't exist locally, we should update
shouldUpdate = true;
}
}
if (shouldUpdate) {
try {
console.log(`Updating ${item.name}...`);
const chainData = await axiosInstance.get(
`https://raw.githubusercontent.com/${CONFIG.github.owner}/${CONFIG.github.repo}/master/${remoteFilePath}`
);
await fs.writeFile(
localFilePath,
JSON.stringify(chainData.data, null, 2)
);
console.log(`${item.name} updated successfully.`);
} catch (error) {
if (error.response && error.response.status === 404) {
console.warn(
`Warning: ${item.name}/chain.json not found, skipping.`
);
} else {
console.error(`Error updating ${item.name}:`, error.message);
}
}
} else {
console.log(`${item.name} is up to date.`);
}
// Respect rate limits
await new Promise((resolve) => setTimeout(resolve, CONFIG.api.delay));
}
}
// Fetch and updsate IBC data
await fetchIBCData();
await fs.writeFile(
path.join(DATA_DIR, 'update_complete'),
new Date().toISOString()
);
console.log('Update completed.');
} catch (error) {
console.error('Error updating data:', error.message);
throw error;
}
}
export default updateChainData;
// Handle script execution
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const forceUpdate =
process.argv.includes('-f') || process.argv.includes('--force');
updateChainData(forceUpdate).catch((error) => {
console.error('An unexpected error occurred:', error);
process.exit(1);
});
}