Skip to content

Commit a62d39c

Browse files
committed
feat: add domain api to return custom domain associated to an offchain space
1 parent 6c83365 commit a62d39c

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/api.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { queue, getProgress } from './lib/queue';
99
import { snapshotFee } from './lib/nftClaimer/utils';
1010
import AiSummary from './lib/ai/summary';
1111
import AiTextToSpeech from './lib/ai/textToSpeech';
12+
import { getDomain } from './lib/domain';
1213

1314
const router = express.Router();
1415

@@ -101,6 +102,17 @@ router.get('/moderation', async (req, res) => {
101102
}
102103
});
103104

105+
router.get('/domains/:domain', async (req, res) => {
106+
const { domain } = req.params;
107+
108+
try {
109+
res.json({ domain, space_id: getDomain(domain) });
110+
} catch (e) {
111+
capture(e);
112+
return rpcError(res, 'INTERNAL_ERROR', '');
113+
}
114+
});
115+
104116
router.get('/nft-claimer', async (req, res) => {
105117
try {
106118
return res.json({ snapshotFee: await snapshotFee() });

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import { name, version } from '../package.json';
1111
import { rpcError } from './helpers/utils';
1212
import initMetrics from './lib/metrics';
1313
import initCacheRefresher from './lib/cacheRefresher';
14+
import { initDomainsRefresher } from './lib/domain';
1415

1516
const app = express();
1617
const PORT = process.env.PORT || 3005;
1718

1819
initLogger(app);
1920
initMetrics(app);
2021
initCacheRefresher();
22+
initDomainsRefresher();
2123

2224
app.disable('x-powered-by');
2325
app.use(express.json({ limit: '4mb' }));

src/lib/domain.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { capture } from '@snapshot-labs/snapshot-sentry';
2+
import { sleep } from '../helpers/utils';
3+
4+
const LIST_URL =
5+
'https://raw.githubusercontent.com/snapshot-labs/snapshot-spaces/master/spaces/domains.json';
6+
7+
const REFRESH_INTERVAL = 1000 * 60 * 5; // 5 minutes
8+
9+
// Map of domain (vote.snapshot.org) to space ID (s:snapshot.eth/eth:0x0)
10+
let data = new Map<string, string>();
11+
12+
export function getDomain(domain: string): string | null {
13+
return data.get(domain.toLowerCase()) ?? null;
14+
}
15+
16+
export async function initDomainsRefresher() {
17+
try {
18+
console.log(`[domains-refresh] Refreshing domains list`);
19+
await refreshList();
20+
console.log(`[domains-refresh] ${data.size} domains found`);
21+
} catch (e) {
22+
capture(e);
23+
} finally {
24+
await sleep(REFRESH_INTERVAL);
25+
await initDomainsRefresher();
26+
}
27+
}
28+
29+
async function refreshList() {
30+
const response = await fetch(LIST_URL, {
31+
headers: {
32+
'content-type': 'application/json'
33+
}
34+
});
35+
36+
const body: Record<string, string> = await response.json();
37+
38+
data = new Map(Object.entries(body).map(([domain, spaceId]) => [domain, `s:${spaceId}`]));
39+
}

0 commit comments

Comments
 (0)