Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add domain api to return custom domain associated to an offchain space #302

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to store sx spaces on snapshot-spaces?

then maybe it is better to add s: on that repo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this repo will be deprecated soon.

Sidekick is aggregating v2 spaces from the repo, sx spaces will have another source I believe

}
Loading