Skip to content

Commit 2881465

Browse files
authored
Merge pull request #435 from hack-a-chain-software/total-count-blocks-from-height
feat: added total count in blocksFromHeight query
2 parents a98225a + 31e0bd7 commit 2881465

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

indexer/src/kadena-server/config/graphql-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ export type QueryBlocksFromHeightConnection = {
10081008
__typename?: 'QueryBlocksFromHeightConnection';
10091009
edges: Array<QueryBlocksFromHeightConnectionEdge>;
10101010
pageInfo: PageInfo;
1011+
totalCount: Scalars['Int']['output'];
10111012
};
10121013

10131014
export type QueryBlocksFromHeightConnectionEdge = {
@@ -3061,6 +3062,7 @@ export type QueryBlocksFromHeightConnectionResolvers<
30613062
ContextType
30623063
>;
30633064
pageInfo?: Resolver<ResolversTypes['PageInfo'], ParentType, ContextType>;
3065+
totalCount?: Resolver<ResolversTypes['Int'], ParentType, ContextType>;
30643066
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
30653067
};
30663068

indexer/src/kadena-server/config/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ type QueryEventsConnectionEdge {
556556
type QueryBlocksFromHeightConnection {
557557
edges: [QueryBlocksFromHeightConnectionEdge!]!
558558
pageInfo: PageInfo!
559+
totalCount: Int!
559560
}
560561

561562
type QueryBlocksFromHeightConnectionEdge {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Resolver for the totalCount field of the QueryEventsConnection type.
3+
* This module counts blockchain events based on various filter criteria.
4+
*/
5+
import { ResolverContext } from '../../../config/apollo-server-config';
6+
import { QueryBlocksFromHeightConnectionResolvers } from '../../../config/graphql-types';
7+
import zod from 'zod';
8+
9+
/**
10+
* Zod schema for validating event query parameters.
11+
* Defines the structure and types of filter parameters used to count events.
12+
* Note that qualifiedEventName is required, while other filters are optional.
13+
*/
14+
const schema = zod.object({
15+
chainIds: zod.array(zod.string()).optional(),
16+
startHeight: zod.number(),
17+
endHeight: zod.number().nullable().optional(),
18+
});
19+
20+
/**
21+
* Resolver function for the totalCount field of the QueryEventsConnection type.
22+
* Retrieves the total count of events matching the provided filter criteria.
23+
*
24+
* @param parent - The parent object containing filter parameters
25+
* @param _args - GraphQL arguments (unused in this resolver)
26+
* @param context - The resolver context containing repositories and services
27+
* @returns The total count of events matching the criteria
28+
*/
29+
export const totalCountQueryBlocksFromHeightConnectionResolver: QueryBlocksFromHeightConnectionResolvers<ResolverContext>['totalCount'] =
30+
async (parent, _args, context) => {
31+
const { startHeight, endHeight, chainIds: chainIdsParam } = schema.parse(parent);
32+
33+
if (endHeight && startHeight > endHeight) {
34+
throw new Error('Start height cannot be greater than end height');
35+
}
36+
37+
let chainIds = [];
38+
if (chainIdsParam?.length) {
39+
chainIds = [...chainIdsParam];
40+
} else {
41+
const networkInfo = await context.networkRepository.getAllInfo();
42+
chainIds = [...networkInfo.nodeChains];
43+
}
44+
45+
const currentChainHeights = await context.networkRepository.getCurrentChainHeights();
46+
const startHeightOfChainsFrom9to19 = 852054;
47+
const totalCount = chainIds.reduce((acum, chainId) => {
48+
const endHeightToUse = endHeight ?? currentChainHeights[chainId];
49+
let totalOfChain = endHeightToUse - startHeight + 1;
50+
if (Number(chainId) > 9) {
51+
if (startHeight >= startHeightOfChainsFrom9to19) {
52+
totalOfChain = endHeightToUse - startHeight + 1;
53+
} else if (endHeightToUse < startHeightOfChainsFrom9to19) {
54+
totalOfChain = 0;
55+
} else {
56+
totalOfChain = endHeightToUse - startHeightOfChainsFrom9to19 + 1;
57+
}
58+
}
59+
return totalOfChain + acum;
60+
}, 0);
61+
62+
return totalCount;
63+
};

indexer/src/kadena-server/resolvers/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import { transfersNonFungibleAccountResolver } from '@/kadena-server/resolvers/f
101101
import { totalCountNonFungibleAccountTransfersConnectionResolver } from '@/kadena-server/resolvers/fields/non-fungible-account/transfers-connection/total-count-non-fungible-account-transfers-connection-resolver';
102102
import { transfersNonFungibleChainAccountResolver } from '@/kadena-server/resolvers/fields/non-fungible-chain-account/transfers-non-fungible-chain-account-resolver';
103103
import { totalCountNonFungibleChainAccountTransfersConnectionResolver } from '@/kadena-server/resolvers/fields/non-fungible-chain-account/transfers-connection/total-count-non-fungible-chain-account-transfers-connection-resolver';
104+
import { totalCountQueryBlocksFromHeightConnectionResolver } from '@/kadena-server/resolvers/fields/query-blocks-from-height-connection/total-count-query-blocks-from-height-connection-resolver';
104105
/**
105106
* Complete resolver map for the GraphQL API
106107
*
@@ -238,6 +239,9 @@ export const resolvers: Resolvers<ResolverContext> = {
238239
signers: signersTransactionCommandResolver, // add dataloader
239240
meta: metaTransactionCommandResolver, // add dataloader
240241
},
242+
QueryBlocksFromHeightConnection: {
243+
totalCount: totalCountQueryBlocksFromHeightConnectionResolver,
244+
},
241245
QueryBlocksFromDepthConnection: {
242246
totalCount: totalCountQueryBlocksFromDepthConnectionResolver,
243247
},

indexer/src/kadena-server/resolvers/query/blocks-from-height-query-resolver.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,24 @@ import { buildBlockOutput } from '../output/build-block-output';
2828
*/
2929
export const blocksFromHeightQueryResolver: QueryResolvers<ResolverContext>['blocksFromHeight'] =
3030
async (_parent, args, context) => {
31+
if (args.endHeight && args.startHeight > args.endHeight) {
32+
throw new Error('Start height cannot be greater than end height');
33+
}
34+
3135
const output = await context.blockRepository.getBlocksBetweenHeights(args);
3236

3337
const edges = output.edges.map(e => ({
3438
cursor: e.cursor,
3539
node: buildBlockOutput(e.node),
3640
}));
3741

38-
return { edges, pageInfo: output.pageInfo };
42+
return {
43+
edges,
44+
pageInfo: output.pageInfo,
45+
46+
startHeight: args.startHeight,
47+
endHeight: args.endHeight,
48+
chainIds: args.chainIds,
49+
totalCount: -1,
50+
};
3951
};

0 commit comments

Comments
 (0)