@awaludinar/nestweb3
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

@awaludinar/nestweb3

Description

Web3.js module for Nest.

Installation

Install module

npm i --save @awaludinar/nestweb3 web3

Usage/Examples

Simple usage

Import module in any module

import { Module } from '@nestjs/common';
import { Web3Module } from '@awaludinar/nestweb3';

@Module({
  import: [Web3Module.forRoot('http://client-rpc-url.tld')],
})
export class AppModule {}

Use in service

import { Injectable } from '@nestjs/common';
import { InjectWeb3 } from '@awaludinar/nestweb3';
import Web3 from 'web3';

@Injectable()
export class WsService {
  constructor(@InjectWeb3() private web3: Web3) {}

  getBlockNumber() {
    return this.web3.eth.getBlockNumber();
  }
}

Multiple RPC

Import module in any module

import { Module } from '@nestjs/common';
import { Web3Module } from '@awaludinar/nestweb3';

@Module({
  import: [
    Web3Module.forRoot('http://client-rpc-url.tld', {
      connectionName: 'RPC1',
      provider: { name: 'HTTP' /* or WS */ },
    }),
    Web3Module.forRoot('wss://client-rpc-url.tld', {
      connectionName: 'RPC2',
      provider: { name: 'WS' /* or WS */ },
    }),
  ],
})
export class AppModule {}

Use in service

import { Injectable } from '@nestjs/common';
import { InjectWeb3 } from '@awaludinar/nestweb3';
import Web3 from 'web3';

@Injectable()
export class WsService {
  constructor(@InjectWeb3('RPC1') private web3: Web3) {}

  getBlockNumber() {
    return this.web3.eth.getBlockNumber();
  }
}

Async Configuration

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { Web3Module } from '@awaludinar/nestweb3';

@Module({
  import: [
    Web3Module.forRootAsync({
      imports: [ConfigModule],
      connectionName: 'RPC_ASYNC',
      useFactory: async (config: ConfigService) => ({
        clientUrl: config.get('CLIENT_WS_URL'),
        provider: { name: 'HTTP' /* or WS */ },
      }),
      inject: [ConfigService],
    }),
  ],
})
export class AnyModule {}

Package Sidebar

Install

npm i @awaludinar/nestweb3

Weekly Downloads

4

Version

1.1.0

License

MIT

Unpacked Size

17.4 kB

Total Files

21

Last publish

Collaborators

  • awaludinar