{"id":19319669,"url":"https://github.com/blockcoders/nestjs-matic","last_synced_at":"2025-02-24T05:13:27.028Z","repository":{"id":47182496,"uuid":"358369396","full_name":"blockcoders/nestjs-matic","owner":"blockcoders","description":"Matic PoS and Plasma utilities for NestJS based on matic.js","archived":false,"fork":false,"pushed_at":"2022-03-24T14:05:37.000Z","size":648,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-02-07T12:44:33.557Z","etag":null,"topics":["blockchain","matic","maticjs","nest","nestjs","polygon","polygonjs"],"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}},"created_at":"2021-04-15T19:24:26.000Z","updated_at":"2022-03-24T14:05:41.000Z","dependencies_parsed_at":"2022-08-20T16:10:07.666Z","dependency_job_id":null,"html_url":"https://github.com/blockcoders/nestjs-matic","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blockcoders%2Fnestjs-matic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blockcoders%2Fnestjs-matic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blockcoders%2Fnestjs-matic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/blockcoders%2Fnestjs-matic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/blockcoders","download_url":"https://codeload.github.com/blockcoders/nestjs-matic/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240420986,"owners_count":19798502,"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","matic","maticjs","nest","nestjs","polygon","polygonjs"],"created_at":"2024-11-10T01:24:46.353Z","updated_at":"2025-02-24T05:13:26.993Z","avatar_url":"https://github.com/blockcoders.png","language":"TypeScript","readme":"# NestJS-Matic\n\n[![npm](https://img.shields.io/npm/v/nestjs-matic)](https://www.npmjs.com/package/nestjs-matic)\n[![CircleCI](https://circleci.com/gh/blockcoders/nestjs-matic/tree/main.svg?style=svg)](https://circleci.com/gh/blockcoders/nestjs-matic/tree/main)\n[![Coverage Status](https://coveralls.io/repos/github/blockcoders/nestjs-matic/badge.svg?branch=main)](https://coveralls.io/github/blockcoders/nestjs-matic?branch=main)\n[![vulnerabilities](https://img.shields.io/snyk/vulnerabilities/npm/nestjs-matic)](https://snyk.io/test/github/blockcoders/nestjs-matic)\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\nMatic PoS and Plasma utilities for NestJS based on [matic.js](https://github.com/maticnetwork/matic.js/)\n\n## Install\n\n```sh\nnpm i nestjs-matic\n```\n\n## Register module\n\n### Configuration params\n\n`nestjs-matic` can be configured with this options:\n\n```ts\ninterface MaticModuleOptions {\n  /**\n   * Optional parameter for connection, must be a MaticNetworks enum.\n   * Take 'mainnet' or 'testnet' as possible values.\n   * If no network is provided, mainnnet is used.\n   * @see {@link https://maticnetwork.github.io/matic.js/docs/plasma/initialize#options}\n   */\n  network?: MaticNetworks;\n\n  /**\n   * Optional parameter for version, must be a MaticVersions enum.\n   * Take 'v1' or 'mumbai' as possible values.\n   * If no version is provided, v1 is used.\n   * @see {@link https://maticnetwork.github.io/matic.js/docs/plasma/initialize#options}\n   */\n  version?: MaticVersions;\n\n  /**\n   * Optional parameter for maticProvider, can be a web3-core provider.\n   * If no maticProvider is provided, empty object is used\n   * The maticProvider may also be a URL to connect to,\n   * such as https://rpc-mumbai.matic.today\n   * @see {@link https://github.com/ChainSafe/web3.js/blob/1.x/packages/web3-core/types/index.d.ts#L436}\n   */\n  maticProvider?: web3-core.provider;\n\n  /**\n   * Required parameter for parentProvider, can be a web3-core provider.\n   * @see {@link https://github.com/ChainSafe/web3.js/blob/1.x/packages/web3-core/types/index.d.ts#L436}\n   */\n  parentProvider: web3-core.provider;\n\n  /**\n   * Optional parameter for parentDefaultOptions, must be a Matic\n   * SendOptions type.\n   * If no parentDefaultOptions is provided, empty object is used.\n   * @see {@link https://github.com/maticnetwork/matic.js/blob/master/src/types/Common.ts#L3}\n   */\n  parentDefaultOptions?: SendOptions;\n\n  /**\n   * Optional parameter for maticDefaultOptions, must be a Matic\n   * SendOptions type.\n   * If no parentDefaultOptions is provided, empty object is used.\n   * @see {@link https://github.com/maticnetwork/matic.js/blob/master/src/types/Common.ts#L3}\n   */\n  maticDefaultOptions?: SendOptions;\n\n  /**\n   * Optional parameter for maticClient, must be a string.\n   * Take 'Plasma' or 'PoS' as possible values.\n   * If no maticClient is provided, 'PoS\" is used.\n   */\n  maticClient?: MaticClients;\n}\n```\n\n### Synchronous configuration\n\nUse `MaticModule.forRoot` method with [Options interface](#configuration-params):\n\n```ts\nimport { MaticModule } from 'nestjs-matic';\nimport { MaticClients, MaticNetworks, MaticVersions } from 'nestjs-matic/dist/matic.interface';\n\n@Module({\n  imports: [\n    MaticModule.forRoot({\n      network: MaticNetworks.Testnet,\n      version: MaticVersions.Mumbai\n      maticProvider: 'https://rpc-mumbai.matic.today',\n      parentProvider: 'https://rpc.goerli.mudit.blog',\n      parentDefaultOptions: {\n        from: '0x9aCd457193F4788Ef7989c88dA89DF7b2754670c',\n      },\n      maticDefaultOptions: {\n        from: '0x54aff400858Dcac39797a81894D9920f16972D1D',\n      },\n      maticClient: MaticClients.PoS\n    })\n  ],\n  ...\n})\nclass MyModule {}\n```\n\n### Asynchronous configuration\n\nWith `MaticModule.forRootAsync` allows you, for example, inject `ConfigService` to use it in Nest `useFactory` method.\n\n`useFactory` should return object with [Options interface](#configuration-params).\n\n```ts\nimport { MaticModule } from 'nestjs-matic';\nimport { MaticClients, MaticNetworks, MaticVersions } from 'nestjs-matic/dist/matic.interface';\n\n@Injectable()\nclass ConfigService {\n  public readonly network = MaticNetworks.Testnet;\n  public readonly version = MaticVersions.Mumbai;\n  public readonly maticProvider = 'https://rpc-mumbai.matic.today';\n  public readonly parentProvider = 'https://rpc.goerli.mudit.blog';\n  public readonly maticDefaultOptions = { from '0x....' };;\n}\n\n@Module({\n  providers: [ConfigService],\n  exports: [ConfigService]\n})\nclass ConfigModule {}\n\n@Module({\n  imports: [\n    MaticModule.forRootAsync({\n      imports: [ConfigModule],\n      inject: [ConfigService],\n      useFactory: async (config: ConfigService) =\u003e {\n        await somePromise();\n\n        return {\n          network: config.network,\n          version: config.version,\n          maticProvider: config.maticProvider,\n          parentProvider: config.parentProvider,\n          maticDefaultOptions: config.maticDefaultOptions,\n          parentDefaultOptions: config.maticDefaultOptions,\n          maticClient: MaticClients.PoS,\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 {\n  MaticModule,\n  MaticNetworks,\n  MaticVersions,\n} from 'nestjs-matic';\n\n@Injectable()\nclass ConfigService {\n  public readonly network = MaticNetworks.Testnet;\n  public readonly version = MaticVersions.Mumbai;\n  public readonly maticProvider = 'https://rpc-mumbai.matic.today';\n  public readonly parentProvider = 'https://rpc.goerli.mudit.blog';\n  public readonly maticDefaultOptions = { from '0x....' };\n}\n\n@Module({\n  imports: [\n    MaticModule.forRootAsync({\n      providers: [ConfigService],\n      inject: [ConfigService],\n      useFactory: (config: ConfigService) =\u003e {\n        return {\n          network: config.network,\n          version: config.version,\n          maticProvider: config.maticProvider,\n          parentProvider: config.parentProvider,\n          maticDefaultOptions: config.maticDefaultOptions,\n          parentDefaultOptions: config.maticDefaultOptions,\n          maticClient: MaticClients.PoS,\n        };\n      }\n    })\n  ],\n  controllers: [TestController]\n})\nclass MyModule {}\n```\n\n## MaticClients\n\n-  `MaticPlasmaClient` implements standard [PlasmaClient](https://maticnetwork.github.io/matic.js/docs/plasma/initialize).\n-  `MaticPOSClient` implements standard [POSClient](https://maticnetwork.github.io/matic.js/docs/pos/initialize).\n\nIf you are familiar with it, you are ready to go.\n\n```ts\nimport { InjectMaticProvider, MaticPOSClient } from 'nestjs-matic';\n\n@Injectable()\nexport class TestService {\n  constructor(\n    @InjectMaticProvider()\n    private readonly maticClient: MaticPOSClient,\n  ) {}\n  async someMethod(): Promise\u003c{ nftBalance: string }\u003e {\n    const nftBalance: number = await this.maticClient.balanceOfERC721('0x....', '0x....')\n\n    return { nftBalance: nftBalance.toString() };\n  }\n}\n```\n\n## Testing a class that uses @InjectMaticProvider\n\nThis package exposes a `getMaticToken()` function that returns a prepared injection token based on the provided context.\nUsing this token, you can easily provide a mock implementation of the [SDKClient](https://github.com/maticnetwork/matic.js/blob/master/src/common/SDKClient.ts) using any of the standard custom provider techniques, including useClass, useValue, and useFactory.\n\n```ts\nconst module: TestingModule = await Test.createTestingModule({\n  providers: [\n    MyService,\n    {\n      provide: getMaticToken(),\n      useValue: mockProvider,\n    },\n  ],\n}).compile();\n```\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* [__Ana Riera__](https://github.com/AnnRiera)\n\n## License\n\nLicensed under the Apache 2.0 - see the [LICENSE](LICENSE) file for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblockcoders%2Fnestjs-matic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fblockcoders%2Fnestjs-matic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fblockcoders%2Fnestjs-matic/lists"}