Skip to main content

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
}
}
});

Examples

The examples for different cases are available in web3 plugin repo