-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
91 lines (76 loc) · 2.85 KB
/
index.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
import 'dotenv/config'
import fs from 'fs';
import { Wallet, JsonRpcProvider } from 'ethers';
import IndexClient from "@indexnetwork/sdk";
import { getAllTeams } from './utils/getAllTeams.js';
import { createTeam } from './utils/getComposeClient.js';
// Function to load teams from cache or fetch new ones
const loadTeams = async () => {
const fromCache = true; // Toggle this to switch between cache and fetching new data, to not hurt PL directory server.
if (fromCache) {
return JSON.parse(fs.readFileSync("temp/teams.json"));
} else {
const teams = await getAllTeams();
fs.writeFileSync("temp/teams.json", JSON.stringify(teams));
return teams;
}
};
const go = async () => {
try {
console.log('Starting process...');
const walletPrivateKey = process.env.WALLET_PRIVATE_KEY;
const rpcEndpoint = process.env.LIT_RPC_ENDPOINT;
if (!walletPrivateKey || !rpcEndpoint) {
throw new Error('Environment variables for wallet or RPC endpoint are missing.');
}
const wallet = new Wallet(walletPrivateKey, new JsonRpcProvider(rpcEndpoint));
console.log('Wallet initialized.');
const indexClient = new IndexClient({
privateKey: wallet.privateKey,
domain: "index.network",
network: "ethereum",
});
console.log('Authenticating index client...');
await indexClient.authenticate();
console.log('Index client authenticated.');
console.log('Creating index...');
const index = await indexClient.createIndex("PL Ecosystem Teams");
console.log(`Index created with ID: ${index.id}`);
console.log('Loading teams...');
const teams = await loadTeams(); // This function needs to be defined elsewhere
console.log(`Loaded ${teams.length} teams.`);
let createdTeams = [];
for (const t of teams) {
let createdTeam;
try {
createdTeam = await createTeam(wallet, t); // Assuming createTeam is defined elsewhere
console.log(`Team created: ${t.name}`);
} catch (err) {
console.error('Error creating team:', err);
continue; // Skip this team and proceed with the next one
}
createdTeams.push(createdTeam);
}
console.log('Adding teams to index...');
for (const t of createdTeams) {
try {
console.log(index.id, t, "seref")
await indexClient.addItemToIndex(index.id, t);
console.log(`Team added to index: ${t}`);
} catch (err) {
console.error('Error adding team to index:', err);
}
}
/* Uncomment this block if you want to query the index
console.log('Querying index...');
const queryResponse = await indexClient.query({
query: "Which team is building a decentralized semantic index?",
indexes: [indexId],
});
console.log('Query response:', queryResponse);
*/
} catch (error) {
console.error('Exception in process:', error);
}
};
go()