Skip to content

Commit

Permalink
feat: openapi endpoint for all inscribed names
Browse files Browse the repository at this point in the history
  • Loading branch information
hstove committed Sep 27, 2023
1 parent 042095d commit f742be8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 35 deletions.
5 changes: 5 additions & 0 deletions api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { prismaPlugin } from './prisma-plugin';
import { aliasRoutes } from './routes/alias-routes';
import { metadataRoutes } from './routes/metadata-routes';
import { appRouter } from './routes/trpc';
import { bridgeRoutes } from './routes/bridge-routes';
import cors from '@fastify/cors';
import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
import { createContext } from './routes/trpc/context';
Expand Down Expand Up @@ -122,6 +123,10 @@ export async function makeApp({
prefix: '/v1',
});

await app.register(bridgeRoutes, {
prefix: '/l1',
});

app.get('/', (req, res) => {
return res.send({ success: true });
});
Expand Down
29 changes: 29 additions & 0 deletions api/src/routes/bridge-routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createContext } from '@routes/trpc/context';
import type { FastifyPlugin } from './api-types';
import { bridgeRouter, inscribedNamesResultsSchema } from './trpc/bridge-router';

export const bridgeRoutes: FastifyPlugin = (fastify, opts, done) => {
fastify.get(
'/inscribed-names',
{
schema: {
description: 'Fetch all BNS names inscribed and bridge to L1',
summary: 'Fetch all inscribed names',
tags: ['L1'],
response: {
200: inscribedNamesResultsSchema,
},
},
},
async (req, res) => {
const caller = bridgeRouter.createCaller(createContext({ req, res }));
const inscribedNames = await caller.inscribedNames();
return res.status(200).send(inscribedNames);
}
);

// fastify.get('/inscription-by-name/:name', async (req, res) => {
// }

done();
};
68 changes: 33 additions & 35 deletions api/src/routes/trpc/bridge-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,44 @@ const inscribedNameResult = z.object({
txid: z.string(),
});

export const inscribedNamesResultsSchema = z.object({
results: z.array(inscribedNameResult),
});

export const bridgeRouter = router({
inscribedNames: procedure
.output(
z.object({
results: z.array(inscribedNameResult),
inscribedNames: procedure.output(inscribedNamesResultsSchema).query(async ({ ctx }) => {
expectDb(ctx.prisma);
expectDb(ctx.bnsxDb);

const inscribedNames = (
await ctx.bnsxDb.inscribedNames.findMany({
include: {
name: true,
},
orderBy: {
blockHeight: 'desc',
},
})
)
.query(async ({ ctx }) => {
expectDb(ctx.prisma);
expectDb(ctx.bnsxDb);
.map(row => {
if (row.name === null) return null;

const inscribedNames = (
await ctx.bnsxDb.inscribedNames.findMany({
include: {
name: true,
},
orderBy: {
blockHeight: 'desc',
},
})
)
.map(row => {
if (row.name === null) return null;

const name = convertDbName(row.name);

return {
inscriptionId: inscriptionBuffToId(hexToBytes(row.inscription_id)),
id: Number(row.id),
name: name.combined,
blockHeight: row.blockHeight,
txid: bytesToHex(row.txid),
};
})
.filter((n): n is NonNullable<typeof n> => n !== null);
const name = convertDbName(row.name);

return {
results: inscribedNames,
};
}),
return {
inscriptionId: inscriptionBuffToId(hexToBytes(row.inscription_id)),
id: Number(row.id),
name: name.combined,
blockHeight: row.blockHeight,
txid: bytesToHex(row.txid),
};
})
.filter((n): n is NonNullable<typeof n> => n !== null);

return {
results: inscribedNames,
};
}),

getInscriptionByName: procedure
.input(z.object({ name: z.string() }))
Expand Down
1 change: 1 addition & 0 deletions api/src/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const OpenApiOptions: SwaggerOptions = {
tags: [
{ name: 'BNS', description: 'BNS Endpoints' },
{ name: 'Backwards Compatible', description: 'Stacks API compatible Endpoints' },
{ name: 'L1', description: 'Endpoints for querying the BNS-L1 bridge' },
],
},
hideUntagged: true,
Expand Down

0 comments on commit f742be8

Please sign in to comment.