Skip to content

Commit

Permalink
feat: add domain api to return custom domain associated to an offchai…
Browse files Browse the repository at this point in the history
…n space (#302)
  • Loading branch information
wa0x6e authored Sep 12, 2024
1 parent 6c83365 commit d34563c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { queue, getProgress } from './lib/queue';
import { snapshotFee } from './lib/nftClaimer/utils';
import AiSummary from './lib/ai/summary';
import AiTextToSpeech from './lib/ai/textToSpeech';
import { getDomain } from './lib/domain';

const router = express.Router();

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

router.get('/domains/:domain', async (req, res) => {
const { domain } = req.params;

try {
res.json({ domain, space_id: getDomain(domain) });
} catch (e) {
capture(e);
return rpcError(res, 'INTERNAL_ERROR', '');
}
});

router.get('/nft-claimer', async (req, res) => {
try {
return res.json({ snapshotFee: await snapshotFee() });
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import { name, version } from '../package.json';
import { rpcError } from './helpers/utils';
import initMetrics from './lib/metrics';
import initCacheRefresher from './lib/cacheRefresher';
import { initDomainsRefresher } from './lib/domain';

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

initLogger(app);
initMetrics(app);
initCacheRefresher();
initDomainsRefresher();

app.disable('x-powered-by');
app.use(express.json({ limit: '4mb' }));
Expand Down
39 changes: 39 additions & 0 deletions src/lib/domain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { capture } from '@snapshot-labs/snapshot-sentry';
import { sleep } from '../helpers/utils';

const LIST_URL =
'https://raw.githubusercontent.com/snapshot-labs/snapshot-spaces/master/spaces/domains.json';

const REFRESH_INTERVAL = 1000 * 60 * 5; // 5 minutes

// Map of domain (vote.snapshot.org) to space ID (s:snapshot.eth/eth:0x0)
let data = new Map<string, string>();

export function getDomain(domain: string): string | null {
return data.get(domain.toLowerCase()) ?? null;
}

export async function initDomainsRefresher() {
try {
console.log(`[domains-refresh] Refreshing domains list`);
await refreshList();
console.log(`[domains-refresh] ${data.size} domains found`);
} catch (e) {
capture(e);
} finally {
await sleep(REFRESH_INTERVAL);
await initDomainsRefresher();
}
}

async function refreshList() {
const response = await fetch(LIST_URL, {
headers: {
'content-type': 'application/json'
}
});

const body: Record<string, string> = await response.json();

data = new Map(Object.entries(body).map(([domain, spaceId]) => [domain, `s:${spaceId}`]));
}

0 comments on commit d34563c

Please sign in to comment.