-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
60 lines (47 loc) · 1.88 KB
/
index.ts
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
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { updateRealmBalances, data_url } from './fetcher';
import * as dotenv from 'dotenv';
dotenv.config();
const localFilePath: string = path.join(__dirname, process.env.REALMS_DATA as string);
const programIdsFilePath: string = path.join(__dirname, 'programId.json');
interface Dao {
displayName: string;
programId: string;
realmId: string;
Token_Treasury: string;
}
async function getDaosData(): Promise<Dao[]> {
console.log('Fetching data...');
const response = await fetch(data_url);
const data: Dao[] = await response.json() as Dao[];
// Ensure the directory exists
fs.mkdirSync(path.dirname(localFilePath), { recursive: true });
fs.writeFileSync(localFilePath, JSON.stringify(data, null, 2));
return data;
}
function getProgramIds(data: Dao[]): string[] {
return [...new Set(data.map(dao => dao.programId))];
}
function writeProgramIds(programIds: string[]): void {
const output = { programIds };
fs.writeFileSync(programIdsFilePath, JSON.stringify(output, null, 2));
console.log('Program IDs saved to programId.json');
}
async function main(args: string[]): Promise<void> {
if (args.includes('update') && args.includes('programids')) {
console.log('Updating program IDs...');
const data = await getDaosData();
const programIds = getProgramIds(data);
writeProgramIds(programIds);
} else if (args.includes('update') && args.includes('realms')) {
console.log('Updating ...');
const programIds = JSON.parse(fs.readFileSync(programIdsFilePath, 'utf-8')).programIds;
for (const programId of programIds) {
console.log(`Updating ${programId}...`);
await updateRealmBalances(programId);
}
}
}
main(process.argv.slice(2)).catch(error => console.error('Error:', error));