peaq API Documentation
Task
This section will discuss APIs; it is broken up into two subsections, the first of which deals with Web3 APIs and the second with RPC calls.
Web3
Web3.js is a set of libraries that allow developers to interact with Ethereum nodes using HTTP, IPC, or WebSocket protocols with JavaScript. Therefore, developers can leverage this compatibility and use the Web3.js library to interact with a peaq node as if they were doing so on Ethereum.
Create a JavaScript Project
To get started, you can create a directory to store all of the files you'll be creating throughout this guide:
For this guide, you'll need to install the Web3.js library and the Solidity compiler. To install both NPM packages, you can run the following command:
Connect to Network Script
To check the balances of both addresses both before and after the transaction is sent, you only need one file. You can build a connect.js file to get going by executing:
Next, you will create the script for this file and complete the following steps:
Example ->
const web3 = new Web3(‘https://erpc.agung.peaq.network’);
Check Balances Script
To check the balances of both addresses both before and after the transaction is sent, you only need one file. You may create a balances.js file to get going by executing:
Next, you will create the script for this file and complete the following steps:
You can view the: complete script on GitHub.
Note
To run the script and fetch the account balances, you can run the following command:
node balances.js
If successful, the balances for the origin and receiving address will be displayed in your terminal in ETH.
Send Transaction Script
For carrying out an account-to-account transaction, just one file is required. In this demonstration, you'll move 1 AGUNG token from an origin address (for which you are in possession of the private key) to a destination address. You can create a transaction.js file to begin going by executing:
touch transaction.js
Next, you will create the script for this file and complete the following steps
You can view the: complete script on GitHub.
To run the script, you can run the following command in your terminal:
node transaction.js
If the transaction was successful, you will see the transaction hash printed out in your terminal.
You can also use the balances.js script to check that the balances for the origin and receiving accounts have changed.
RPC Calls
Link for information regarding all the current RPC calls methods
The return value of this command is not in human-readable format. For that, it needs to use Type encoding (SCALE).