{"id":14970712,"url":"https://github.com/blockcoders/nestjs-ethers","last_synced_at":"2025-10-06T00:00:15.066Z","repository":{"id":39710262,"uuid":"356714506","full_name":"blockcoders/nestjs-ethers","owner":"blockcoders","description":"Ethereum utilities for NestJS based on Ethers.js","archived":false,"fork":false,"pushed_at":"2024-03-28T18:51:45.000Z","size":1185,"stargazers_count":92,"open_issues_count":6,"forks_count":24,"subscribers_count":6,"default_branch":"main","last_synced_at":"2024-10-25T07:14:54.832Z","etag":null,"topics":["blockchain","ethereum","ethers","ethersjs","nest","nestjs"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/blockcoders.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-10T22:49:32.000Z","updated_at":"2024-10-23T00:25:13.000Z","dependencies_parsed_at":"2023-02-15T12:16:09.396Z","dependency_job_id":"ca204946-d232-4536-ab74-cfe10b252500","html_url":"https://github.com/blockcoders/nestjs-ethers","commit_stats":{"total_commits":78,"total_committers":7,"mean_commits":"11.142857142857142","dds":0.3076923076923077,"last_synced_commit":"cbcd89e38bff5e3aa090b1d825a20c94311a8aa5"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blockcoders%2Fnestjs-ethers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blockcoders%2Fnestjs-ethers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blockcoders%2Fnestjs-ethers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blockcoders%2Fnestjs-ethers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blockcoders","download_url":"https://codeload.github.com/blockcoders/nestjs-ethers/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248008630,"owners_count":21032556,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blockchain","ethereum","ethers","ethersjs","nest","nestjs"],"created_at":"2024-09-24T13:44:01.490Z","updated_at":"2025-10-06T00:00:15.058Z","avatar_url":"https://github.com/blockcoders.png","language":"TypeScript","readme":"NestJS-Ethers\n=============\n\n[![npm](https://img.shields.io/npm/v/nestjs-ethers)](https://www.npmjs.com/package/nestjs-ethers)\n[![CircleCI](https://circleci.com/gh/blockcoders/nestjs-ethers/tree/main.svg?style=svg)](https://circleci.com/gh/blockcoders/nestjs-ethers/tree/main)\n[![Coverage Status](https://coveralls.io/repos/github/blockcoders/nestjs-ethers/badge.svg?branch=main)](https://coveralls.io/github/blockcoders/nestjs-ethers?branch=main)\n[![vulnerabilities](https://badgen.net/snyk/blockcoders/nestjs-ethers)](https://snyk.io/test/github/blockcoders/nestjs-ethers)\n[![CodeQL](https://github.com/blockcoders/nestjs-ethers/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/blockcoders/nestjs-ethers/actions/workflows/codeql-analysis.yml)\n[![supported platforms](https://img.shields.io/badge/platforms-Express%20%26%20Fastify-green)](https://img.shields.io/badge/platforms-Express%20%26%20Fastify-green)\n\n\nEthereum utilities for NestJS based on [Ethers.js](https://github.com/ethers-io/ethers.js/)\n\n## Install\n\n```sh\nnpm i nestjs-ethers\n```\n\n## Register module\n\n### Zero configuration\n\nJust import `EthersModule` to your module:\n\n```ts\nimport { EthersModule } from 'nestjs-ethers';\n\n@Module({\n  imports: [EthersModule.forRoot()],\n  ...\n})\nclass MyModule {}\n```\n\n**NOTE:** *By default `EthersModule` will try to connect using [getDefaultProvider](https://docs.ethers.org/v6/api/providers/#getDefaultProvider). It's the safest, easiest way to begin developing on Ethereum. It creates a [FallbackProvider](https://docs.ethers.org/v6/api/providers/fallback-provider/) connected to as many backend services as possible.* \n\n### Configuration params\n\n`nestjs-ethers` can be configured with this options:\n\n```ts\ninterface EthersModuleOptions {\n  /**\n   * Optional parameter for connection, can be a Network object\n   * or the name of a common network as a string (e.g. \"homestead\")\n   * If no network is provided, homestead (i.e. mainnet) is used.\n   * The network may also be a URL to connect to,\n   * such as http://localhost:8545 or wss://example.com.\n   * @see {@link https://docs.ethers.org/v6/api/providers/#Networkish}\n   */\n  network?: Networkish;\n\n  /**\n   * Optional parameter for Alchemy API Token\n   * @see {@link https://alchemyapi.io}\n   */\n  alchemy?: string;\n\n  /**\n   * Optional parameter for Etherscan API Token\n   * @see {@link https://etherscan.io}\n   */\n  etherscan?: string;\n  \n  /**\n   * Optional parameter for Bscscan API Token\n   * @see {@link https://bscscan.com/}\n   */\n  bscscan?: string;\n\n  /**\n   * Optional parameter for use Cloudflare Provider\n   * @see {@link https://cloudflare-eth.com}\n   */\n  cloudflare?: boolean;\n\n  /**\n   * Optional parameter for Infura Project ID\n   * or InfuraProviderOptions(applicationId, applicationSecretKey)\n   * @see {@link https://infura.io}\n   */\n  infura?: InfuraProviderOptions | string;\n\n  /**\n   * Optional parameter for Pocket Network Application ID\n   * or PocketProviderOptions(projectId, projectSecret)\n   * @see {@link https://pokt.network}\n   */\n  pocket?: PocketProviderOptions | string;\n  \n  /**\n   * Optional parameter for Moralis API Token\n   * or MoralisProviderOptions(apiKey, region)\n   * @see {@link https://moralis.io/}\n   */\n  moralis?: MoralisProviderOptions | string;\n  \n  /**\n   * Optional parameter for Ankr API Token\n   * or AnkrProviderOptions(apiKey, projectSecret)\n   * @see {@link https://www.ankr.com/}\n   */\n  ankr?: AnkrProviderOptions | string;\n  \n  /**\n   * Optional parameter for a custom JsonRpcProvider\n   * You can connect using an URL, FetchRequest or an array of both.\n   * @see {@link https://docs.ethers.org/v6/api/providers/jsonrpc/}\n   * @ses {@link https://docs.ethers.org/v6/api/utils/fetching/#FetchRequest}\n   */\n  custom?: string | FetchRequest | (string | FetchRequest)[] \n\n  /**\n   * Optional parameter the number of backends that must agree\n   * (default: 2 for mainnet, 1 for testnets)\n   */\n  quorum?: number;\n\n  /**\n   * Optional parameter if this option is false, EthersModule will try to connect\n   * with the credentials provided in options. If you define more than one provider,\n   * EthersModule will use the FallbackProvider to send multiple requests simultaneously.\n   */\n  useDefaultProvider?: boolean;\n  \n  /**\n   * Optional parameter to associate a token name to EthersProvider,\n   * the token is used to request an instance of a class by the same name.\n   * This can be useful when you want multiple intances of EthersProvider.\n   */\n  token?: string;\n}\n```\n\n### Synchronous configuration\n\nUse `EthersModule.forRoot` method with [Options interface](#configuration-params):\n\n```ts\nimport { EthersModule, RINKEBY_NETWORK } from 'nestjs-ethers';\n\n@Module({\n  imports: [\n    EthersModule.forRoot({\n      network: RINKEBY_NETWORK,\n      alchemy: '845ce4ed0120d68eb5740c9160f08f98',\n      etherscan: 'e8cce313c1cfbd085f68be509451f1bab8',\n      cloudflare: true,\n      infura: {\n        projectId: 'd71b3d93c2fcfa7cab4924e63298575a',\n        projectSecret: 'ed6baa9f7a09877998a24394a12bf3dc',\n      },\n      pocket: {\n        applicationId: '9b0afc55221c429104d04ef9',\n        applicationSecretKey: 'b5e6d6a55426712a42a93f39555973fc',\n      },\n      quorum: 1,\n      useDefaultProvider: true,\n    })\n  ],\n  ...\n})\nclass MyModule {}\n```\n\n### Asynchronous configuration\n\nWith `EthersModule.forRootAsync` you can, for example, import your `ConfigModule` and inject `ConfigService` to use it in `useFactory` method.\n\n`useFactory` should return object with [Options interface](#configuration-params) or undefined\n\nHere's an example:\n\n```ts\nimport { EthersModule, RINKEBY_NETWORK } from 'nestjs-ethers';\n\n@Injectable()\nclass ConfigService {\n  public readonly infura = {\n    projectId: 'd71b3d93c2fcfa7cab4924e63298575a',\n    projectSecret: 'ed6baa9f7a09877998a24394a12bf3dc',\n  };\n}\n\n@Module({\n  providers: [ConfigService],\n  exports: [ConfigService]\n})\nclass ConfigModule {}\n\n@Module({\n  imports: [\n    EthersModule.forRootAsync({\n      imports: [ConfigModule],\n      inject: [ConfigService],\n      useFactory: async (config: ConfigService) =\u003e {\n        await somePromise();\n        return {\n          network: RINKEBY_NETWORK,\n          infura: config.infura,\n          useDefaultProvider: false,\n        };\n      }\n    })\n  ],\n  ...\n})\nclass TestModule {}\n```\n\nOr you can just pass `ConfigService` to `providers`, if you don't have any `ConfigModule`:\n\n```ts\nimport { EthersModule, RINKEBY_NETWORK } from 'nestjs-ethers';\n\n@Injectable()\nclass ConfigService {\n  public readonly pocket: {\n    applicationId: '9b0afc55221c429104d04ef9',\n    applicationSecretKey: 'b5e6d6a55426712a42a93f39555973fc',\n  };\n}\n\n@Module({\n  imports: [\n    EthersModule.forRootAsync({\n      providers: [ConfigService],\n      inject: [ConfigService],\n      useFactory: (config: ConfigService) =\u003e {\n        return {\n          network: RINKEBY_NETWORK,\n          pocket: config.pocket,\n          useDefaultProvider: false,\n        };\n      }\n    })\n  ],\n  controllers: [TestController]\n})\nclass TestModule {}\n```\n\nYou can also pass multiple `ethersjs` configs, if you want to use the `FallbackProvider` to send multiple requests simultaneously:\n\n```ts\nimport { EthersModule, RINKEBY_NETWORK } from 'nestjs-ethers';\n\n@Injectable()\nclass ConfigService {\n  public readonly infura = {\n    projectId: 'd71b3d93c2fcfa7cab4924e63298575a',\n    projectSecret: 'ed6baa9f7a09877998a24394a12bf3dc',\n  };\n  public readonly pocket: {\n    applicationId: '9b0afc55221c429104d04ef9',\n    applicationSecretKey: 'b5e6d6a55426712a42a93f39555973fc',\n  };\n}\n\n@Module({\n  providers: [ConfigService],\n  exports: [ConfigService]\n})\nclass ConfigModule {}\n\n@Module({\n  imports: [\n    EthersModule.forRootAsync({\n      imports: [ConfigModule],\n      inject: [ConfigService],\n      useFactory: async (config: ConfigService) =\u003e {\n        await somePromise();\n        return {\n          network: RINKEBY_NETWORK,\n          infura: config.infura,\n          pocket: config.pocket,\n          useDefaultProvider: false,\n        };\n      }\n    })\n  ],\n  ...\n})\nclass TestModule {}\n```\n\n## BaseProvider\n\n`BaseProvider` implements standard [Ether.js Provider](https://docs.ethers.org/v6/api/providers/#Provider). So if you are familiar with it, you are ready to go.\n\n```ts\nimport { InjectEthersProvider, BaseProvider } from 'nestjs-ethers';\n\n@Injectable()\nexport class TestService {\n  constructor(\n    @InjectEthersProvider()\n    private readonly ethersProvider: BaseProvider,\n  ) {}\n  async someMethod(): Promise\u003cNetwork\u003e {\n    return this.ethersProvider.getNetwork();\n  }\n}\n```\n\n## Binance Smart Chain Provider\n\nif you are familiar with [BscscanProvider](https://github.com/ethers-io/ancillary-bsc), you are ready to go.\n\n```ts\nimport {\n  EthersModule,\n  InjectEthersProvider,\n  BscscanProvider,\n  BINANCE_NETWORK\n} from 'nestjs-ethers';\n\n@Module({\n  imports: [\n    EthersModule.forRoot({\n      network: BINANCE_NETWORK,\n      bscscan: '845ce4ed0120d68eb5740c9160f08f98',\n      useDefaultProvider: false,\n    })\n  ],\n  ...\n})\nclass MyModule {}\n\n@Controller('/')\nclass TestController {\n  constructor(\n    @InjectEthersProvider()\n    private readonly bscProvider: BscscanProvider,\n  ) {}\n  @Get()\n  async get() {\n    const gasPrice: BigNumber = await this.bscProvider.getGasPrice()\n\n    return { gasPrice: gasPrice.toString() }\n  }\n}\n```\n\n### Binance Smart Chain Default Provider\n\nThis will create a `FallbackProvider`, backed by all popular Third-Party BSC services (currently only [BscscanProvider](https://github.com/ethers-io/ancillary-bsc)).\n\n**NOTE:** if `bscscan` is null or undefined. The BSC default provider will use the [community API Key](https://github.com/ethers-io/ancillary-bsc/blob/main/src.ts/bscscan-provider.ts#L8).\n\n```ts\nimport {\n  EthersModule,\n  InjectEthersProvider,\n  FallbackProvider,\n  BINANCE_NETWORK\n} from 'nestjs-ethers';\n\n@Module({\n  imports: [\n    EthersModule.forRoot({\n      network: BINANCE_NETWORK,\n      useDefaultProvider: true,\n    })\n  ],\n  ...\n})\nclass MyModule {}\n\n@Controller('/')\nclass TestController {\n  constructor(\n    @InjectEthersProvider()\n    private readonly bscProvider: FallbackProvider,\n  ) {}\n  @Get()\n  async get() {\n    const gasPrice: BigNumber = await this.bscProvider.getGasPrice()\n\n    return { gasPrice: gasPrice.toString() }\n  }\n}\n```\n\n## Custom JsonRpcProvider\n\nif you are familiar with [JsonRpcProvider](https://docs.ethers.org/v6/api/providers/jsonrpc/), you are ready to go. The custom provider is very helpful when you want to use a RPC that is not defined in [ethers](https://github.com/ethers-io/ethers.js/). This is the case for Binance Smart Chain public [RPCs](https://docs.binance.org/smart-chain/developer/rpc.html).\n\n```ts\nimport {\n  EthersModule,\n  InjectEthersProvider,\n  StaticJsonRpcProvider,\n  BNB_TESTNET_NETWORK\n} from 'nestjs-ethers';\n\n@Module({\n  imports: [\n    EthersModule.forRoot({\n      network: BNB_TESTNET_NETWORK,\n      custom: 'https://data-seed-prebsc-1-s1.binance.org:8545',\n      useDefaultProvider: false,\n    })\n  ],\n  ...\n})\nclass MyModule {}\n\n@Controller('/')\nclass TestController {\n  constructor(\n    @InjectEthersProvider()\n    private readonly customProvider: StaticJsonRpcProvider,\n  ) {}\n  @Get()\n  async get() {\n    const gasPrice: BigNumber = await this.customProvider.getGasPrice()\n\n    return { gasPrice: gasPrice.toString() }\n  }\n}\n```\n\nYou can also pass multiple `custom` providers, if you want to use the `FallbackProvider` to send multiple requests simultaneously:\n\n```ts\nimport {\n  EthersModule,\n  InjectEthersProvider,\n  FallbackProvider,\n  BNB_TESTNET_NETWORK\n} from 'nestjs-ethers';\n\n@Module({\n  imports: [\n    EthersModule.forRoot({\n      network: BNB_TESTNET_NETWORK,\n      custom: [\n        'https://data-seed-prebsc-1-s1.binance.org:8545',\n        'https://data-seed-prebsc-1-s3.binance.org:8545',\n        'https://data-seed-prebsc-2-s2.binance.org:8545'\n      ],\n      useDefaultProvider: false,\n    })\n  ],\n  ...\n})\nclass MyModule {}\n\n@Controller('/')\nclass TestController {\n  constructor(\n    @InjectEthersProvider()\n    private readonly customProvider: FallbackProvider,\n  ) {}\n  @Get()\n  async get() {\n    const gasPrice: BigNumber = await this.customProvider.getGasPrice()\n\n    return { gasPrice: gasPrice.toString() }\n  }\n}\n```\n\n## EthersSigner \n\n`EthersSigner` implements methods to create a [Wallet](https://docs.ethers.org/v6/api/wallet/#Wallet) or [VoidSigner](https://docs.ethers.org/v6/api/providers/abstract-signer/#VoidSigner). A `Signer` in ethers is an abstraction of an Ethereum Account, which can be used to sign messages and transactions and send signed transactions to the Ethereum Network. This service will also inject the `BaseProvider` into the wallet.\n\nCreate a `Wallet` from a private key:\n\n```ts\nimport { EthersSigner, InjectSignerProvider, Wallet } from 'nestjs-ethers';\n\n@Injectable()\nexport class TestService {\n  constructor(\n    @InjectSignerProvider()\n    private readonly ethersSigner: EthersSigner,\n  ) {}\n  async someMethod(): Promise\u003cstring\u003e {\n    const wallet: Wallet = this.ethersSigner.createWallet(\n      '0x4c94faa2c558a998d10ee8b2b9b8eb1fbcb8a6ac5fd085c6f95535604fc1bffb'\n    );\n\n    return wallet.getAddress();\n  }\n}\n```\n\nCreate a random [Wallet](https://docs.ethers.org/v6/api/wallet/#Wallet_createRandom):\n\n```ts\nimport { EthersSigner, InjectSignerProvider, Wallet } from 'nestjs-ethers';\n\n@Injectable()\nexport class TestService {\n  constructor(\n    @InjectSignerProvider()\n    private readonly ethersSigner: EthersSigner,\n  ) {}\n  async someMethod(): Promise\u003cstring\u003e {\n    const wallet: Wallet = this.ethersSigner.createRandomWallet();\n\n    return wallet.getAddress();\n  }\n}\n```\n\nCreate a [Wallet](https://docs.ethers.org/v6/api/wallet/#Wallet_fromEncryptedJson) from an encrypted JSON:\n\n```ts\nimport { EthersSigner, InjectSignerProvider, Wallet } from 'nestjs-ethers';\n\n@Injectable()\nexport class TestService {\n  constructor(\n    @InjectSignerProvider()\n    private readonly ethersSigner: EthersSigner,\n  ) {}\n  async someMethod(): Promise\u003cstring\u003e {\n    const wallet: Wallet = this.ethersSigner.createWalletFromEncryptedJson(\n      {\n        address: '012363d61bdc53d0290a0f25e9c89f8257550fb8',\n        id: '5ba8719b-faf9-49ec-8bca-21522e3d56dc',\n        version: 3,\n        Crypto: {\n          cipher: 'aes-128-ctr',\n          cipherparams: { iv: 'bc0473d60284d2d6994bb6793e916d06' },\n          ciphertext:\n            'e73ed0b0c53bcaea4516a15faba3f6d76dbe71b9b46a460ed7e04a68e0867dd7',\n          kdf: 'scrypt',\n          kdfparams: {\n            salt: '97f0b6e17c392f76a726ceea02bac98f17265f1aa5cf8f9ad1c2b56025bc4714',\n            n: 131072,\n            dklen: 32,\n            p: 1,\n            r: 8,\n          },\n          mac: 'ff4f2db7e7588f8dd41374d7b98dfd7746b554c0099a6c0765be7b1c7913e1f3',\n        },\n        'x-ethers': {\n          client: 'ethers.js',\n          gethFilename: 'UTC--2018-01-27T01-52-22.0Z--012363d61bdc53d0290a0f25e9c89f8257550fb8',\n          mnemonicCounter: '70224accc00e35328a010a19fef51121',\n          mnemonicCiphertext: 'cf835e13e4f90b190052263dbd24b020',\n          version: '0.1',\n        },\n      },\n      'password'\n    );\n\n    return wallet.getAddress();\n  }\n}\n```\n\nCreate a [Wallet](https://docs.ethers.org/v6/api/wallet/#Wallet_fromPhrase) from a mnemonic:\n\n```ts\nimport { EthersSigner, InjectSignerProvider, Wallet } from 'nestjs-ethers';\n\n@Injectable()\nexport class TestService {\n  constructor(\n    @InjectSignerProvider()\n    private readonly ethersSigner: EthersSigner,\n  ) {}\n  async someMethod(): Promise\u003cstring\u003e {\n    const wallet: Wallet = this.ethersSigner.createWalletfromMnemonic(\n      'service basket parent alcohol fault similar survey twelve hockey cloud walk panel'\n    );\n\n    return wallet.getAddress();\n  }\n}\n```\n\nCreate a [VoidSigner](https://docs.ethers.org/v6/api/providers/#Signer) from an address:\n\n```ts\nimport { EthersSigner, InjectSignerProvider, VoidSigner } from 'nestjs-ethers';\n\n@Injectable()\nexport class TestService {\n  constructor(\n    @InjectSignerProvider()\n    private readonly ethersSigner: EthersSigner,\n  ) {}\n  async someMethod(): Promise\u003cstring\u003e {\n    const wallet: VoidSigner = this.ethersSigner.createVoidSigner(\n      '0x012363d61bdc53d0290a0f25e9c89f8257550fb8'\n    );\n\n    return wallet.getAddress();\n  }\n}\n```\n\n## EthersContract\n\n`EthersContract` implements a method for the creation of a [Contract](https://docs.ethers.org/v6/api/contract/) instance. This service will also inject the a `BaseProvider` into the contract.\n\nCreate a `SmartContract` attached to an address:\n\n```ts\nimport { EthersContract, InjectContractProvider, Contract, Network } from 'nestjs-ethers';\nimport * as ABI from './utils/ABI.json';\n\n@Injectable()\nclass TestService {\n  constructor(\n    @InjectContractProvider()\n    private readonly ethersContract: EthersContract,\n  ) {}\n  async someMethod(): Promise\u003cNetwork\u003e {\n    const contract: Contract = this.ethersContract.create(\n      '0x012363d61bdc53d0290a0f25e9c89f8257550fb8',\n      ABI,\n    );\n\n    return contract.provider.getNetwork();\n  }\n}\n```\n\nCreate a [Contract](https://docs.ethers.org/v6/api/contract/) with a Wallet:\n\n```ts\nimport {\n  EthersContract,\n  EthersSigner,\n  InjectContractProvider,\n  InjectSignerProvider,\n  Contract,\n  Network,\n  Wallet\n} from 'nestjs-ethers';\nimport * as ABI from './utils/ABI.json';\n\n@Injectable()\nclass TestService {\n  constructor(\n    @InjectContractProvider()\n    private readonly ethersContract: EthersContract,\n    @InjectSignerProvider()\n    private readonly ethersSigner: EthersSigner,\n  ) {}\n  async someMethod(): Promise\u003cNetwork\u003e {\n    const wallet: Wallet = this.ethersSigner.createWallet(\n      '0x4c94faa2c558a998d10ee8b2b9b8eb1fbcb8a6ac5fd085c6f95535604fc1bffb'\n    );\n    const contract: Contract = this.ethersContract.create(\n      '0x012363d61bdc53d0290a0f25e9c89f8257550fb8',\n      ABI,\n      wallet,\n    );\n\n    return contract.signer.provider.getNetwork();\n  }\n}\n```\n\n## Multichain mode\n\nYou can use the `token` property to use multiple instances of Ethers. This can be helpful when you want to connect with more than one EVN compatible chain like `BSC`, `Polygon` or `Fantom`.\n\nIf you know what you're doing, you can enable it like so:\n\n### Synchronous\n\n```ts\nimport { Module, Controller, Get } from '@nestjs/common'\nimport {\n  EthersModule,\n  InjectEthersProvider,\n  InjectEthersProvider,\n  InjectEthersProvider,\n  PocketProvider,\n  AlchemyProvider,\n  StaticJsonRpcProvider,\n  BigNumber,\n  RINKEBY_NETWORK,\n  MUMBAI_NETWORK,\n  BNB_TESTNET_NETWORK,\n} from 'nestjs-ethers';\n\n@Controller('/')\nclass TestController {\n  constructor(\n    @InjectEthersProvider('eth')\n    private readonly pocketProvider: PocketProvider,\n    @InjectEthersProvider('poly')\n    private readonly alchemyProvider: AlchemyProvider,\n    @InjectEthersProvider('bsc')\n    private readonly customProvider: StaticJsonRpcProvider,\n  ) {}\n  @Get()\n  async get() {\n    const pocketGasPrice: BigNumber = await this.pocketProvider.getGasPrice()\n    const alchemyGasPrice: BigNumber = await this.alchemyProvider.getGasPrice()\n    const bscGasPrice: BigNumber = await this.customProvider.getGasPrice()\n\n    return {\n      pocketGasPrice: pocketGasPrice.toString(),\n      alchemyGasPrice: alchemyGasPrice.toString(),\n      bscGasPrice: bscGasPrice.toString(),\n    }\n  }\n}\n\n@Module({\n  imports: [\n    EthersModule.forRoot({\n      token: 'eth',\n      network: RINKEBY_NETWORK,\n      pocket: {\n        applicationId: '9b0afc55221c429104d04ef9',\n        applicationSecretKey: 'b5e6d6a55426712a42a93f39555973fc',\n      },\n      useDefaultProvider: false,\n    }),\n    EthersModule.forRoot({\n      token: 'poly',\n      network: MUMBAI_NETWORK,\n      alchemy: '845ce4ed0120d68eb5740c9160f08f98',\n      useDefaultProvider: false,\n    }),\n    EthersModule.forRoot({\n      token: 'bsc',\n      network: BNB_TESTNET_NETWORK,\n      custom: 'https://data-seed-prebsc-1-s1.binance.org:8545',\n      useDefaultProvider: false,\n    }),\n  ],\n  controllers: [TestController],\n})\nclass TestModule {}\n```\n\n### Asynchronous configuration\n\n```ts\nimport { Module, Controller, Get } from '@nestjs/common'\nimport {\n  EthersModule,\n  InjectEthersProvider,\n  InjectEthersProvider,\n  InjectEthersProvider,\n  PocketProvider,\n  AlchemyProvider,\n  StaticJsonRpcProvider,\n  BigNumber,\n  RINKEBY_NETWORK,\n  MUMBAI_NETWORK,\n  BNB_TESTNET_NETWORK,\n} from 'nestjs-ethers';\n\n@Controller('/')\nclass TestController {\n  constructor(\n    @InjectEthersProvider('eth')\n    private readonly pocketProvider: PocketProvider,\n    @InjectEthersProvider('poly')\n    private readonly alchemyProvider: AlchemyProvider,\n    @InjectEthersProvider('bsc')\n    private readonly customProvider: StaticJsonRpcProvider,\n  ) {}\n  @Get()\n  async get() {\n    const pocketGasPrice: BigNumber = await this.pocketProvider.getGasPrice()\n    const alchemyGasPrice: BigNumber = await this.alchemyProvider.getGasPrice()\n    const bscGasPrice: BigNumber = await this.customProvider.getGasPrice()\n\n    return {\n      pocketGasPrice: pocketGasPrice.toString(),\n      alchemyGasPrice: alchemyGasPrice.toString(),\n      bscGasPrice: bscGasPrice.toString(),\n    }\n  }\n}\n\n@Injectable()\nclass ConfigService {\n  public readonly applicationId: '9b0afc55221c429104d04ef9'\n  public readonly applicationSecretKey: 'b5e6d6a55426712a42a93f39555973fc'\n  public readonly alchemy: '845ce4ed0120d68eb5740c9160f08f98'\n  public readonly custom: 'https://data-seed-prebsc-1-s1.binance.org:8545'\n}\n\n@Module({\n  providers: [ConfigService],\n  exports: [ConfigService],\n})\nclass ConfigModule {}\n\n@Module({\n  imports: [\n    EthersModule.forRootAsync({\n      imports: [ConfigModule],\n      inject: [ConfigService],\n      token: 'eth',\n      useFactory: (config: ConfigService) =\u003e {\n        return {\n          network: RINKEBY_NETWORK,\n          pocket: {\n            applicationId: config.applicationId,\n            applicationSecretKey: config.applicationSecretKey,\n          },\n          useDefaultProvider: false,\n        }\n      },\n    }),\n    EthersModule.forRootAsync({\n      imports: [ConfigModule],\n      inject: [ConfigService],\n      token: 'poly',\n      useFactory: (config: ConfigService) =\u003e {\n        return {\n          network: MUMBAI_NETWORK,\n          alchemy: config.alchemy,\n          useDefaultProvider: false,\n        }\n      },\n    }),\n    EthersModule.forRootAsync({\n      imports: [ConfigModule],\n      inject: [ConfigService],\n      token: 'bsc',\n      useFactory: (config: ConfigService) =\u003e {\n        return {\n          network: BNB_TESTNET_NETWORK,\n          custom: config.custom,\n          useDefaultProvider: false,\n        }\n      },\n    }),\n  ],\n  controllers: [TestController],\n})\nclass TestModule {}\n```\n\n## Testing a class that uses InjectEthersProvider\n\nThis package exposes a getEthersToken(token?: string) function that returns a prepared injection token based on the provided context. \nUsing this token, you can easily provide a mock implementation of the `BaseProvider` using any of the standard custom provider techniques, including useClass, useValue, and useFactory.\n\n```ts\n  const module: TestingModule = await Test.createTestingModule({\n    providers: [\n      MyService,\n      {\n        provide: getEthersToken(MyService.name),\n        useValue: mockProvider,\n      },\n    ],\n  }).compile();\n```\n\n## Testing a class that uses InjectContractProvider\n\nThis package exposes a getContractToken(token?: string) function that returns a prepared injection token based on the contract provided context. \nUsing this token, you can easily provide a mock implementation of the `ethers.Contract` using any of the standard custom provider techniques, including useClass, useValue, and useFactory.\n\n```ts\n  const module: TestingModule = await Test.createTestingModule({\n    providers: [\n      MyService,\n      {\n        provide: getContractToken(MyService.name),\n        useValue: mockContractProvider,\n      },\n    ],\n  }).compile();\n```\n\n## Testing a class that uses InjectSignerProvider\n\nThis package exposes a getSignerToken(token?: string) function that returns a prepared injection token based on the signer provided context. \nUsing this token, you can easily provide a mock implementation of the `ethers.Signer` using any of the standard custom provider techniques, including useClass, useValue, and useFactory.\n\n```ts\n  const module: TestingModule = await Test.createTestingModule({\n    providers: [\n      MyService,\n      {\n        provide: getSignerToken(MyService.name),\n        useValue: mockSignerProvider,\n      },\n    ],\n  }).compile();\n```\n\n## Migration\n\n### v1\n\n- `SmartContract` was renamed to `Contract`\n- `EthersBaseProvider` was renamed to `BaseProvider`\n- `SmartContractInterface` was renamed to `ContractInterface`\n- `WalletSigner` was renamed to `Wallet`\n- `SmartContractFactory` was renamed to `ContractFactory`\n- `RandomWalletSignerOptions` was renamed to `RandomWalletOptions`\n\nA new more convenient way to inject the `EthersSigner` and `EthersContract` providers into a service or controller was add it.\n\n#### EthersSigner\n\n```ts\n// v0\nimport { EthersSigner, WalletSigner } from 'nestjs-ethers';\n\n@Injectable()\nexport class TestService {\n  constructor(private readonly ethersSigner: EthersSigner) {}\n  async someMethod(): Promise\u003cstring\u003e {\n    const wallet: WalletSigner = this.ethersSigner.createWallet(\n      '0x4c94faa2c558a998d10ee8b2b9b8eb1fbcb8a6ac5fd085c6f95535604fc1bffb'\n    );\n\n    return wallet.getAddress();\n  }\n}\n\n// v1\nimport { EthersSigner, InjectSignerProvider, Wallet } from 'nestjs-ethers';\n\n@Injectable()\nexport class TestService {\n  constructor(\n    @InjectSignerProvider()\n    private readonly ethersSigner: EthersSigner,\n  ) {}\n  async someMethod(): Promise\u003cstring\u003e {\n    const wallet: Wallet = this.ethersSigner\n    .createWallet(\n      '0x4c94faa2c558a998d10ee8b2b9b8eb1fbcb8a6ac5fd085c6f95535604fc1bffb'\n    );\n\n    return wallet.getAddress();\n  }\n}\n```\n\n#### EthersContract\n\n```ts\n// v0\nimport { EthersContract, SmartContract } from 'nestjs-ethers';\nimport * as ABI from './utils/ABI.json';\n\n@Injectable()\nclass TestService {\n  constructor(private readonly ethersContract: EthersContract) {}\n  async someMethod(): Promise\u003cstring\u003e {\n    const contract: SmartContract = this.ethersContract.create(\n      '0x012363d61bdc53d0290a0f25e9c89f8257550fb8',\n      ABI,\n    );\n\n    return contract.provider.getNetwork();\n  }\n}\n\n// v1\nimport { EthersContract, InjectContractProvider, Contract, Network } from 'nestjs-ethers';\nimport * as ABI from './utils/ABI.json';\n\n@Injectable()\nclass TestService {\n  constructor(\n    @InjectContractProvider()\n    private readonly contract: EthersContract,\n  ) {}\n  async someMethod(): Promise\u003cNetwork\u003e {\n    const contract: Contract = this.ethersContract.create(\n      '0x012363d61bdc53d0290a0f25e9c89f8257550fb8',\n      ABI,\n    );\n\n    return contract.provider.getNetwork();\n  }\n}\n```\n\n### v3\n\n- Migrated to ethers.js v6 from v5, introduced breaking changes, see the [migration guide](https://docs.ethers.org/v6/migrating/#migrating) for comprehensive list of changes\n- `BigNumber` was replaced with the standard `bigint`\n- `StaticJsonRpcProvider` was merged to `JsonRpcProvider` with `staticNetwork` option.\n\n## Change Log\n\nSee [Changelog](CHANGELOG.md) for more information.\n\n## Contributing\n\nContributions welcome! See [Contributing](CONTRIBUTING.md).\n\n## Collaborators\n\n* [__Jose Ramirez__](https://github.com/0xslipk)\n\n## License\n\nLicensed under the Apache 2.0 - see the [LICENSE](LICENSE) file for details.\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblockcoders%2Fnestjs-ethers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblockcoders%2Fnestjs-ethers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblockcoders%2Fnestjs-ethers/lists"}