@moralisweb3/evm-connector-utils
TypeScript icon, indicating that this package has built-in type declarations

2.0.0-alpha.2 • Public • Published

@moralisweb3/evm-connector-utils

This is a shared library for all connectors.

🔧 How to Build Own Connector?

Install below dependencies to your TypeScript project.

yarn add @moralisweb3/core
yarn add @moralisweb3/evm-connector-utils

After this you may create a new connector class. Your class should extend EvmAbstractConnector class and:

  • set a connector name,
  • implement createProvider method, this method should return a valid EIP1193 provider,
  • implement createConnection method.
import {
  MoralisCore,
  EvmConnection,
  EvmProvider,
  EvmChain,
  EvmAddress,
  EvmBaseConnectOptions,
} from '@moralisweb3/core';
import { EvmAbstractConnector } from '@moralisweb3/evm-connector-utils';
import { MyProvider } from 'my-provider';

export interface MyConnectorConnectOptions extends EvmBaseConnectOptions {
  myParam: number;
  // ...
}

export class MyConnector extends EvmAbstractConnector<MyProvider, MyConnectorConnectOptions> {
  public constructor(core: MoralisCore) {
    super('connector-name', core);
  }

  protected async createProvider(options?: MyConnectorConnectOptions): Promise<MyProvider> {
    // ...
    return provider;
  }

  protected async createConnection(options?: MyConnectorConnectOptions): Promise<EvmConnection> {
    // DO NOT call createProvider() here, use getProvider() instead.
    const provider: EvmProvider = await this.getProvider();

    const chain: EvmChain | null = new EvmChain('ropsten');
    const account: EvmAddress | null = new EvmAddress('0x1234...');

    // ...

    return { provider, chain, account };
  }
}

👉 Check our implementations: EvmMetamaskConnector, EvmWalletconnectConnector.

Now you can register your connector:

import core from '@moralisweb3/core';

const myConnector = new MyConnector(core);
Moralis.Evm.connectors.register(myConnector);

And use it:

Moralis.Evm.connect('connector-name', {
  myParam: 0x0,
  /* ... */
});

Readme

Keywords

none

Package Sidebar

Install

npm i @moralisweb3/evm-connector-utils

Weekly Downloads

27

Version

2.0.0-alpha.2

License

MIT

Unpacked Size

79.8 kB

Total Files

11

Last publish

Collaborators

  • locothedev
  • ernow
  • cicdmoralis
  • y0moo
  • capplequoppe
  • gerardo15
  • ivanontech