Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 70 additions & 7 deletions src/common/plugins/plugin.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,84 @@
import { Injectable } from "@nestjs/common";
import { Injectable, Logger } from "@nestjs/common";
import { NestExpressApplication } from "@nestjs/platform-express";
import { AccountDetailed } from "src/endpoints/accounts/entities/account.detailed";
import { About } from "src/endpoints/network/entities/about";
import { Nft } from "src/endpoints/nfts/entities/nft";
import { Transaction } from "src/endpoints/transactions/entities/transaction";

export interface IPlugin {
name: string;
version: string;
processTransactions?(transactions: Transaction[], withScamInfo?: boolean): Promise<void>;
processTransactionSend?(transaction: any): Promise<any>;
processAccount?(account: AccountDetailed): Promise<void>;
bootstrapPublicApp?(application: NestExpressApplication): Promise<void>;
batchProcessNfts?(nfts: Nft[], withScamInfo?: boolean): Promise<void>;
processAbout?(about: About): Promise<void>;
}

@Injectable()
export class PluginService {
async processTransactions(_transactions: Transaction[], _withScamInfo?: boolean): Promise<void> { }
private readonly logger = new Logger(PluginService.name);
private plugins: IPlugin[] = [];

registerPlugins(plugins: IPlugin[]) {
for (const plugin of plugins) {
try {
if (!plugin.name || !plugin.version) {
throw new Error('Plugin must have name and version');
}
this.plugins.push(plugin);
this.logger.log(`Registered plugin: ${plugin.name} v${plugin.version}`);
} catch (err) {
this.logger.error(`Failed to register plugin:`, err);
}
}
}

private async executePluginMethod<T>(methodName: keyof IPlugin, ...args: any[]): Promise<void> {
for (const plugin of this.plugins) {
const method = plugin[methodName] as Function;
if (method) {
try {
await method.apply(plugin, args);
} catch (err) {
this.logger.error(`Error in plugin ${plugin.name} method ${methodName}:`, err);
}
}
}
}

async processTransactions(transactions: Transaction[], withScamInfo?: boolean): Promise<void> {
await this.executePluginMethod('processTransactions', transactions, withScamInfo);
}

async processTransactionSend(_transaction: any): Promise<any> { }
async processTransactionSend(transaction: any): Promise<any> {
let result = transaction;
for (const plugin of this.plugins) {
if (plugin.processTransactionSend) {
try {
result = await plugin.processTransactionSend(result);
} catch (err) {
this.logger.error(`Error in plugin ${plugin.name} processTransactionSend:`, err);
}
}
}
return result;
}

async processAccount(_account: AccountDetailed): Promise<void> { }
async processAccount(account: AccountDetailed): Promise<void> {
await this.executePluginMethod('processAccount', account);
}

async bootstrapPublicApp(_application: NestExpressApplication): Promise<void> { }
async bootstrapPublicApp(application: NestExpressApplication): Promise<void> {
await this.executePluginMethod('bootstrapPublicApp', application);
}

async batchProcessNfts(_nfts: Nft[], _withScamInfo?: boolean): Promise<void> { }
async batchProcessNfts(nfts: Nft[], withScamInfo?: boolean): Promise<void> {
await this.executePluginMethod('batchProcessNfts', nfts, withScamInfo);
}

async processAbout(_about: About): Promise<void> { }
async processAbout(about: About): Promise<void> {
await this.executePluginMethod('processAbout', about);
}
}
39 changes: 38 additions & 1 deletion src/plugins.template/plugin.module.ts.template
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
import { Module } from "@nestjs/common";
import { PluginService } from "../common/plugins/plugin.service";
import * as fs from 'fs';
import * as path from 'path';

// Load compiled plugins
const compiledPluginsPath = path.join(__dirname, 'compiled');
const loadCompiledPlugins = () => {
const plugins = [];
if (fs.existsSync(compiledPluginsPath)) {
const entries = fs.readdirSync(compiledPluginsPath);
for (const entry of entries) {
const pluginPath = path.join(compiledPluginsPath, entry);
if (fs.statSync(pluginPath).isDirectory()) {
const distPath = path.join(pluginPath, 'dist', 'index.js');
if (fs.existsSync(distPath)) {
try {
const plugin = require(distPath);
plugins.push(plugin);
} catch (err) {
console.error(`Failed to load compiled plugin ${entry}:`, err);
}
}
}
}
}
return plugins;
};

@Module({
providers: [PluginService],
providers: [
{
provide: PluginService,
useFactory: () => {
const service = new PluginService();
// Load and register compiled plugins
const compiledPlugins = loadCompiledPlugins();
service.registerPlugins(compiledPlugins);
return service;
},
},
],
exports: [PluginService],
})
export class PluginModule { }
Loading