Overview

The path toward adoption of easily accessible, decentralized infrastructure is a pivotal direction for the blockchain industry. Pocket Network provides a trustless API Layer, allowing easy access to any blockchain. By aggregating supply and demand through a decentralized network, Pocket creates a robust open source infrastructure layer, free from a central point of failure. We are excited to announce full support for node infrastructure provided by Pocket Network and will continue to promote the shift toward decentralized infrastructure provision throughout the blockchain industry.

Today, we will go through integrating Pocket Core MVP with the TerminalSDK to surface logging and analytics on the Pocket provider to Terminal. To begin, we will assume that you already have a Pocket Dev ID. If not you can sign up for one here. You’ll have to wait for your application to be reviewed, but the Pocket team does a really great job about quickly responding to requests for IDs. Once you have a basic dapp running, surfacing logs with the TerminalSDK is easy and straightforward. If you are unfamiliar with TerminalSDK you can check out the docs and npm page.

For this tutorial we’re going to use a simple Javascript file that retrieves the balance of an address as well as the transaction details of a transaction. The full code for the script is available at the bottom of the page and feel free to substitute in any web3 methods during the tutorial. Let’s start with a sample of what your code with an integrated pocket provider might look like before wrapping the TerminalSDK:

const PocketProvider = require("pocket-js-web3-provider");
const Web3 = require("web3");
 
var pocketProvider = new PocketProvider("ETH", "1", "POCKET_DEV_ID", {
  // This is just a stub, only gets called when eth_sendRawTransaction or eth_sendTransaction are called.
  transactionSigner: {
    hasAddress: async function(address) {
      return false;
    },
    signTransaction: async function(txParams) {
      console.log(txParams);
      return "0x";
    }
  },
  maxNodes: 3,
  requestTimeOut: 20000,
  sslOnly: false
});
 
const web3 = new Web3(pocketProvider);
// MAKE YOUR WEB3 CALLS HERE

The first step to begin surfacing logs is to signup for a Terminal account here. You will need to generate an API key as well as create a new project and find the projectId in project settings.

Once your account is created install the Terminal SDK with the following command:

npm install @terminal-packages/sdk

If you don't already have them installed, you will need to install Web3 as well as Pocket Provider like this:

npm install --save pocket-js-web3-provider
npm install web3

Now that you have the TerminalSDK installed, import the sdk to your project like this:

const sdk from '@terminal-packages/sdk';

From here all you have to do is wrap the initialization of your web3 instance like this:

const web3Instance = new Web3(
  new TerminalHttpProvider({
    apiKey: "YOUR_TERMINAL_API_KEY",
    Source: sdk.SourceType.Pocket,
    customHttpProvider: pocketProvider,
    projectId: "YOUR_TERMINAL_PROJECTID",
    web3Version: Web3Versions.one
  })
); 

Once you’ve done this, you can simply run the script from your command line using node.js. As soon as you run the script, the logs will get pushed to your Terminal Logging Platform and look something like the picture below. Now that you are receiving logs directly to your Terminal account, you can sort through your logs using an array of different filters.

If you’re having trouble getting this to work, please feel free to message the Terminal Support team for help on our website using messaging icon in the bottom right. You can also email us at support@terminal.co

If your project uses other infrastructure or wallet providers, the logs will be surfaceable as well. Terminal offers a host of other monitoring services to accommodate all of your web3 needs. Check out the docs here for information on integrating TerminalSDK with other sources.

Below is the full code for the project. Paste in your corresponding endpoint and keys and you should be good to go.

const PocketProvider = require("pocket-js-web3-provider");
const Web3 = require("web3");
const sdk = require("@terminal-packages/sdk");
 
var pocketProvider = new PocketProvider("ETH", "1", "POCKET_DEV_ID", {
  // This is just a stub, only gets called when eth_sendRawTransaction or eth_sendTransaction are called.
  transactionSigner: {
    hasAddress: async function(address) {
      return false;
    },
    signTransaction: async function(txParams) {
      console.log(txParams);
      return "0x";
    }
  },
  maxNodes: 3,
  requestTimeOut: 20000,
  sslOnly: false
});
 
const web3 = new Web3(
  new sdk.TerminalHttpProvider({
    apiKey: "TERMINAL_API_KEY",
    // projectId is not required to log but we suggest
    // using it for the best experience
    projectId: "TERMINAL_PROJECT_ID",
    // source can be any static or dynamic string as well
    source: sdk.SourceType.Pocket, //
    customHttpProvider: pocketProvider, // set the provider here
    web3Version: sdk.Web3Versions.one // specifiy what version of web3 you are using
  })
);
 
const address = "0xab7c74abC0C4d48d1bdad5DCB26153FC8780f83E";
 
web3.eth.getBalance(address, (err, wei) => {
  balance = web3.utils.fromWei(wei, "ether");
  console.log(balance.toString(10));
 
});
 
const tx = "0x4b35d880f099fb8ad9a00846699c868bf1de1fe045bd68491f2c92b891571d58";
 
web3.eth.getTransaction(tx, (error, result) => {
  console.log(error);
  console.log(result);
});

Read our Docs to get started, follow us on Twitter for all the updates, and subscribe to our Blog for all the insights.