Connecting to the blockchain

How to connect to the Public Mint blockchain.

Integrating or building solely on the blockchain doesn't require permission from Public Mint or anybody else. You can connect to the Public Mint blockchain by remotely connecting to it and only takes a few steps.

Web3.js and other wrappers

Developing blockchain-based applications usually requires work on two domains:

  1. Smart contract development: code which gets deployed to the blockchain, written in Solidity in the case of Public Mint (and Ethereum);

  2. Web or app development: code deployed on the web as an interface to read and write data from the blockchain/smart contracts.

Even though you can connect your web app directly to a client using JSON-RPC calls, you can use a wrapper to make your life easier when trying to connect the two domains and create a full-fledged interface.

Example of diagram to illustrate stack

Web3.js is a collection of Javascript libraries that allow you to easily perform transactions on the blockchain. This includes read/write data, send money, and even deploy smart contracts!

We use Node.js, publicmint-web3.js, and similar Javascript-based libraries throughout this documentation. However, you are free to use any language/wrapper you want.

Getting started

Connecting to the blockchain takes just a few steps:

Step 1: Connect to a node

The Public Mint client is based on Hyperledger Besu, and likewise it includes a command line interface and JSON-RPC API for running and monitoring nodes.

Our client also supports smart contract deployment and operational tools (such as Remix, web3.js, or MyCrypto). Apart from a few key differences, the interface should be quite familiar to Ethereum developers.

In our examples we'll use publicmint-web3.js, this package is a umbrella class that is building above web3.js one of the most popular Javascript libraries for Ethereum. For installation instructions, go to publicmint-web3.js and web3.js .

Start by installing publicmint-web3.js. From the terminal, type:

$ mkdir myDappProject && cd $_
$ npm init .
$ npm install @publicmint/publicmint-web3
$ touch index.js

Inside index.js start by requiring web3:

import PublicMint from 'publicmint-web3';
// For JS es5 use:
const PublicMint = require('publicmint-web3').default;

This variable will now allow you to create a connection to a Public Mint node.

Next, it's time to connect. Depending on whether your are connecting to our TestNet (blockchain used by Public Mint Sandbox environment) or to our MainNet (Public Mint production environment), you should enter the following:

TestNet
MainNet
TestNet
const web3 = new PublicMint('testNet'); // or chainId 2019
MainNet
const web3 = new PublicMint(); // default 'mainNet' or chainId 2020

See more about providers here.

This step will create a normal web3 instance excluding modules not working for now (excluded modules), and create a new namespace web3.pm that contains all contracts and basic features for interface with PublicMint client.

Learn more about .pm modules.

You're almost done. Now it's time to test it.

Step 3 - Test your connection

To make sure everything went smoothly, let's query the blockchain for the latest block:

> web3.eth.getBlockNumber()
.then(console.log);
> 3141 // Most recent block number

Congratulations! You are connected to the Public Mint blockchain. Read on to go through examples of some of the operations you can perform.