-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
141 lines (119 loc) · 3.57 KB
/
app.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
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
import express, { Request, Response } from "express";
import axios from "axios";
import cors from "cors";
import { EAS_CHAIN_CONFIGS } from "./chains";
import { ethers } from "ethers";
import { setGlobalCache, getGlobalCache } from "./cache";
const app = express();
const port = 3008;
// Enable CORS for all routes
app.use(cors());
// Helper function to get attestation count
async function getAttestationCount(
chainId: string,
address: string
): Promise<number> {
const cacheKey = `attestations_${chainId}_${address.toLowerCase()}`;
const cachedResult = getGlobalCache<{ count: number }>(cacheKey);
if (cachedResult) {
console.log("Cache hit for", cacheKey);
return cachedResult.count;
}
const chainConfig = EAS_CHAIN_CONFIGS.find((chain) => chain.id === chainId);
if (!chainConfig) {
throw new Error("Invalid chainId");
}
const url = `https://${chainConfig.subdomain}easscan.org/graphql`;
try {
const response = await axios.post(
url,
{
query: `
query AggregateAttestation($where: AttestationWhereInput) {
aggregateAttestation(where: $where) {
_count {
_all
}
}
}
`,
variables: {
where: {
OR: [
{ attester: { equals: ethers.getAddress(address) } },
{ recipient: { equals: ethers.getAddress(address) } },
],
},
},
},
{
headers: { "Content-Type": "application/json" },
}
);
const count = response.data.data.aggregateAttestation._count._all;
const result = { count };
// Cache the result for 1 hour (3600000 milliseconds)
setGlobalCache(cacheKey, result, 3600 * 1000);
return count;
} catch (error) {
console.error(
`Error fetching attestation count for chain ${chainId}:`,
error
);
return 0;
}
}
// Helper function to validate Ethereum address
function isValidEthereumAddress(address: string): boolean {
try {
ethers.getAddress(address);
return true;
} catch (error) {
return false;
}
}
// Update /countAttestations endpoint
app.get(
"/countAttestations/:chainId/:address",
async (req: Request, res: Response) => {
const { chainId, address } = req.params;
if (!isValidEthereumAddress(address)) {
return res.status(400).json({ error: "Invalid Ethereum address" });
}
try {
const count = await getAttestationCount(chainId, address);
res.json({ count });
} catch (error) {
if (error instanceof Error) {
res.status(400).json({ error: error.message });
} else {
res.status(400).json({ error: "An unknown error occurred" });
}
}
}
);
// Update /countAllAttestations/:address endpoint
app.get(
"/countAllAttestations/:address",
async (req: Request, res: Response) => {
const { address } = req.params;
if (!address) {
return res.status(400).json({ error: "Address is required" });
}
if (!isValidEthereumAddress(address)) {
return res.status(400).json({ error: "Invalid Ethereum address" });
}
const formattedAddress = ethers.getAddress(address);
const counts = await Promise.all(
EAS_CHAIN_CONFIGS.map(async (chain) => {
const count = await getAttestationCount(chain.id, formattedAddress);
return { chainId: chain.id, count };
})
);
const totalCount = counts.reduce((sum, item) => sum + item.count, 0);
res.json({ counts, totalCount });
}
);
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});