Connect to OAN(Aion)

This tutorial is to walk you through connecting to the Ethereum network using the Pocket-Web3-Provider.

Initial Setup/ Installation

Before we get started, you will need to have a build of the Pocket Core CLI and a generated Application Authorization Token before you can use Pocket-web3-provider.

To download the packages, we will use:

Download the Pocket Provider:

The Pocket-web3-provider package is hosted in NPM, to install open up a terminal and enter:

npm install --save @pokt-network/web3-provider
npm install --save aion-web3 

Setting Up Web3 Env

You can use the call the Pocket Provider in any file that needs to connect to the Pocket Network using JavaScript, but to do that, we need to create an instance that contains all the data your application needs to have in order to send the request.

Import libraries

/* 
 Import some important libraries: 
    - PocketProvider: (required) Used to connect to the Pocket Network.
    - AionWeb3: (required) to send a transaction to Aion blockchain. 
    - PocketAAT: (required) Required to allow the client to access the network on the applications behalf. 
    - Configuration: (required) Storing network settings like: timeout, blockchainID, SSL, ect. 
    - TransactionSigner:(optional) Used for allowing your application to sign data to the blockchian  
        
*/

const PocketProvider = require('@pokt-network/web3-provider').PocketProvider;
const AionWeb3 = require('aion-web3');
const PocketCore = require('@pokt-network/pocket-js');
const Pocket = PocketCore.Pocket;
const PocketAAT = PocketCore.PocketAAT;
const Configuration = PocketCore.Configuration;
const SignTX = require('aion-web3/src/index').prototype.aion-web3-eth-accounts;

Preconfiguration:

We are going to hard code our application account, blockchain, pocket relay ,configuration options, dispatchers as well as define our transaction signer to sign the Ethereum transaction.


// An array holding the initial dispatcher url(s). You can use our known dispatcher list found here(https://docs.pokt.network/v2.1/docs/known-dispatcher-list) 
const dispatchers = [new URL("http://node1.testnet.pokt.network:8081"), new URL("http://node2.testnet.pokt.network:8081")];

/*
- 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.
   - maxConsensusNodes - (optional) Maximun amount of nodes for local consensus, mandatory ODD number, default 0.
   - requestTimeOut - (optional) Maximun timeout for every request in miliseconds, default 0.
   - acceptDisputedResponses - (optional) Accept or reject responses based on having a full consensus, default false.
  
*/
const configuration = new Configuration(1,5,3,30000,false);

// application PrivateKey Hex
const appPrivKeyHex = "6401..."

// blockchain: The specified blockchian ID
const blockchain = "a969144c864bd87a92e974f11aca9d964fb84cf5fb67bcc6583fe91a407a9309"; //Aion Mastery


/*
  create a transaction signer 
*/
const transactionSigner = {  
    /*
     Callback method called to determine wether or not the
     TransactionSigner supports signing transactions for the given addr         */
      hasAddress: async function (address) {             
          return address === "0x0" ? true : false;
      },
      /*
      Callback method called to generate a serialized signed format
      of the given a Web3.js transaction object 
      (https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sendtransaction)
      */
      signTransaction: async function (txParams) {
          var privateKeyBuffer = "0x0";
          var result = await SignTX.prototype.signTransaction(txParams,privateKey);
          var signedTx = result.rawTransaction;
          return signedTx;
      }
  }


/*
Creates a Pocket relay that takes the following params:
	- dispatchers: Array holding the initial dispatcher url(s).
  - rpcProvider: (optional) Provider which will be used to reach out to the Pocket Core RPC interface.
  - configuration: (optional) Configuration object.
  - store — (optional) Save data using a Key/Value relationship. This object save information in memory.
*/
const pocket = new Pocket(dispatchers,undefined, configuration,undefined);

Getting Network Hash:

View our supported networks along with their network hash to properly send the relay.

Creating AAT Token

The following section is to show you how to create an AAT token using the Pocket JS library.

async function sendRelay() {
  
    // generate a new client account.
    const clientPassphrase = "1234";
    const clientAccount = await pocket.keybase.createAccount(clientPassphrase);

    /*
      Import application acct: 
      	- privateKey: (required) The application accounts private key
        - passphrase: (required) A passphrase to encrypt the private key iin the keybase 
    */
    const importacct = await pocket.keybase.importAccount(appPrivKeyHex,"Diglett");
    const unlockAcct =  await pocket.keybase.unlockAccount(clientAccount.addressHex,clientPassphrase,0);

    //optional test to check if it has been unlocked returns true or false.
    const isUnlocked = await pocket.keybase.isUnlocked(clientAccount.addressHex);

    /* 
       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 key
        - applicationPublicKey: The hexadecimal publicKey of the Application
        - privateKey: private key of the application    
   */
  
    const pocketAAT = PocketAAT.from("0.0.1", clientAccount.publicKey.toString("hex"),appPubKeyHex,appPrivKeyHex);

   

Create Provider and Send a Relay:

Now that we have everything set up, it's time to put everything together.

Query The OAN(Aion) Blockchain:

To Query the OAN(Aion) blockchain, enter:

 /*
   Create the Pocket Provider instance:
      - activeBlockchain: Target blockchain hash
      - pocketAAT: Pocket Authentication Token object.
      - pocket: Pocket instance
      - transactionSigner: Object containing the TransactionSigner interface methods.
 */  
    const pocketProvider = new PocketProvider(blockchain, pocketAAT, pocket,transactionSigner);

    // inject into Web3:
    const aionWeb3 = new AionWeb3(pocketProvider)

    // call the web3 getBalance function. 
    const oanBal = aionWeb3.eth.getBalance("0xf892400Dc3C5a5eeBc96070ccd575D6A720F0F9f");
    
   console.log(await oanBal);
    
} // end of async function sendRelay()

sendRelay()

Sending a Transaction:

To send a transaction, enter:

Updated 3 days ago


Connect to OAN(Aion)


Suggested Edits are limited on API Reference Pages

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