1- import { FastifyInstance , FastifyReply , FastifyRequest } from "fastify" ;
2- import { timedQuery } from "../../../helpers/functions.js" ;
3- import { getSkipLimit } from "../../v2-history/get_actions/functions.js" ;
1+ import { FastifyInstance , FastifyReply , FastifyRequest } from "fastify" ;
2+ import { timedQuery } from "../../../helpers/functions.js" ;
3+ import { getSkipLimit } from "../../v2-history/get_actions/functions.js" ;
44
5- import { estypes } from "@elastic/elasticsearch" ;
6- import { IVoter } from "../../../../interfaces/table-voter.js" ;
5+ import { estypes } from "@elastic/elasticsearch" ;
6+ import { IVoter } from "../../../../interfaces/table-voter.js" ;
77
88async function getVoters ( fastify : FastifyInstance , request : FastifyRequest ) {
99 const query : any = request . query ;
10- const { skip, limit } = getSkipLimit ( request . query ) ;
10+ const { skip, limit} = getSkipLimit ( request . query ) ;
1111 const maxDocs = fastify . manager . config . api . limits . get_voters ?? 100 ;
1212
1313 const response : any = {
@@ -17,46 +17,58 @@ async function getVoters(fastify: FastifyInstance, request: FastifyRequest) {
1717
1818 let stateResult : IVoter [ ] ;
1919
20- if ( fastify . manager . config . indexer . experimental_mongodb_state && fastify . manager . conn . mongodb && query . useMongo === 'true' ) {
20+ if ( fastify . manager . config . indexer . experimental_mongodb_state && fastify . manager . conn . mongodb && query . useMongo === 'true' ) {
2121 const dbName = `${ fastify . manager . conn . mongodb . database_prefix } _${ fastify . manager . chain } ` ;
2222 const collection = fastify . mongo . client . db ( dbName ) . collection < IVoter > ( 'voters' ) ;
2323
2424 const mongoQuery : any = { } ;
2525 if ( query . producer ) {
26- mongoQuery . producers = { $all : query . producer . split ( "," ) } ;
26+ mongoQuery . producers = { $all : query . producer . split ( "," ) } ;
2727 }
28- if ( query . proxy === 'true' ) {
28+
29+ if ( query . is_proxy === 'true' ) {
2930 mongoQuery . is_proxy = true ;
3031 }
3132
3233 response . voter_count = await collection . countDocuments ( mongoQuery ) ;
3334
3435 stateResult = await collection
35- . find ( mongoQuery , { projection : { _id : 0 , voter : 1 , last_vote_weight : 1 , block_num : 1 } } )
36+ . find ( mongoQuery , {
37+ projection : {
38+ _id : 0 ,
39+ voter : 1 ,
40+ last_vote_weight : 1 ,
41+ is_proxy : 1 ,
42+ block_num : 1 ,
43+ staked : 1 ,
44+ producers : query . listProducers === 'true' ? 1 : undefined
45+ }
46+ } )
3647 . skip ( skip || 0 )
3748 . limit ( limit || 50 )
3849 . toArray ( ) ;
3950
4051 } else {
41- let queryStruct : any = { bool : { must : [ ] } } ;
52+
53+ let queryStruct : any = { bool : { must : [ ] } } ;
4254 if ( query . producer ) {
4355 for ( const bp of query . producer . split ( "," ) ) {
44- queryStruct . bool . must . push ( { term : { producers : bp } } ) ;
56+ queryStruct . bool . must . push ( { term : { producers : bp } } ) ;
4557 }
4658 }
4759 if ( query . proxy === 'true' ) {
48- queryStruct . bool . must . push ( { term : { is_proxy : true } } ) ;
60+ queryStruct . bool . must . push ( { term : { is_proxy : true } } ) ;
4961 }
5062 if ( queryStruct . bool . must . length === 0 ) {
51- queryStruct = { match_all : { } } ;
63+ queryStruct = { match_all : { } } ;
5264 }
5365
5466 const esResult = await fastify . elastic . search < any > ( {
5567 index : `${ fastify . manager . chain } -table-voters-*` ,
5668 from : skip || 0 ,
5769 size : ( limit > maxDocs ? maxDocs : limit ) || 10 ,
5870 query : queryStruct ,
59- sort : [ { last_vote_weight : "desc" } ]
71+ sort : [ { last_vote_weight : "desc" } ]
6072 } ) ;
6173 stateResult = esResult . hits . hits . map ( ( hit : any ) => hit . _source ) ;
6274 response . voter_count = ( esResult . hits . total as estypes . SearchTotalHits ) . value ;
@@ -66,7 +78,10 @@ async function getVoters(fastify: FastifyInstance, request: FastifyRequest) {
6678 response . voters . push ( {
6779 account : voter . voter ,
6880 weight : voter . last_vote_weight ,
69- last_vote : voter . block_num
81+ staked : voter . staked ,
82+ is_proxy : voter . is_proxy ,
83+ last_vote_block : voter . block_num ,
84+ producers : voter . producers || undefined
7085 } ) ;
7186 }
7287
0 commit comments