Home > Mobile >  Nestjs - Can't resolve dependencies of service
Nestjs - Can't resolve dependencies of service

Time:10-29

I don't know how to fix this issue. I keept trying but can't get my head arround it.

The log throws me this Error: Nest can't resolve dependencies of the ERC20Service (?). Please make sure that the argument dependency at index [0] is available in the ERC20Module context.

import { DynamicModule, Module, Provider } from '@nestjs/common';
import { ERC20Service } from './erc20.service';
const Web3 = require("web3");

export interface ERC20ModuleOptions {
  global?: boolean;
  abi: Object,
  wsEndpoint: string,
  httpEndpoint: string
}

export const WEB3_HTTP_TOKEN = 'WEB3_HTTP_TOKEN';
export const WEB3_WS_TOKEN = 'WEB3_WS_TOKEN'


export class ERC20Module {
  static forRoot(options: ERC20ModuleOptions): DynamicModule {
  
    const httpProvider: Provider = {
      provide: WEB3_HTTP_TOKEN,
      useValue:new Web3(new Web3.providers.HttpProvider(options.httpEndpoint))
    };
    const wsProvider: Provider = {
        provide: WEB3_WS_TOKEN,
        useValue:new Web3(new Web3.providers.WebsocketProvider(options.wsEndpoint))
      };

    return {
      module: ERC20Module,
      providers: [httpProvider, wsProvider, ERC20Service],
    
      exports: [ERC20Service],
      global: options.global,
    };
  }
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

import Web3 from "web3"
import { Inject, Injectable } from '@nestjs/common';
import { WEB3_HTTP_TOKEN, WEB3_WS_TOKEN } from './erc20.module';

@Injectable()
export class ERC20Service {
    constructor(@Inject(WEB3_HTTP_TOKEN) private http: Web3,@Inject(WEB3_WS_TOKEN) private ws: Web3) {
    
    }
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule } from '@nestjs/config';
import { ERC20Module } from 'src/modules/erc20/erc20.module';
import { abi } from 'src/config/abi';

@Module({
  imports: [
    ConfigModule.forRoot(),
    ERC20Module.forRoot({
      global: true,
      httpEndpoint: `https://${process.env.CHAINSTACK_USER}:${process.env.CHAINSTACK_PASSWORD}@${process.env.CHAINSTACK_HTTP_ENDPOINT}`,
      wsEndpoint: `wss://${process.env.CHAINSTACK_USER}:${process.env.CHAINSTACK_PASSWORD}@${process.env.CHAINSTACK_WS_ENDPOINT}`,
      abi: abi,
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

I must be blind because as far as I see it, I am doing everything the correct way when in comes to injecting, exporting and importing. Maybe someone on here can guide me along the way to solve this issue. Thank you in advance

CodePudding user response:

Your erc20.service and erc20.module files import from each other, creating a circular dependency. Move the constants to a separate file and you should be good to go

// erc20.constants.ts
export const WEB3_HTTP_TOKEN = 'WEB3_HTTP_TOKEN';
export const WEB3_WS_TOKEN = 'WEB3_WS_TOKEN'
// erc20.module.ts
import { DynamicModule, Module, Provider } from '@nestjs/common';
import { WEB3_HTTP_TOKEN, WEB3_WS_TOKEN } from './erc20.constants';
import { ERC20Service } from './erc20.service';
const Web3 = require("web3");

export interface ERC20ModuleOptions {
  global?: boolean;
  abi: Object,
  wsEndpoint: string,
  httpEndpoint: string
}

export class ERC20Module {
  static forRoot(options: ERC20ModuleOptions): DynamicModule {
  
    const httpProvider: Provider = {
      provide: WEB3_HTTP_TOKEN,
      useValue:new Web3(new Web3.providers.HttpProvider(options.httpEndpoint))
    };
    const wsProvider: Provider = {
        provide: WEB3_WS_TOKEN,
        useValue:new Web3(new Web3.providers.WebsocketProvider(options.wsEndpoint))
      };

    return {
      module: ERC20Module,
      providers: [httpProvider, wsProvider, ERC20Service],
    
      exports: [ERC20Service],
      global: options.global,
    };
  }
}
// erc20.service.ts
import Web3 from "web3"
import { Inject, Injectable } from '@nestjs/common';
import { WEB3_HTTP_TOKEN, WEB3_WS_TOKEN } from './erc20.constants';

@Injectable()
export class ERC20Service {
    constructor(@Inject(WEB3_HTTP_TOKEN) private http: Web3,@Inject(WEB3_WS_TOKEN) private ws: Web3) {
    
    }
}
  • Related