From f598da1e786a0c98b5692056c071d2236e6bb2d5 Mon Sep 17 00:00:00 2001 From: Vladimir Volek Date: Tue, 5 Dec 2023 14:39:53 +0100 Subject: [PATCH] feat: dreps endpoints --- .vscode/settings.json | 1 + src/app.ts | 5 +++ .../governance/dreps/hash/distribution.ts | 28 ++++++++++++++ src/routes/governance/dreps/hash/index.ts | 34 +++++++++++++++++ src/routes/governance/dreps/index.ts | 37 +++++++++++++++++++ src/sql/governance/dreps.sql | 1 + src/sql/governance/dreps_detail.sql | 1 + src/sql/governance/dreps_distribution.sql | 1 + src/sql/index.ts | 3 ++ src/types/queries/blocks.ts | 1 + src/types/queries/epochs.ts | 1 + 11 files changed, 113 insertions(+) create mode 100644 src/routes/governance/dreps/hash/distribution.ts create mode 100644 src/routes/governance/dreps/hash/index.ts create mode 100644 src/routes/governance/dreps/index.ts create mode 100644 src/sql/governance/dreps.sql create mode 100644 src/sql/governance/dreps_detail.sql create mode 100644 src/sql/governance/dreps_distribution.sql diff --git a/.vscode/settings.json b/.vscode/settings.json index ca5e1fc0..2b5e1536 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,6 +7,7 @@ "cdbsync", "dbsync", "delegators", + "dreps", "elgohr", "emurgo", "esbuild", diff --git a/src/app.ts b/src/app.ts index 2ef8b73a..5996aa4f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -111,6 +111,11 @@ const start = (options = {}): FastifyInstance => { registerRoute(app, import('./routes/epochs/number/stakes/index.js')); registerRoute(app, import('./routes/epochs/number/stakes/pool-id.js')); + // governance + registerRoute(app, import('./routes/governance/dreps/index.js')); + registerRoute(app, import('./routes/governance/dreps/hash/index.js')); + registerRoute(app, import('./routes/governance/dreps/hash/distribution.js')); + // health registerRoute(app, import('./routes/health/index.js')); registerRoute(app, import('./routes/health/clock.js')); diff --git a/src/routes/governance/dreps/hash/distribution.ts b/src/routes/governance/dreps/hash/distribution.ts new file mode 100644 index 00000000..2774b43d --- /dev/null +++ b/src/routes/governance/dreps/hash/distribution.ts @@ -0,0 +1,28 @@ +import { FastifyInstance, FastifyRequest } from 'fastify'; +import * as QueryTypes from '../../../../types/queries/blocks.js'; +import * as ResponseTypes from '../../../../types/responses/blocks.js'; +import { getDbSync } from '../../../../utils/database.js'; +import { SQLQuery } from '../../../../sql/index.js'; + +async function route(fastify: FastifyInstance) { + fastify.route({ + url: '/governance/dreps/:hash/distribution', + method: 'GET', + // TODO: add schema when available + // schema: getSchemaForEndpoint('/governance/dreps/{hash}/distribution'), + handler: async (request: FastifyRequest, reply) => { + const clientDbSync = await getDbSync(fastify); + + const { rows }: { rows: ResponseTypes.Block[] } = await clientDbSync.query( + SQLQuery.get('governance_dreps_distribution'), + [request.params.hash, request.query.order, request.query.count, request.query.page], + ); + + clientDbSync.release(); + + return reply.send(rows); + }, + }); +} + +export default route; diff --git a/src/routes/governance/dreps/hash/index.ts b/src/routes/governance/dreps/hash/index.ts new file mode 100644 index 00000000..5233e843 --- /dev/null +++ b/src/routes/governance/dreps/hash/index.ts @@ -0,0 +1,34 @@ +import { FastifyInstance, FastifyRequest } from 'fastify'; +import * as QueryTypes from '../../../../types/queries/blocks.js'; +import * as ResponseTypes from '../../../../types/responses/blocks.js'; +import { getDbSync } from '../../../../utils/database.js'; +import { handle404 } from '../../../../utils/error-handler.js'; +import { SQLQuery } from '../../../../sql/index.js'; + +async function route(fastify: FastifyInstance) { + fastify.route({ + url: '/governance/dreps/:hash', + method: 'GET', + // TODO: add schema when available + // schema: getSchemaForEndpoint('/governance/dreps/{hash}'), + handler: async (request: FastifyRequest, reply) => { + const clientDbSync = await getDbSync(fastify); + + const { rows }: { rows: ResponseTypes.Block[] } = await clientDbSync.query( + SQLQuery.get('governance_dreps_details'), + [request.params.hash], + ); + + clientDbSync.release(); + + const row = rows[0]; + + if (!row) { + return handle404(reply); + } + return reply.send(row); + }, + }); +} + +export default route; diff --git a/src/routes/governance/dreps/index.ts b/src/routes/governance/dreps/index.ts new file mode 100644 index 00000000..8db93a61 --- /dev/null +++ b/src/routes/governance/dreps/index.ts @@ -0,0 +1,37 @@ +import { FastifyInstance, FastifyRequest } from 'fastify'; +import { SQLQuery } from '../../../sql/index.js'; +import * as QueryTypes from '../../../types/queries/epochs.js'; +import * as ResponseTypes from '../../../types/responses/epochs.js'; +import { getDbSync } from '../../../utils/database.js'; + +async function route(fastify: FastifyInstance) { + fastify.route({ + url: '/governance/dreps', + method: 'GET', + // TODO: add schema when available + // schema: getSchemaForEndpoint('/epochs/latest'), + handler: async (request: FastifyRequest, reply) => { + const clientDbSync = await getDbSync(fastify); + + try { + const { rows }: { rows: ResponseTypes.Epoch[] } = + await clientDbSync.query(SQLQuery.get('governance_dreps'), [ + request.query.order, + request.query.count, + request.query.page, + ]); + + clientDbSync.release(); + + return reply.send(rows); + } catch (error) { + if (clientDbSync) { + clientDbSync.release(); + } + throw error; + } + }, + }); +} + +export default route; diff --git a/src/sql/governance/dreps.sql b/src/sql/governance/dreps.sql new file mode 100644 index 00000000..0b84f489 --- /dev/null +++ b/src/sql/governance/dreps.sql @@ -0,0 +1 @@ +governance_dreps \ No newline at end of file diff --git a/src/sql/governance/dreps_detail.sql b/src/sql/governance/dreps_detail.sql new file mode 100644 index 00000000..ee92c4eb --- /dev/null +++ b/src/sql/governance/dreps_detail.sql @@ -0,0 +1 @@ +drep_details \ No newline at end of file diff --git a/src/sql/governance/dreps_distribution.sql b/src/sql/governance/dreps_distribution.sql new file mode 100644 index 00000000..cbc0384c --- /dev/null +++ b/src/sql/governance/dreps_distribution.sql @@ -0,0 +1 @@ +dreps_dristribution \ No newline at end of file diff --git a/src/sql/index.ts b/src/sql/index.ts index 89fda6ae..490bfb95 100644 --- a/src/sql/index.ts +++ b/src/sql/index.ts @@ -87,6 +87,9 @@ const QUERY_FILES = { epochs_number_stakes_pool_id: 'epochs/epochs_number_stakes_pool_id.sql', epochs_number_stakes_pool_id_unpaged: 'epochs/unpaged/epochs_number_stakes_pool_id.sql', epochs_pool_404: 'epochs/epochs_pool_404.sql', + governance_dreps: 'governance/dreps.sql', + governance_dreps_details: 'governance/dreps_detail.sql', + governance_dreps_distribution: 'governance/dreps_distribution.sql', metadata_txs_labels: 'metadata/metadata_txs_labels.sql', metadata_txs_labels_unpaged: 'metadata/unpaged/metadata_txs_labels.sql', metadata_txs_labels_label: 'metadata/metadata_txs_labels_label.sql', diff --git a/src/types/queries/blocks.ts b/src/types/queries/blocks.ts index d0f1290a..2a94d1f9 100644 --- a/src/types/queries/blocks.ts +++ b/src/types/queries/blocks.ts @@ -4,6 +4,7 @@ export type { ResultFound } from '../common.js'; export interface RequestParameters { Params: { hash_or_number: string; + hash: string; }; Querystring: { count: number; diff --git a/src/types/queries/epochs.ts b/src/types/queries/epochs.ts index b4bc780e..29a72342 100644 --- a/src/types/queries/epochs.ts +++ b/src/types/queries/epochs.ts @@ -7,6 +7,7 @@ export interface RequestParameters { Querystring: { count: number; page: number; + order: 'asc' | 'desc'; }; }