Light Client node with no RPC
Integrate with the Astar networks using a light client.
This documentation will guide you on how to connect to the Astar Network using a light client (ScProvider) with the Polkadot.js API and Substrate Connect.
Prerequisites:
- Node.js (https://nodejs.org/en/download/)
- npm (https://www.npmjs.com/get-npm)
Overview
Astar Network is a scalable smart contract platform on Polkadot that supports Ethereum compatibility. Connecting to the Astar Network using a light client (ScProvider) allows you to interact with the network without having to sync the entire blockchain.
- Initialize a new Node.js project Create a new directory for your project and navigate to it in your terminal or command prompt. Initialize a new Node.js project by running:
mkdir astar-light-client
cd astar-light-client
npm init -y
- Install required packages Install the required packages by running:
npm install @polkadot/api @substrate/connect
- Create the chain specification file
Create a new file named
astar.json
in thechain-specs
directory of your project. You can download the latest Astar chain specification file from the Astar GitHub repository or use this link.
Create a new directory named chain-specs
and save the astar.json
file in it:
mkdir chain-specs
wget https://raw.githubusercontent.com/AstarNetwork/astar-apps/main/src/config/api/polkadot/chain-specs/astar.json -P chain-specs
- Create a script to connect to the Astar Network
Create a new file named
connect-astar.js
in your project directory and paste the following script into it:
const { ApiPromise, ScProvider } = require("@polkadot/api");
const Sc = require("@substrate/connect");
async function queryInfo(api) {
const assetMetadata = await api.query.assets.metadata.entries();
assetMetadata.map((asset) => {
let h = asset[1].toHuman();
console.log(JSON.stringify(h));
});
}
async function setup() {
const jsonParachainSpecAstar = require("./chain-specs/astar.json");
const astarSpec = JSON.stringify(jsonParachainSpecAstar);
const relayProvider = new ScProvider(Sc, Sc.WellKnownChain.polkadot);
const provider = new ScProvider(Sc, astarSpec, relayProvider);
await provider.connect();
const api = await ApiPromise.create({ provider });
console.log("Connected to Astar Network using ScProvider (light client)");
await queryInfo(api);
process.exit();
}
setup();
This script sets up a connection to the Astar Network using a light client (ScProvider) by leveraging Substrate Connect and the Polkadot.js API.
- Run the script Now, you can run the script to connect to the Astar Network:
node connect-astar.js
If the connection is successful, the script will output the following message:
Connected to Astar Network using ScProvider (light client)
{"deposit":"86,000,000,000","name":"MochiMochiCoin","symbol":"MMC","decimals":"2","isFrozen":false}
...
{"deposit":"0","name":"Liquid DOT","symbol":"LDOT","decimals":"10","isFrozen":false}
Congratulations! You have successfully connected to the Astar Network using a light client (ScProvider) with the Polkadot.js API and Substrate Connect. You can now use the api instance to interact with the Astar Network and query data or submit transactions.