Skip to content

Commit

Permalink
add /v2/state/get_top_holders
Browse files Browse the repository at this point in the history
  • Loading branch information
igorls committed Sep 19, 2024
1 parent e5499dd commit 45445d3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
62 changes: 62 additions & 0 deletions api/routes/v2-state/get_top_holders/get_top_holders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { timedQuery } from "../../../helpers/functions";
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { getSkipLimit } from "../../v2-history/get_actions/functions";

async function getTopHolders(fastify: FastifyInstance, request: FastifyRequest) {

const query: any = request.query;

const response: any = {
contract: query.account,
symbol: undefined,
holders: []
};

const { skip, limit } = getSkipLimit(request.query);

const maxDocs = fastify.manager.config.api.limits.get_top_holders ?? 500;

const terms: any[] = [];


if (query.contract) {
terms.push({ "term": { "code": { "value": query.contract } } });
}

if (query.symbol) {
terms.push({ "term": { "symbol": { "value": query.symbol } } });
}


const stateResult = await fastify.elastic.search<any>({
"index": fastify.manager.chain + '-table-accounts-*',
"size": (limit > maxDocs ? maxDocs : limit) || 50,
"from": skip || 0,
body: {
sort: {
amount: {
order: "desc"
}
},
query: { bool: { "must": terms } }
}
});

response.holders = stateResult.body.hits.hits.map((doc: any) => {
return {
owner: doc._source.scope,
amount: doc._source.amount,
symbol: doc._source.symbol,
updated_on: doc._source.block_num
};
});

return response;

}

export function getTopHoldersHandler(fastify: FastifyInstance, route: string) {
return async (request: FastifyRequest, reply: FastifyReply) => {
reply.send(await timedQuery(getTopHolders, fastify, request, route));
}
}
31 changes: 31 additions & 0 deletions api/routes/v2-state/get_top_holders/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {FastifyInstance} from "fastify";
import {addApiRoute, extendQueryStringSchema, getRouteName} from "../../../helpers/functions";
import { getTopHoldersHandler } from "./get_top_holders";

export default function (fastify: FastifyInstance, opts: any, next) {
const schema = {
description: 'get the list of top holders for a given token',
summary: 'get top token holders',
tags: ['accounts'],
querystring: extendQueryStringSchema({
"contract": {
description: 'token contract account name',
type: 'string',
minLength: 1,
maxLength: 12
},
"symbol": {
description: 'filter by token symbol',
type: 'string'
},
}, ["contract"])
};
addApiRoute(
fastify,
'GET',
getRouteName(__filename),
getTopHoldersHandler,
schema
);
next();
}
1 change: 1 addition & 0 deletions interfaces/hyperionConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface IndexerConfigs {
}

interface ApiLimits {
get_top_holders?: number;
get_links?: number;
get_actions?: number;
get_blocks?: number;
Expand Down

0 comments on commit 45445d3

Please sign in to comment.