npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-matic

v0.1.2

Published

Matic chain utilities for NestJS based on maticjs

Downloads

11

Readme

NestJS-Matic

npm CircleCI Coverage Status vulnerabilities supported platforms

Matic PoS and Plasma utilities for NestJS based on matic.js

Install

npm i nestjs-matic

Register module

Configuration params

nestjs-matic can be configured with this options:

interface MaticModuleOptions {
  /**
   * Optional parameter for connection, must be a MaticNetworks enum.
   * Take 'mainnet' or 'testnet' as possible values.
   * If no network is provided, mainnnet is used.
   * @see {@link https://maticnetwork.github.io/matic.js/docs/plasma/initialize#options}
   */
  network?: MaticNetworks;

  /**
   * Optional parameter for version, must be a MaticVersions enum.
   * Take 'v1' or 'mumbai' as possible values.
   * If no version is provided, v1 is used.
   * @see {@link https://maticnetwork.github.io/matic.js/docs/plasma/initialize#options}
   */
  version?: MaticVersions;

  /**
   * Optional parameter for maticProvider, can be a web3-core provider.
   * If no maticProvider is provided, empty object is used
   * The maticProvider may also be a URL to connect to,
   * such as https://rpc-mumbai.matic.today
   * @see {@link https://github.com/ChainSafe/web3.js/blob/1.x/packages/web3-core/types/index.d.ts#L436}
   */
  maticProvider?: web3-core.provider;

  /**
   * Required parameter for parentProvider, can be a web3-core provider.
   * @see {@link https://github.com/ChainSafe/web3.js/blob/1.x/packages/web3-core/types/index.d.ts#L436}
   */
  parentProvider: web3-core.provider;

  /**
   * Optional parameter for parentDefaultOptions, must be a Matic
   * SendOptions type.
   * If no parentDefaultOptions is provided, empty object is used.
   * @see {@link https://github.com/maticnetwork/matic.js/blob/master/src/types/Common.ts#L3}
   */
  parentDefaultOptions?: SendOptions;

  /**
   * Optional parameter for maticDefaultOptions, must be a Matic
   * SendOptions type.
   * If no parentDefaultOptions is provided, empty object is used.
   * @see {@link https://github.com/maticnetwork/matic.js/blob/master/src/types/Common.ts#L3}
   */
  maticDefaultOptions?: SendOptions;

  /**
   * Optional parameter for maticClient, must be a string.
   * Take 'Plasma' or 'PoS' as possible values.
   * If no maticClient is provided, 'PoS" is used.
   */
  maticClient?: MaticClients;
}

Synchronous configuration

Use MaticModule.forRoot method with Options interface:

import { MaticModule } from 'nestjs-matic';
import { MaticClients, MaticNetworks, MaticVersions } from 'nestjs-matic/dist/matic.interface';

@Module({
  imports: [
    MaticModule.forRoot({
      network: MaticNetworks.Testnet,
      version: MaticVersions.Mumbai
      maticProvider: 'https://rpc-mumbai.matic.today',
      parentProvider: 'https://rpc.goerli.mudit.blog',
      parentDefaultOptions: {
        from: '0x9aCd457193F4788Ef7989c88dA89DF7b2754670c',
      },
      maticDefaultOptions: {
        from: '0x54aff400858Dcac39797a81894D9920f16972D1D',
      },
      maticClient: MaticClients.PoS
    })
  ],
  ...
})
class MyModule {}

Asynchronous configuration

With MaticModule.forRootAsync allows you, for example, inject ConfigService to use it in Nest useFactory method.

useFactory should return object with Options interface.

import { MaticModule } from 'nestjs-matic';
import { MaticClients, MaticNetworks, MaticVersions } from 'nestjs-matic/dist/matic.interface';

@Injectable()
class ConfigService {
  public readonly network = MaticNetworks.Testnet;
  public readonly version = MaticVersions.Mumbai;
  public readonly maticProvider = 'https://rpc-mumbai.matic.today';
  public readonly parentProvider = 'https://rpc.goerli.mudit.blog';
  public readonly maticDefaultOptions = { from '0x....' };;
}

@Module({
  providers: [ConfigService],
  exports: [ConfigService]
})
class ConfigModule {}

@Module({
  imports: [
    MaticModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (config: ConfigService) => {
        await somePromise();

        return {
          network: config.network,
          version: config.version,
          maticProvider: config.maticProvider,
          parentProvider: config.parentProvider,
          maticDefaultOptions: config.maticDefaultOptions,
          parentDefaultOptions: config.maticDefaultOptions,
          maticClient: MaticClients.PoS,
        };
      },
    }),
  ],
  ...
})
class TestModule {}

Or you can just pass ConfigService to providers, if you don't have any ConfigModule:

import {
  MaticModule,
  MaticNetworks,
  MaticVersions,
} from 'nestjs-matic';

@Injectable()
class ConfigService {
  public readonly network = MaticNetworks.Testnet;
  public readonly version = MaticVersions.Mumbai;
  public readonly maticProvider = 'https://rpc-mumbai.matic.today';
  public readonly parentProvider = 'https://rpc.goerli.mudit.blog';
  public readonly maticDefaultOptions = { from '0x....' };
}

@Module({
  imports: [
    MaticModule.forRootAsync({
      providers: [ConfigService],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => {
        return {
          network: config.network,
          version: config.version,
          maticProvider: config.maticProvider,
          parentProvider: config.parentProvider,
          maticDefaultOptions: config.maticDefaultOptions,
          parentDefaultOptions: config.maticDefaultOptions,
          maticClient: MaticClients.PoS,
        };
      }
    })
  ],
  controllers: [TestController]
})
class MyModule {}

MaticClients

If you are familiar with it, you are ready to go.

import { InjectMaticProvider, MaticPOSClient } from 'nestjs-matic';

@Injectable()
export class TestService {
  constructor(
    @InjectMaticProvider()
    private readonly maticClient: MaticPOSClient,
  ) {}
  async someMethod(): Promise<{ nftBalance: string }> {
    const nftBalance: number = await this.maticClient.balanceOfERC721('0x....', '0x....')

    return { nftBalance: nftBalance.toString() };
  }
}

Testing a class that uses @InjectMaticProvider

This package exposes a getMaticToken() function that returns a prepared injection token based on the provided context. Using this token, you can easily provide a mock implementation of the SDKClient using any of the standard custom provider techniques, including useClass, useValue, and useFactory.

const module: TestingModule = await Test.createTestingModule({
  providers: [
    MyService,
    {
      provide: getMaticToken(),
      useValue: mockProvider,
    },
  ],
}).compile();

Change Log

See Changelog for more information.

Contributing

Contributions welcome! See Contributing.

Collaborators

License

Licensed under the Apache 2.0 - see the LICENSE file for details.