Installation and Setup of Matic.js

Intermediate

Installation and Setup of Matic.js

 

This tutorial is about installing the matic.js library, which is a javascript library that helps in interacting with the various components of the Matic Network. It explains how to install the Main library and the Ethereum library (Web3.js or Ethers.js) via npm, and how to set up and use each library to create a POS client, which helps to interact with the POS bridge. The tutorial provides examples of how to use the libraries to create an instance of the POS client and initialize it with the appropriate network and version.

Installation

maticjs has two parts –

  1. Main library

  2. Ethereum library

Main library

The Main library has the core logic and provides different APIs. The user interacts mostly with this library.

npm i @maticnetwork/maticjs

 

Ethereum library

Ethereum library allows us to use any favourite ether library. It is injected into maticjs using plugins.

matic.js supports two popular library –

  1. Web3.js

  2. Ethers

Web3.js

 

npm install @maticnetwork/maticjs-web3

 

ethers

npm install @maticnetwork/maticjs-ethers

 

Supported libraries – Setup

In order to support multiple web3 libraries – maticjs provides plugins.

Currently two libraries are supported –

  1. Web3.js

  2. Ethers

Web3.js

web3.js is a collection of libraries that allow you to interact with a local or remote ethereum node using HTTP, IPC or WebSocket.

Setup web3.js

web3.js support is available via seperate package as a plugin for matic.js.

Installation

npm install @maticnetwork/maticjs-web3

 

setup

import { use } from '@maticnetwork/maticjs'
import { Web3ClientPlugin } from '@maticnetwork/maticjs-web3'

// install web3 plugin
use(Web3ClientPlugin)

 

Let’s see an example of creating POSClient using web3 –

 
import { POSClient,use } from "@maticnetwork/maticjs"
import { Web3ClientPlugin } from '@maticnetwork/maticjs-web3'
import HDWalletProvider from "@truffle/hdwallet-provider"

// install web3 plugin
use(Web3ClientPlugin);

const posClient = new POSClient();
await posClient.init({
    network: 'testnet',
    version: 'mumbai',
    parent: {
      provider: new HDWalletProvider(privateKey, mainRPC),
      defaultConfig: {
        from : fromAddress
      }
    },
    child: {
      provider: new HDWalletProvider(privateKey, childRPC),
      defaultConfig: {
        from : fromAddress
      }
    }
});

 

Ether.js

The ethers.js library aims to be a complete and compact library for interacting with the Ethereum Blockchain and its ecosystem.

Setup ether.js

ether.js support is available via seperate package as a plugin for matic.js.

Installation

npm install @maticnetwork/maticjs-ethers

 

setup

import { use } from '@maticnetwork/maticjs'
import { Web3ClientPlugin } from '@maticnetwork/maticjs-ethers'

// install ethers plugin
use(Web3ClientPlugin)

 

Let’s see one example of creating POSClient using ethers –

import { POSClient,use } from "@maticnetwork/maticjs"
import { Web3ClientPlugin } from '@maticnetwork/maticjs-ethers'
import { providers, Wallet } from "ethers";


// install web3 plugin
use(Web3ClientPlugin);

const parentProvider = new providers.JsonRpcProvider(rpc.parent);
const childProvider = new providers.JsonRpcProvider(rpc.child);

const posClient = new POSClient();
await posClient.init({
    network: 'testnet',
    version: 'mumbai',
    parent: {
      provider: new Wallet(privateKey, parentProvider),
      defaultConfig: {
        from : fromAddress
      }
    },
    child: {
      provider: new Wallet(privateKey, childProvider),
      defaultConfig: {
        from : fromAddress
      }
    }
});