nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.05k stars 7.56k forks source link

New EVM Transport #14043

Closed TrejGun closed 4 days ago

TrejGun commented 4 days ago

Is there an existing issue that is already proposing this?

NestJS version

10.0.0

Is your performance suggestion related to a problem? Please describe it

We are excited to announce the public release of the new EVM Transport, which seamlessly integrates with EVM blockchains to listen for on-chain events and route them directly to your NestJS controllers.

Key Features:

  1. Event-driven Architecture: The transport is designed to subscribe to specific smart contract events on any EVM-compatible blockchain. It then emits these events directly to designated NestJS controllers, enabling real-time event handling and processing within your application.

  2. Easy Configuration: By extending the capabilities of @ethberry/nestjs-ethers, configuring the transport is straightforward. You can specify network details in your root module setup and configure the smart contracts’ events in specific modules.

  3. Scalable and Robust: The transport supports listening to multiple contracts simultaneously, ensuring that your application efficiently handles network requests. It supports both on-init and on-the-fly configurations that accommodate contract factories.

  4. Seamless Controller Integration: Events emitted from the blockchain are passed to NestJS controllers as if they were typical HTTP requests. This allows developers to leverage NestJS decorators and other built-in features, providing a familiar and intuitive development experience.

  5. Built-in Error Handling and Logging: The transport includes comprehensive error handling and logging, enabling developers to monitor and respond to issues quickly and effectively.

Example Usage

Here’s a quick example using @ethberry/nestjs-ethers to demonstrate how you can set up a listener for a blockchain event:

@Injectable()
class Erc20Service {
  public async transfer(
    event: ILogEvent<IERC20TransferEvent>,
    ctx: Log,
  ): Promise<void> {
    await Promise.resolve({ event, ctx });
  }
}

@Controller()
class Erc20Controller {
  constructor(private readonly erc20Service: Erc20Service) {}

  @EventPattern({
    contractType: "ERC20_TOKEN",
    eventName: "Transfer",
  })
  public transfer(@Payload() event: ILogEvent<IERC20TransferEvent>, @Ctx() ctx: Log): Promise<void> {
    return this.erc20Service.transfer(event, ctx);
  }
}

@Module({
  imports: [EthersModule.deferred()],
  providers: [Erc20Service],
  controllers: [Erc20Controller],
  exports: [Erc20Service],
})
class Erc20Module {}

@Module({
  imports: [
    ConfigModule.forRoot(),
    EthersModule.forRootAsync(EthersModule, {
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService): Promise<IModuleOptions> => {
        const latency = ~~configService.get<string>("LATENCY", "1");
        const fromBlock = ~~configService.get<string>("STARTING_BLOCK", "0");
        return Promise.resolve({
          latency,
          fromBlock,
          debug: true,
          cron: CronExpression.EVERY_5_SECONDS,
        });
      },
    }),
    Erc20Module,
  ],
})
export class AppModule {}

In this example, we are subscribing to the Transfer event of an ERC-20 contract. When a transfer occurs, the transport captures the event and triggers the corresponding handler in the controller.

Getting Started

To get started with the new transport, check out the documentation and example repository here. Integrate it into your existing NestJS projects to unlock new possibilities for building event-driven blockchain applications!

We look forward to seeing what you build with this new tool. Happy coding!

Describe the performance enhancement you are proposing and how we can try it out

ability to work directly with EVM chains

Benchmarks result or another proof (eg: POC)

https://github.com/ethberry/nestjs-ethers/

kamilmysliwiec commented 4 days ago

Thanks for sharing @TrejGun!