-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters