Skip to content

Commit

Permalink
add candidates/rank endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
will pankiewicz authored and will pankiewicz committed Feb 14, 2024
1 parent c4c33c7 commit 5ee058a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
8 changes: 8 additions & 0 deletions packages/gateway/src/controllers/Candidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ export default class CandidateController {
response(context, 200, await CandidateService.getCandidates());
}

public static async getRankCandidates(context: any): Promise<void> {
if (await context.cashed()) {
logger.info(`{Gateway} getCandidates is cached`, gatewayLabel);
return;
}
response(context, 200, await CandidateService.getRankCandidates());
}

public static async getNodes(context: any): Promise<void> {
if (await context.cashed()) {
logger.info(`{Gateway} getNodes is cached`, gatewayLabel);
Expand Down
2 changes: 2 additions & 0 deletions packages/gateway/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const API = {
Accounting: "/accounting/:address",
Candidate: "/candidate/:address",
GetCandidates: "/candidates",
GetRankCandidates: "/candidates/rank",
GetValidCandidates: "/candidates/valid",
GetInvalidCandidates: "/candidates/invalid",
GetNodes: "/nodes",
Expand Down Expand Up @@ -92,6 +93,7 @@ router.get(API.Accounting, Accounting.getAccounting);

router.get(API.Candidate, Candidate.getCandidate);
router.get(API.GetCandidates, Candidate.getCandidates);
router.get(API.GetRankCandidates, Candidate.getRankCandidates);
router.get(API.GetValidCandidates, Candidate.getValidCandidates);
router.get(API.GetInvalidCandidates, Candidate.getInvalidCandidates);

Expand Down
15 changes: 13 additions & 2 deletions packages/gateway/src/services/Candidate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ export const getInvalidCandidates = async (): Promise<any> => {
};

export const getCandidates = async (): Promise<any> => {
const metadata = await queries.getChainMetadata();
const denom = Math.pow(10, metadata?.decimals);
let allCandidates = await queries.allCandidates();
allCandidates = await Promise.all(
allCandidates.map(async (candidate) => {
Expand All @@ -105,6 +103,19 @@ export const getCandidates = async (): Promise<any> => {
return allCandidates;
};

export const getRankCandidates = async (): Promise<any> => {
let allCandidates = await queries.allCandidates();
allCandidates = await Promise.all(
allCandidates.map(async (candidate) => {
return await getCandidateData(candidate);
}),
);
allCandidates = allCandidates.sort((a, b) => {
return b.rank - a.rank;
});
return allCandidates;
};

export const getNodes = async (): Promise<any> => {
const allNodes: Array<any> = await queries.allNodes();
return allNodes.map((node) => {
Expand Down

0 comments on commit 5ee058a

Please sign in to comment.