Skip to content

Latest commit

 

History

History
207 lines (163 loc) · 7.57 KB

File metadata and controls

207 lines (163 loc) · 7.57 KB

Consensus Layer API Module

NestJS Consensus Layer API Module for Lido Finance projects. Part of Lido NestJS Modules

Install

yarn add @lido-nestjs/consensus

Update types

The types used in the API methods are based on Eth2Spec. To update them use the script:

./generate.sh

Usage

This module depends on FetchModule from @lido-nestjs/fetch, so you need to provide it as a global module or import it into ConsensusModule.

// Import
import { Module } from '@nestjs/common';
import { ConsensusModule } from '@lido-nestjs/consensus';
import { FetchModule } from '@lido-nestjs/fetch';
import { MyService } from './my.service';

@Module({
  imports: [ConsensusModule.forFeature({ imports: [FetchModule] })],
  providers: [MyService],
  exports: [MyService],
})
export class MyModule {}

// Usage
import { ConsensusService } from '@lido-nestjs/consensus';

export class MyService {
  constructor(private consensusService: ConsensusService) {}

  async myMethod() {
    return await this.consensusService.getGenesis();
  }
}

Global usage

import { Module } from '@nestjs/common';
import { ConsensusModule } from '@lido-nestjs/consensus';
import { FetchModule } from '@lido-nestjs/fetch';

@Module({
  imports: [FetchModule.forRoot(), ConsensusModule.forRoot()],
})
export class MyModule {}

Async usage

import { Module } from '@nestjs/common';
import { ConsensusModule } from '@lido-nestjs/consensus';
import { FetchModule } from '@lido-nestjs/fetch';
import { ConfigModule, ConfigService } from './my.service';

@Module({
  imports: [
    ConfigModule.forRoot(),
    FetchModule.forRoot(),
    ConsensusModule.forRootAsync({
      async useFactory(configService: ConfigService) {
        return { pollingInterval: configService.pollingInterval };
      },
      inject: [ConfigService],
    }),
  ],
})
export class MyModule {}

Fetch options

Methods support fetch options:

// Usage
import { ConsensusService } from '@lido-nestjs/consensus';

export class MyService {
  constructor(private consensusService: ConsensusService) {}

  async myMethod() {
    return await this.consensusService.getGenesis({
      options: { headers: { 'x-header-foo': 'bar' } },
    });
  }
}

Subscription

Subscribe to head blocks:

import { ConsensusService } from '@lido-nestjs/consensus';

export class MyService {
  constructor(private consensusService: ConsensusService) {
    this.consensusService.subscribe((error, data) => {
      console.log(error, data);
    });
  }
}

Subscribe to finalized blocks:

import { ConsensusService } from '@lido-nestjs/consensus';

export class MyService {
  constructor(private consensusService: ConsensusService) {
    this.consensusService.subscribe(
      (error, data) => {
        console.log(error, data);
      },
      { blockId: 'finalized' },
    );
  }
}

Methods

Beacon

Config

Debug

Events

Node

Validator