Skip to content

Commit

Permalink
added function name for pool transactions (#1425)
Browse files Browse the repository at this point in the history
* added function name for pool transactions

* fix modules imports

* fix unit tests

* add function filter
  • Loading branch information
bogdan-rosianu authored Jan 8, 2025
1 parent 0937b65 commit 1f340f7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/endpoints/pool/entities/pool.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export class PoolFilter {
senderShard?: number;
receiverShard?: number;
type?: TransactionType;
functions?: string[];
}
3 changes: 3 additions & 0 deletions src/endpoints/pool/entities/transaction.in.pool.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export class TransactionInPool {
@ApiProperty({ type: String, example: "0228618b6339c5eaf71ed1a8cd71df010ccd0369a29d957c37d53b0409408161726dd97e10ac7836996f666ffd636a797b9b9abecbd276971376fb3479b48203" })
signature: string = '';

@ApiProperty({ type: String, nullable: true, example: 'composeTasks', required: false })
function: string = '';

@ApiProperty({ type: String, example: "SmartContractResult" })
type: TransactionType = TransactionType.Transaction;
}
7 changes: 6 additions & 1 deletion src/endpoints/pool/pool.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ParseAddressAndMetachainPipe, ParseAddressPipe, ParseEnumPipe, ParseIntPipe, ParseTransactionHashPipe } from "@multiversx/sdk-nestjs-common";
import { ParseAddressAndMetachainPipe, ParseAddressPipe, ParseEnumPipe, ParseIntPipe, ParseTransactionHashPipe, ParseArrayPipe } from "@multiversx/sdk-nestjs-common";
import { Controller, DefaultValuePipe, Get, NotFoundException, Param, Query } from "@nestjs/common";
import { ApiExcludeEndpoint, ApiNotFoundResponse, ApiOkResponse, ApiOperation, ApiQuery, ApiTags } from "@nestjs/swagger";
import { PoolService } from "./pool.service";
import { QueryPagination } from "src/common/entities/query.pagination";
import { TransactionInPool } from "./entities/transaction.in.pool.dto";
import { TransactionType } from "../transactions/entities/transaction.type";
import { PoolFilter } from "./entities/pool.filter";
import { ParseArrayPipeOptions } from "@multiversx/sdk-nestjs-common/lib/pipes/entities/parse.array.options";

@Controller()
@ApiTags('pool')
Expand All @@ -24,6 +25,8 @@ export class PoolController {
@ApiQuery({ name: 'senderShard', description: 'The shard of the sender', required: false })
@ApiQuery({ name: 'receiverShard', description: 'The shard of the receiver', required: false })
@ApiQuery({ name: 'type', description: 'Search in transaction pool by type', required: false })
@ApiQuery({ name: 'function', description: 'Filter transactions by function name', required: false })

async getTransactionPool(
@Query('from', new DefaultValuePipe(0), ParseIntPipe) from: number,
@Query('size', new DefaultValuePipe(25), ParseIntPipe) size: number,
Expand All @@ -32,13 +35,15 @@ export class PoolController {
@Query('senderShard', ParseIntPipe) senderShard?: number,
@Query('receiverShard', ParseIntPipe) receiverShard?: number,
@Query('type', new ParseEnumPipe(TransactionType)) type?: TransactionType,
@Query('function', new ParseArrayPipe(new ParseArrayPipeOptions({ allowEmptyString: true }))) functions?: string[],
): Promise<TransactionInPool[]> {
return await this.poolService.getPool(new QueryPagination({ from, size }), new PoolFilter({
sender: sender,
receiver: receiver,
senderShard: senderShard,
receiverShard: receiverShard,
type: type,
functions: functions,
}));
}

Expand Down
4 changes: 4 additions & 0 deletions src/endpoints/pool/pool.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Module } from "@nestjs/common";
import { PoolService } from "./pool.service";
import { TransactionActionModule } from "../transactions/transaction-action/transaction.action.module";

@Module({
imports: [
TransactionActionModule,
],
providers: [
PoolService,
],
Expand Down
16 changes: 15 additions & 1 deletion src/endpoints/pool/pool.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { PoolFilter } from "./entities/pool.filter";
import { TxInPoolFields } from "src/common/gateway/entities/tx.in.pool.fields";
import { AddressUtils } from "@multiversx/sdk-nestjs-common";
import { ProtocolService } from "../../common/protocol/protocol.service";
import { TransactionActionService } from "../transactions/transaction-action/transaction.action.service";
import { Transaction } from "../transactions/entities/transaction";
import { ApiUtils } from "@multiversx/sdk-nestjs-http";

@Injectable()
export class PoolService {
Expand All @@ -19,6 +22,7 @@ export class PoolService {
private readonly apiConfigService: ApiConfigService,
private readonly cacheService: CacheService,
private readonly protocolService: ProtocolService,
private readonly transactionActionService: TransactionActionService,
) { }

async getTransactionFromPool(txHash: string): Promise<TransactionInPool | undefined> {
Expand Down Expand Up @@ -105,6 +109,11 @@ export class PoolService {
transaction.receiverShard = AddressUtils.computeShard(AddressUtils.bech32Decode(transaction.receiver), shardCount);
}

const metadata = await this.transactionActionService.getTransactionMetadata(this.poolTransactionToTransaction(transaction), false);
if (metadata && metadata.functionName) {
transaction.function = metadata.functionName;
}

return transaction;
}

Expand All @@ -115,8 +124,13 @@ export class PoolService {
(!filters.receiver || transaction.receiver === filters.receiver) &&
(!filters.type || transaction.type === filters.type) &&
(filters.senderShard === undefined || transaction.senderShard === filters.senderShard) &&
(filters.receiverShard === undefined || transaction.receiverShard === filters.receiverShard)
(filters.receiverShard === undefined || transaction.receiverShard === filters.receiverShard) &&
(filters.functions === undefined || transaction.function === undefined || filters.functions.indexOf(transaction.function) > -1)
);
});
}

private poolTransactionToTransaction(transaction: TransactionInPool): Transaction {
return ApiUtils.mergeObjects(new Transaction(), transaction);
}
}
7 changes: 7 additions & 0 deletions src/test/unit/services/pool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PoolFilter } from "src/endpoints/pool/entities/pool.filter";
import { PoolService } from "src/endpoints/pool/pool.service";
import { TransactionType } from "src/endpoints/transactions/entities/transaction.type";
import { ProtocolService } from "../../../common/protocol/protocol.service";
import { TransactionActionService } from "../../../endpoints/transactions/transaction-action/transaction.action.service";

describe('PoolService', () => {
let service: PoolService;
Expand Down Expand Up @@ -41,6 +42,12 @@ describe('PoolService', () => {
isTransactionPoolEnabled: jest.fn().mockResolvedValue(true),
},
},
{
provide: TransactionActionService,
useValue: {
getTransactionMetadata: jest.fn(),
},
},
],
}).compile();

Expand Down

0 comments on commit 1f340f7

Please sign in to comment.