website logo
WebsiteGithubDiscordTwitterTelegram
⌘K
Get started
Welcome to peaq
Introduction to peaq
Our mission
Our vision
Learn
Networks overview
Economics
Tokens & Token Utility
Why Polkadot
Tech stack
Source code
Important links
Security Assessment Reports
Builders
Get started
Build
Integrate
Get inspired
Grants & Funding
Ecosystem and Community Initiatives
User Interfaces
peaq control
peaq App
Node operators
Run a node
Tokens & Earn
Agung tokens (Faucet)
Wallets
Docs powered by archbee 
12min

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:

mkdir web3-demo && cd web3-demo

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:

npm install web3

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:

touch connect.js

Next, you will create the script for this file and complete the following steps:

Connect Web3 Script


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:

1. Follow the steps above to create a connection 2. Define the addressFrom and addressTo variables 3. Create the asynchronous balances function which wraps the web3.eth.getBalance method. 4. Use the web3.eth.getBalance function to fetch the balances for the addressFrom and addressTo addresses. You can also leverage the web3.utils.fromWei function to ransform the balance into a more readable number in ETH. 5. Lastly, run the balances function
// 1. Add the Web3 provider connection logic here: // 2. Create address variables const addressFrom = 'ADDRESS-FROM-HERE'; const addressTo = 'ADDRESS-TO-HERE';
Check Balance Script


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

1. Follow the steps above to create a connection 2. Define the addressFrom, including the privateKey, and the addressTo variables. The private key is required to create a wallet instance. Note: This is for example purposes only. Never store your private keys in a JavaScript file 3. Create the asynchronous send function which wraps the transaction object and the sign and send transaction functions. 4. Create and sign the transaction using the web3.eth.accounts.signTransaction function. Pass in the gas, addressTo, and value for the transaction along with the sender's privateKey. 5. Send the signed transaction using the web3.eth.sendSignedTransaction method and pass in the raw transaction. Then use await to wait until the transaction is processed and the transaction receipt is returned Lastly, run the send function.
Send Transaction Script


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

calling RPC
POST
Params
Body Parameters
id
required
Number
ID
jsonrpc
required
String
JSONRPC version like 2.0
method
required
String
Method name of RPC call


The return value of this command is not in human-readable format. For that, it needs to use Type encoding (SCALE).

function decode_metadata(metadata) { return new TextDecoder().decode(util.hexToU8a(metadata)); }



Did this page help you?
Yes
No
UP NEXT
Integrate
Docs powered by archbee 
Connect Web3 Script