Skip to content

Commit

Permalink
feat: getTopHolders to support MongoDB queries
Browse files Browse the repository at this point in the history
  • Loading branch information
lealbrunocalhau committed Nov 25, 2024
1 parent 7e50237 commit 68c5ae9
Showing 1 changed file with 54 additions and 29 deletions.
83 changes: 54 additions & 29 deletions src/api/routes/v2-state/get_top_holders/get_top_holders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {timedQuery} from "../../../helpers/functions.js";
import {getSkipLimit} from "../../v2-history/get_actions/functions.js";

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

const query: any = request.query;

const response: any = {
Expand All @@ -16,41 +15,67 @@ async function getTopHolders(fastify: FastifyInstance, request: FastifyRequest)

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

const terms: any[] = [];
if (fastify.manager.config.indexer.experimental_mongodb_state && fastify.manager.conn.mongodb && query.useMongo === 'true') {
console.log(`MongoDB is enabled for state queries - get_top_holders`);
const dbName = `${fastify.manager.conn.mongodb.database_prefix}_${fastify.manager.chain}`;
const collection = fastify.mongo.client.db(dbName).collection('accounts');

const mongoQuery: any = {};
if (query.contract) {
mongoQuery.code = query.contract;
}
if (query.symbol) {
mongoQuery.symbol = query.symbol;
}

if (query.contract) {
terms.push({"term": {"code": {"value": query.contract}}});
}
response.holders = await collection
.find(mongoQuery)
.sort({ amount: -1 })
.skip(skip || 0)
.limit((limit > maxDocs ? maxDocs : limit) || 50)
.toArray();

if (query.symbol) {
terms.push({"term": {"symbol": {"value": query.symbol}}});
}
response.holders = response.holders.map((doc: any) => ({
owner: doc.scope,
amount: doc.amount,
symbol: doc.symbol,
updated_on: doc.block_num
}));
} else {
console.log(`Elastic is enabled for state queries - get_top_holders`);
const terms: any[] = [];

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

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

response.holders = stateResult.hits.hits.map((doc: any) => {
return {
owner: doc._source.scope,
amount: doc._source.amount,
symbol: doc._source.symbol,
updated_on: doc._source.block_num
};
});
if (query.symbol) {
terms.push({"term": {"symbol": {"value": query.symbol}}});
}

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

response.holders = stateResult.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) {
Expand Down

0 comments on commit 68c5ae9

Please sign in to comment.