Skip to content

Commit

Permalink
feat: dreps endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirvolek committed Dec 5, 2023
1 parent ae974c3 commit f598da1
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"cdbsync",
"dbsync",
"delegators",
"dreps",
"elgohr",
"emurgo",
"esbuild",
Expand Down
5 changes: 5 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
28 changes: 28 additions & 0 deletions src/routes/governance/dreps/hash/distribution.ts
Original file line number Diff line number Diff line change
@@ -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<QueryTypes.RequestParameters>, reply) => {
const clientDbSync = await getDbSync(fastify);

const { rows }: { rows: ResponseTypes.Block[] } = await clientDbSync.query<QueryTypes.Block>(
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;
34 changes: 34 additions & 0 deletions src/routes/governance/dreps/hash/index.ts
Original file line number Diff line number Diff line change
@@ -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<QueryTypes.RequestParameters>, reply) => {
const clientDbSync = await getDbSync(fastify);

const { rows }: { rows: ResponseTypes.Block[] } = await clientDbSync.query<QueryTypes.Block>(
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;
37 changes: 37 additions & 0 deletions src/routes/governance/dreps/index.ts
Original file line number Diff line number Diff line change
@@ -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<QueryTypes.RequestParameters>, reply) => {
const clientDbSync = await getDbSync(fastify);

try {
const { rows }: { rows: ResponseTypes.Epoch[] } =
await clientDbSync.query<QueryTypes.Epoch>(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;
1 change: 1 addition & 0 deletions src/sql/governance/dreps.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
governance_dreps
1 change: 1 addition & 0 deletions src/sql/governance/dreps_detail.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drep_details
1 change: 1 addition & 0 deletions src/sql/governance/dreps_distribution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dreps_dristribution
3 changes: 3 additions & 0 deletions src/sql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions src/types/queries/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export type { ResultFound } from '../common.js';
export interface RequestParameters {
Params: {
hash_or_number: string;
hash: string;
};
Querystring: {
count: number;
Expand Down
1 change: 1 addition & 0 deletions src/types/queries/epochs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface RequestParameters {
Querystring: {
count: number;
page: number;
order: 'asc' | 'desc';
};
}

Expand Down

0 comments on commit f598da1

Please sign in to comment.