Create Relay With Pocket JS

The Pocket-JS is the core client used for sending relays to any network that is running on the Pocket Network.


Installing Pocket-JS

To install the Pocket-JS library in your project, run the following NPM command or add pocket-js in your package.json:

npm install --save @pokt-network/pocket-js

Creating a Relay

Import Pocket Library

To create a relay we have to import our @pokt-network/pocket-js module and declare some variables.

// Import PocketJS and declare the following variables 
const Pocketjs = require('@pokt-network/pocket-js')
const pocket = Pocketjs.Pocket
const Configuration = Pocketjs.Configuration;
const PocketAAT = Pocketjs.PocketAAT;
const HttpRpcProvider = Pocketjs.HttpRpcProvider;
const Node = Pocketjs.Node;
const BondStatus = Pocketjs.BondStatus;

Obtain a Node From The Network

Next, we are going to obtain a node and being to configure our Pocket instance to be able to send relays.

// Gets a list of all the Pocket Nodes on the network 
const getPocketNode = pocketInstance.rpc.query.getNodes();

// Gets list of blockchians that node is servicing. 
const getNodeNetworks = pocketInstance.rpc.query.getNode("<node address>");

     address: 'f993...',
     jailed: false,
     status: 2,
     stakedTokens: 1000000000n,
     serviceURL: '<node URL>',


Create Pocket Instance and Configure Network Properties:

The Configuration class stores multiple properties that can be used to interact with the Pocket Network.

Create a node:
	- address: the hex address of the validator
  - publicKey: the hex consensus public key of the validator.
  - jailed: (boolean) has the validator been jailed from staked status? 
  - status: validator status
  - stakedTokens: how many staked tokens
  - serviceURL: service node URL
  - chains: chains
  - unstakingCompletionTime: if unstaking, min time for the validator to complete unstaking


const node = new Node("17ca...","05f1...",false,BondStatus.bonded,1000,"",["8ef9...","a969..."])

(optional)Configuration stores multiple properties used to interact with the Pocket Network. 
   - maxDispatchers - (optional) Maximun amount of dispatchers urls to stored in rounting table, default 0.
   - maxSessions - (optional) Maximun amount of sessions to stored for the session manager, default 0.
   - requestTimeOut - (optional) Maximun timeout for every request in miliseconds, default 0.
const config =  new Configuration([node],3,2,2000);

// create RPC provider 
const provider = new HttpRpcProvider(new URL(node.serviceURL))

 create a pocket instance and stores muliple configuration options for your node
	- dispatchers: Array holding the initial dispatcher url(s).
  - rpcProvider: Provider which will be used to reach out to the Pocket Core RPC interface.
  - configuration: configuration object
  - store: Save data using a Key/Value relationship. This object save information in memory.

const pocketInstance =  new pocket(provider,config,); 

NOTE: when copying a nodes chains list, it is easier to use a compactor to condense the hashes to single line. A helpful tool is:

Unlock Client Account and Create AAT

// client account private key
 const myPK = Buffer.from("36ec...","hex");
//import your client account     
const importacct = await pocketInstance.keybase.importAccount(myPK,"passphrase");

// unlock client account  
const unlockAcct =  await pocketInstance.keybase.unlockAccount(importacct.addressHex,"passphrase",0);

//(optional) check to see if your account has been unlocked 
const isunlocked= await pocketInstance.keybase.isUnlocked(importacct.addressHex);

// public and private key key of the staked application account that will be signing the relays 
const acctPubk = "25e4...";

const acctPriv = "640d...";

   - pocketAAT: Creates a PocketAAT object, and creates the signature using the provided parameters:
      - Version: The spec version under which this ATT needs to be interpreted.
      - clientPublicKey: The client wallets public address.
      - applicationPublicKey: The hexadecimal publicKey of the Application.
      - privateKey: The private key of the application. 
const pocketAAT = PocketAAT.from("0.0.1",importacct.publicKey.toString("hex"),acctPubk,acctPriv);

Sending the Relay:

Before we send the relay, we ned to create a payload with the data we are quering(or writing) to the blockchain.

//create payload with the required ETH params 
var payload = {
    "jsonrpc": "2.0",
    "method": "eth_getBalance",
    "params": ["0xf892400Dc3C5a5eeBc96070ccd575D6A720F0F9f", "latest"],
    "id": (new Date()).getTime()

//rinkeby blockchain hash
const BC = "8cf7f8799c5b30d36c86d18f0f4ca041cf1803e0414ed9e9fd3a19ba2f0938ff";

//send relay to retrieve eth balance 
const balance = await pocketInstance.sendRelay(JSON.stringify(payload),BC,pocetAAT);

Updated 11 days ago

Create Relay With Pocket JS

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.