Skip to content

Commit 59c1525

Browse files
committed
add pikespeak and delete db endpoints
1 parent 9208d1b commit 59c1525

File tree

1 file changed

+87
-8
lines changed

1 file changed

+87
-8
lines changed

src/server.ts

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,29 +279,108 @@ app.get("/api/user-daos", async (req: Request, res: Response) => {
279279
return res.status(400).send({ error: "account_id is required" });
280280
}
281281

282-
283282
const cacheKey = `user-daos:${account_id}`;
284-
285283
const cachedDaos = cache.get(cacheKey);
286284
if (cachedDaos !== undefined) {
287285
console.log(`🔁 Returning cached Daos for ${account_id}`);
288286
return res.send(cachedDaos);
289287
}
290288

289+
const { data } = await axios.get(`https://api.pikespeak.ai/daos/members`, {
290+
headers: {
291+
"x-api-key": process.env.PIKESPEAK_KEY,
292+
},
293+
});
294+
const userDaos = data?.[account_id]?.["daos"] || [];
295+
cache.set(cacheKey, userDaos, 600); // 10 minutes
296+
return res.send(userDaos);
297+
} catch (error) {
298+
console.error("Error fetching user daos:", error);
299+
return res.status(500).send({ error: "Failed to fetch user daos" });
300+
}
301+
});
302+
303+
app.get("/api/validator-details", async (req: Request, res: Response) => {
304+
try {
305+
const { account_id } = req.query;
306+
307+
if (!account_id || typeof account_id !== "string") {
308+
return res.status(400).send({ error: "account_id is required" });
309+
}
310+
const cacheKey = `validator-details:${account_id}`;
311+
312+
const cachedValidatorDetails = cache.get(cacheKey);
313+
if (cachedValidatorDetails !== undefined) {
314+
console.log(`🔁 Returning cached validator-details`);
315+
return res.send(cachedValidatorDetails);
316+
}
317+
291318
const { data } = await axios.get(
292-
`https://api.pikespeak.ai/daos/members`,
319+
`https://api.pikespeak.ai/validators/details/${account_id}`,
293320
{
294321
headers: {
295322
"x-api-key": process.env.PIKESPEAK_KEY,
296323
},
297324
}
298325
);
299-
const userDaos = data?.[account_id]?.["daos"] || []
300-
cache.set(cacheKey, userDaos, 600); // 10 minutes
301-
return res.send(userDaos);
326+
327+
cache.set(cacheKey, data, 60 * 60 * 24 * 7); // 7 days
328+
return res.send(data);
302329
} catch (error) {
303-
console.error("Error fetching user daos:", error);
304-
return res.status(500).send({ error: "Failed to fetch user daos" });
330+
console.error("Error fetching validator details:", error);
331+
return res.status(500).send({ error: "Failed to fetch validator details" });
332+
}
333+
});
334+
335+
app.get("/api/validators", async (req: Request, res: Response) => {
336+
try {
337+
const cacheKey = `validators`;
338+
339+
const cachedValidators = cache.get(cacheKey);
340+
if (cachedValidators !== undefined) {
341+
console.log(`🔁 Returning cached validators`);
342+
return res.send(cachedValidators);
343+
}
344+
345+
const { data } = await axios.get(
346+
`https://api.pikespeak.ai/validators/current`,
347+
{
348+
headers: {
349+
"x-api-key": process.env.PIKESPEAK_KEY,
350+
},
351+
}
352+
);
353+
const validators = data?.map((item: any) => {
354+
return {
355+
pool_id: item.account_id,
356+
fee: item.fees.numerator,
357+
};
358+
});
359+
cache.set(cacheKey, validators, 60 * 60 * 24); // 1 day
360+
return res.send(validators);
361+
} catch (error) {
362+
console.error("Error fetching validators:", error);
363+
return res.status(500).send({ error: "Failed to fetch validators" });
364+
}
365+
});
366+
367+
app.delete("/api/rpc-request-db", async (req: Request, res: Response) => {
368+
try {
369+
// Delete all rows from RpcRequest table
370+
await prisma.rpcRequest.deleteMany();
371+
372+
// Delete all rows from AccountBlockExistence table
373+
await prisma.accountBlockExistence.deleteMany();
374+
375+
res
376+
.status(200)
377+
.send({
378+
message:
379+
"RpcRequest and AccountBlockExistence tables cleared successfully.",
380+
});
381+
} catch (error) {
382+
console.error("Error clearing tables:", error);
383+
res.status(500).send({ error: "Failed to clear tables" });
305384
}
306385
});
307386

0 commit comments

Comments
 (0)