generated from multiversx/mx-template-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create new app transactions-processor
- Loading branch information
1 parent
06bdad5
commit f3c1ad8
Showing
14 changed files
with
219 additions
and
2 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
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,28 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ApiMetricsController, CommonConfigModule, DynamicModuleUtils, HealthCheckController } from '@libs/common'; | ||
import { ApiMetricsModule } from '@libs/common'; | ||
import { LoggingModule } from '@multiversx/sdk-nestjs-common'; | ||
import { AppConfigModule } from './config/app-config.module'; | ||
import { ScheduleModule } from '@nestjs/schedule'; | ||
import { ProcessorService } from './processor/processor.service'; | ||
import { DatabaseModule } from '@libs/database'; | ||
|
||
@Module({ | ||
imports: [ | ||
LoggingModule, | ||
ApiMetricsModule, | ||
AppConfigModule, | ||
CommonConfigModule, | ||
ScheduleModule.forRoot(), | ||
DynamicModuleUtils.getCachingModule(), | ||
DatabaseModule, | ||
], | ||
providers: [ | ||
ProcessorService, | ||
], | ||
controllers: [ | ||
ApiMetricsController, | ||
HealthCheckController, | ||
], | ||
}) | ||
export class AppModule { } |
13 changes: 13 additions & 0 deletions
13
apps/transactions-processor/src/config/app-config.module.ts
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,13 @@ | ||
import { Global, Module } from "@nestjs/common"; | ||
import { AppConfigService } from "./app-config.service"; | ||
|
||
@Global() | ||
@Module({ | ||
providers: [ | ||
AppConfigService, | ||
], | ||
exports: [ | ||
AppConfigService, | ||
], | ||
}) | ||
export class AppConfigModule { } |
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,7 @@ | ||
import { configuration } from "@libs/common/config/configuration"; | ||
import { Injectable } from "@nestjs/common"; | ||
|
||
@Injectable() | ||
export class AppConfigService { | ||
readonly config = configuration().apps.transactionsProcessor; | ||
} |
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,44 @@ | ||
import * as dotenv from 'dotenv'; | ||
import { resolve } from 'path'; | ||
|
||
// Determine which .env file to load based on NODE_ENV | ||
const envPath = process.env.NODE_ENV === 'infra' ? '.env' : `.env.${process.env.NODE_ENV ?? 'mainnet'}`; | ||
dotenv.config({ | ||
path: resolve(process.cwd(), envPath), | ||
}); | ||
|
||
import 'module-alias/register'; | ||
import { NestFactory } from '@nestjs/core'; | ||
import { CommonConfigService, PubSubListenerModule } from '@libs/common'; | ||
import { AppModule } from './app.module'; | ||
import { MicroserviceOptions, Transport } from '@nestjs/microservices'; | ||
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston'; | ||
import { AppConfigService } from './config/app-config.service'; | ||
|
||
async function bootstrap() { | ||
const transactionProcessorApp = await NestFactory.create(AppModule); | ||
const appConfigService = transactionProcessorApp.get<AppConfigService>(AppConfigService); | ||
const commonConfigService = transactionProcessorApp.get<CommonConfigService>(CommonConfigService); | ||
|
||
await transactionProcessorApp.listen(appConfigService.config.port); | ||
|
||
const pubSubApp = await NestFactory.createMicroservice<MicroserviceOptions>( | ||
PubSubListenerModule.forRoot(), | ||
{ | ||
transport: Transport.REDIS, | ||
options: { | ||
host: commonConfigService.config.redis.host, | ||
port: commonConfigService.config.redis.port, | ||
retryAttempts: 100, | ||
retryDelay: 1000, | ||
retryStrategy: () => 1000, | ||
}, | ||
}, | ||
); | ||
pubSubApp.useLogger(pubSubApp.get(WINSTON_MODULE_NEST_PROVIDER)); | ||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
pubSubApp.listen(); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
bootstrap(); |
46 changes: 46 additions & 0 deletions
46
apps/transactions-processor/src/processor/processor.service.ts
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,46 @@ | ||
import { Locker } from "@multiversx/sdk-nestjs-common"; | ||
import { Injectable } from "@nestjs/common"; | ||
import { Cron, CronExpression } from "@nestjs/schedule"; | ||
|
||
import { DynamicCollectionRepository } from "@libs/database/collections"; | ||
import { EventProcessor, EventProcessorOptions } from "@libs/services/event-processor/event.processor"; | ||
|
||
@Injectable() | ||
export class ProcessorService { | ||
private eventsProcessor: EventProcessor = new EventProcessor(); | ||
// private readonly logger: Logger; | ||
|
||
constructor( | ||
private readonly dynamicCollectionService: DynamicCollectionRepository, | ||
// private readonly commonConfigService: CommonConfigService, | ||
// private readonly appConfigService: AppConfigService, | ||
) { | ||
// this.logger = new Logger(ProcessorService.name); | ||
} | ||
|
||
@Cron(CronExpression.EVERY_10_SECONDS) | ||
async handleNewTransactions() { | ||
await Locker.lock('newTransactions', async () => { | ||
await this.eventsProcessor.start(new EventProcessorOptions({ | ||
elasticUrl: 'https://index.multiversx.com', | ||
eventIdentifiers: ['ESDTTransfer'], | ||
emitterAddresses: ['erd1z08z0svvqs84dnrh5hrm47agcxmch79fslmupzgqcgfdtpp3slwqlna25a'], | ||
pageSize: 3, | ||
getLastProcessedTimestamp: async () => { | ||
const result = await this.dynamicCollectionService.getLastProcessedTimestamp() | ||
console.log(result); | ||
return result; | ||
}, | ||
setLastProcessedTimestamp: async (nonce) => { | ||
await this.dynamicCollectionService.setLastProcessedTimestamp(nonce); | ||
}, | ||
onEventsReceived: async (highestTimestamp, events) => { | ||
await Promise.resolve(); | ||
console.log(`onEventsReceived -> highestTimestamp: ${highestTimestamp}`); | ||
console.log(`onEventsReceived -> events: ${JSON.stringify(events)}`); | ||
}, | ||
})); | ||
}) | ||
} | ||
|
||
} |
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,16 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"declaration": false, | ||
"outDir": "../../dist" | ||
}, | ||
"include": [ | ||
"src/**/*" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
"dist", | ||
"test", | ||
"**/*spec.ts" | ||
] | ||
} |
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
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
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
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
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
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
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