Skip to content

Commit c006c14

Browse files
committed
refactor: created the transactionsByPactCode query to avoid performance issues
1 parent 79c028b commit c006c14

File tree

11 files changed

+566
-68
lines changed

11 files changed

+566
-68
lines changed

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,8 @@ export type Query = {
798798
* At least one of accountName, fungibleName, blockHash, or requestKey must be provided.
799799
*/
800800
transactions: QueryTransactionsConnection;
801+
/** Retrieve all transactions by a given pact code. */
802+
transactionsByPactCode: QueryTransactionsByPactCodeConnection;
801803
/** Retrieve all transactions by a given public key. */
802804
transactionsByPublicKey: QueryTransactionsByPublicKeyConnection;
803805
/** Retrieve transfers. Default page size is 20. */
@@ -998,6 +1000,14 @@ export type QueryTransactionsArgs = {
9981000
requestKey?: InputMaybe<Scalars['String']['input']>;
9991001
};
10001002

1003+
export type QueryTransactionsByPactCodeArgs = {
1004+
after?: InputMaybe<Scalars['String']['input']>;
1005+
before?: InputMaybe<Scalars['String']['input']>;
1006+
first?: InputMaybe<Scalars['Int']['input']>;
1007+
last?: InputMaybe<Scalars['Int']['input']>;
1008+
pactCode: Scalars['String']['input'];
1009+
};
1010+
10011011
export type QueryTransactionsByPublicKeyArgs = {
10021012
after?: InputMaybe<Scalars['String']['input']>;
10031013
before?: InputMaybe<Scalars['String']['input']>;
@@ -1108,6 +1118,18 @@ export type QueryTokensEdge = {
11081118
node: Token;
11091119
};
11101120

1121+
export type QueryTransactionsByPactCodeConnection = {
1122+
__typename?: 'QueryTransactionsByPactCodeConnection';
1123+
edges: Array<QueryTransactionsByPactCodeConnectionEdge>;
1124+
pageInfo: PageInfo;
1125+
};
1126+
1127+
export type QueryTransactionsByPactCodeConnectionEdge = {
1128+
__typename?: 'QueryTransactionsByPactCodeConnectionEdge';
1129+
cursor: Scalars['String']['output'];
1130+
node: TransactionSummary;
1131+
};
1132+
11111133
export type QueryTransactionsByPublicKeyConnection = {
11121134
__typename?: 'QueryTransactionsByPublicKeyConnection';
11131135
edges: Array<QueryTransactionsByPublicKeyConnectionEdge>;
@@ -1377,6 +1399,19 @@ export type TransactionSignature = {
13771399
sig: Scalars['String']['output'];
13781400
};
13791401

1402+
export type TransactionSummary = {
1403+
__typename?: 'TransactionSummary';
1404+
canonical: Scalars['Boolean']['output'];
1405+
chainId: Scalars['String']['output'];
1406+
creationTime: Scalars['String']['output'];
1407+
gas: Scalars['String']['output'];
1408+
gasLimit: Scalars['String']['output'];
1409+
gasPrice: Scalars['String']['output'];
1410+
height: Scalars['String']['output'];
1411+
requestKey: Scalars['String']['output'];
1412+
sender: Scalars['String']['output'];
1413+
};
1414+
13801415
/** A transfer of funds from a fungible between two accounts. */
13811416
export type Transfer = Node & {
13821417
__typename?: 'Transfer';
@@ -1779,6 +1814,8 @@ export type ResolversTypes = {
17791814
QueryPoolsConnectionEdge: ResolverTypeWrapper<QueryPoolsConnectionEdge>;
17801815
QueryTokensConnection: ResolverTypeWrapper<QueryTokensConnection>;
17811816
QueryTokensEdge: ResolverTypeWrapper<QueryTokensEdge>;
1817+
QueryTransactionsByPactCodeConnection: ResolverTypeWrapper<QueryTransactionsByPactCodeConnection>;
1818+
QueryTransactionsByPactCodeConnectionEdge: ResolverTypeWrapper<QueryTransactionsByPactCodeConnectionEdge>;
17821819
QueryTransactionsByPublicKeyConnection: ResolverTypeWrapper<
17831820
Omit<QueryTransactionsByPublicKeyConnection, 'edges'> & {
17841821
edges: Array<ResolversTypes['QueryTransactionsByPublicKeyConnectionEdge']>;
@@ -1856,6 +1893,7 @@ export type ResolversTypes = {
18561893
Omit<TransactionResultTransfersConnectionEdge, 'node'> & { node: ResolversTypes['Transfer'] }
18571894
>;
18581895
TransactionSignature: ResolverTypeWrapper<TransactionSignature>;
1896+
TransactionSummary: ResolverTypeWrapper<TransactionSummary>;
18591897
Transfer: ResolverTypeWrapper<
18601898
Omit<Transfer, 'block' | 'crossChainTransfer' | 'transaction'> & {
18611899
block: ResolversTypes['Block'];
@@ -2047,6 +2085,8 @@ export type ResolversParentTypes = {
20472085
QueryPoolsConnectionEdge: QueryPoolsConnectionEdge;
20482086
QueryTokensConnection: QueryTokensConnection;
20492087
QueryTokensEdge: QueryTokensEdge;
2088+
QueryTransactionsByPactCodeConnection: QueryTransactionsByPactCodeConnection;
2089+
QueryTransactionsByPactCodeConnectionEdge: QueryTransactionsByPactCodeConnectionEdge;
20502090
QueryTransactionsByPublicKeyConnection: Omit<QueryTransactionsByPublicKeyConnection, 'edges'> & {
20512091
edges: Array<ResolversParentTypes['QueryTransactionsByPublicKeyConnectionEdge']>;
20522092
};
@@ -2105,6 +2145,7 @@ export type ResolversParentTypes = {
21052145
'node'
21062146
> & { node: ResolversParentTypes['Transfer'] };
21072147
TransactionSignature: TransactionSignature;
2148+
TransactionSummary: TransactionSummary;
21082149
Transfer: Omit<Transfer, 'block' | 'crossChainTransfer' | 'transaction'> & {
21092150
block: ResolversParentTypes['Block'];
21102151
crossChainTransfer?: Maybe<ResolversParentTypes['Transfer']>;
@@ -3094,6 +3135,12 @@ export type QueryResolvers<
30943135
ContextType,
30953136
Partial<QueryTransactionsArgs>
30963137
>;
3138+
transactionsByPactCode?: Resolver<
3139+
ResolversTypes['QueryTransactionsByPactCodeConnection'],
3140+
ParentType,
3141+
ContextType,
3142+
RequireFields<QueryTransactionsByPactCodeArgs, 'pactCode'>
3143+
>;
30973144
transactionsByPublicKey?: Resolver<
30983145
ResolversTypes['QueryTransactionsByPublicKeyConnection'],
30993146
ParentType,
@@ -3264,6 +3311,30 @@ export type QueryTokensEdgeResolvers<
32643311
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
32653312
};
32663313

3314+
export type QueryTransactionsByPactCodeConnectionResolvers<
3315+
ContextType = any,
3316+
ParentType extends
3317+
ResolversParentTypes['QueryTransactionsByPactCodeConnection'] = ResolversParentTypes['QueryTransactionsByPactCodeConnection'],
3318+
> = {
3319+
edges?: Resolver<
3320+
Array<ResolversTypes['QueryTransactionsByPactCodeConnectionEdge']>,
3321+
ParentType,
3322+
ContextType
3323+
>;
3324+
pageInfo?: Resolver<ResolversTypes['PageInfo'], ParentType, ContextType>;
3325+
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
3326+
};
3327+
3328+
export type QueryTransactionsByPactCodeConnectionEdgeResolvers<
3329+
ContextType = any,
3330+
ParentType extends
3331+
ResolversParentTypes['QueryTransactionsByPactCodeConnectionEdge'] = ResolversParentTypes['QueryTransactionsByPactCodeConnectionEdge'],
3332+
> = {
3333+
cursor?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
3334+
node?: Resolver<ResolversTypes['TransactionSummary'], ParentType, ContextType>;
3335+
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
3336+
};
3337+
32673338
export type QueryTransactionsByPublicKeyConnectionResolvers<
32683339
ContextType = any,
32693340
ParentType extends
@@ -3595,6 +3666,23 @@ export type TransactionSignatureResolvers<
35953666
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
35963667
};
35973668

3669+
export type TransactionSummaryResolvers<
3670+
ContextType = any,
3671+
ParentType extends
3672+
ResolversParentTypes['TransactionSummary'] = ResolversParentTypes['TransactionSummary'],
3673+
> = {
3674+
canonical?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>;
3675+
chainId?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
3676+
creationTime?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
3677+
gas?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
3678+
gasLimit?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
3679+
gasPrice?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
3680+
height?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
3681+
requestKey?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
3682+
sender?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
3683+
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
3684+
};
3685+
35983686
export type TransferResolvers<
35993687
ContextType = any,
36003688
ParentType extends ResolversParentTypes['Transfer'] = ResolversParentTypes['Transfer'],
@@ -3701,6 +3789,8 @@ export type Resolvers<ContextType = any> = {
37013789
QueryPoolsConnectionEdge?: QueryPoolsConnectionEdgeResolvers<ContextType>;
37023790
QueryTokensConnection?: QueryTokensConnectionResolvers<ContextType>;
37033791
QueryTokensEdge?: QueryTokensEdgeResolvers<ContextType>;
3792+
QueryTransactionsByPactCodeConnection?: QueryTransactionsByPactCodeConnectionResolvers<ContextType>;
3793+
QueryTransactionsByPactCodeConnectionEdge?: QueryTransactionsByPactCodeConnectionEdgeResolvers<ContextType>;
37043794
QueryTransactionsByPublicKeyConnection?: QueryTransactionsByPublicKeyConnectionResolvers<ContextType>;
37053795
QueryTransactionsByPublicKeyConnectionEdge?: QueryTransactionsByPublicKeyConnectionEdgeResolvers<ContextType>;
37063796
QueryTransactionsConnection?: QueryTransactionsConnectionResolvers<ContextType>;
@@ -3725,6 +3815,7 @@ export type Resolvers<ContextType = any> = {
37253815
TransactionResultTransfersConnection?: TransactionResultTransfersConnectionResolvers<ContextType>;
37263816
TransactionResultTransfersConnectionEdge?: TransactionResultTransfersConnectionEdgeResolvers<ContextType>;
37273817
TransactionSignature?: TransactionSignatureResolvers<ContextType>;
3818+
TransactionSummary?: TransactionSummaryResolvers<ContextType>;
37283819
Transfer?: TransferResolvers<ContextType>;
37293820
UserGuard?: UserGuardResolvers<ContextType>;
37303821
};

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,17 @@ type Query {
338338
publicKey: String!
339339
): QueryTransactionsByPublicKeyConnection! @complexity(value: 1, multipliers: ["first", "last"])
340340

341+
"""
342+
Retrieve all transactions by a given pact code.
343+
"""
344+
transactionsByPactCode(
345+
after: String
346+
before: String
347+
first: Int
348+
last: Int
349+
pactCode: String!
350+
): QueryTransactionsByPactCodeConnection! @complexity(value: 1, multipliers: ["first", "last"])
351+
341352
"""
342353
Retrieve transfers. Default page size is 20.
343354
"""
@@ -432,6 +443,28 @@ type Query {
432443
tokenPrices(protocolAddress: String): [TokenPrice!]! @complexity(value: 1)
433444
}
434445

446+
type QueryTransactionsByPactCodeConnection {
447+
edges: [QueryTransactionsByPactCodeConnectionEdge!]!
448+
pageInfo: PageInfo!
449+
}
450+
451+
type QueryTransactionsByPactCodeConnectionEdge {
452+
cursor: String!
453+
node: TransactionSummary!
454+
}
455+
456+
type TransactionSummary {
457+
requestKey: String!
458+
height: String!
459+
chainId: String!
460+
canonical: Boolean!
461+
creationTime: String!
462+
sender: String!
463+
gas: String!
464+
gasLimit: String!
465+
gasPrice: String!
466+
}
467+
435468
"""
436469
Connection type for balance query results.
437470
"""

indexer/src/kadena-server/repository/application/transaction-repository.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import { ConnectionEdge } from '../types';
2525
*/
2626
export type GetTransactionsParams = GetTransactionsCountParams & PaginationsParams;
2727

28+
export type GetTransactionsByPactCodeParams = PaginationsParams & {
29+
pactCode: string;
30+
};
31+
2832
/**
2933
* Parameters for fetching transactions by a specific public key.
3034
* Extends pagination parameters to support page-based data access.
@@ -109,6 +113,18 @@ export type TransactionOutput = Omit<Transaction, 'cmd'> & {
109113
blockHeight: number;
110114
};
111115

116+
export type TransactionByPactCodeOutput = {
117+
creationTime: string;
118+
requestKey: string;
119+
chainId: string;
120+
height: string;
121+
canonical: boolean;
122+
gas: string;
123+
gasLimit: string;
124+
gasPrice: string;
125+
sender: string;
126+
};
127+
112128
/**
113129
* Transaction metadata as returned by the repository.
114130
* Direct mapping to the GraphQL TransactionMeta type.
@@ -138,6 +154,18 @@ export default interface TransactionRepository {
138154
edges: ConnectionEdge<TransactionOutput>[];
139155
}>;
140156

157+
/**
158+
* Retrieves transactions by pact code with pagination.
159+
*
160+
* @param params - Pact code and pagination parameters
161+
* @returns Promise resolving to paginated transaction results
162+
*/
163+
164+
getTransactionsByPactCode(params: GetTransactionsByPactCodeParams): Promise<{
165+
pageInfo: PageInfo;
166+
edges: ConnectionEdge<TransactionByPactCodeOutput>[];
167+
}>;
168+
141169
/**
142170
* Retrieves the last transactions based on specified parameters.
143171
*

indexer/src/kadena-server/repository/infra/query-builders/transaction-query-builder.ts

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -421,14 +421,8 @@ export default class TransactionQueryBuilder {
421421
limit: number;
422422
transactionCode: string;
423423
}) {
424-
let whereCondition = `\nWHERE td.code::text LIKE '%' || $2::text || '%' AND b.canonical = $3`;
425-
let queryParams: (string | number | boolean)[] = [params.limit, params.transactionCode, true];
426-
427-
if (!params.after && !params.before && params.order === 'DESC') {
428-
const currentTime = Date.now() - 10000000;
429-
queryParams.push(currentTime, 0);
430-
whereCondition += `\nAND t.creationtime > $${queryParams.length - 1} AND t.id > $${queryParams.length}`;
431-
}
424+
let whereCondition = `\nWHERE td.code::text LIKE '%' || $2 || '%' AND t.sender != 'coinbase'`;
425+
let queryParams: (string | number | boolean)[] = [params.limit, params.transactionCode];
432426

433427
if (params.after) {
434428
const [creationTime, id] = params.after.split(':');
@@ -442,38 +436,24 @@ export default class TransactionQueryBuilder {
442436
}
443437

444438
const query = `
445-
WITH filtered_transactions AS (
446-
SELECT t.*, td.code, td.nonce, td.sigs, td.continuation, td.pactid, td.proof, td.rollback, td.gas, td.step, td.data, b.height, b.hash as "blockHash"
447-
FROM "Transactions" t
448-
JOIN "TransactionDetails" td ON t.id = td."transactionId"
449-
JOIN "Blocks" b ON b.id = t."blockId"
450-
${whereCondition}
451-
ORDER BY t.creationtime ${params.order}, t.id ${params.order}
452-
)
453439
SELECT
454440
t.id AS id,
455-
t.creationtime AS "creationTime",
456-
t.hash AS "hashTransaction",
457-
t.nonce AS "nonceTransaction",
458-
t.sigs AS sigs,
459-
t.continuation AS continuation,
460-
t.num_events AS "eventCount",
461-
t.pactid AS "pactId",
462-
t.proof AS proof,
463-
t.rollback AS rollback,
464-
t.txid AS txid,
465-
t.height AS "height",
466-
t."blockHash" AS "blockHash",
441+
t.requestkey AS "requestKey",
467442
t."chainId" AS "chainId",
468-
t.gas AS "gas",
469-
t.step AS step,
470-
t.data AS data,
471-
t.code AS code,
472-
t.logs AS "logs",
443+
t.creationtime AS "creationTime",
444+
t.sender AS "sender",
445+
td.gas AS "gas",
446+
td.gaslimit AS "gasLimit",
447+
td.gasprice AS "gasPrice",
473448
t.result AS "result",
474-
t.requestkey AS "requestKey"
475-
FROM filtered_transactions t
476-
LIMIT $1
449+
b.height AS "height",
450+
b.canonical AS "canonical"
451+
FROM "TransactionDetails" td
452+
JOIN "Transactions" t ON t.id = td."transactionId"
453+
JOIN "Blocks" b ON b.id = t."blockId"
454+
${whereCondition}
455+
ORDER BY t.creationtime ${params.order}, t.id ${params.order}
456+
LIMIT $1
477457
`;
478458

479459
return { query, queryParams };

0 commit comments

Comments
 (0)