5

In the documentation getChainProperties(https://polkadot.js.org/docs/api/examples/promise/chain-info/) is mentioned for getting the properties for a blockchain, but it contains very little amount of information.

How could I query the chainspec of a blockchain from an RPC? I am specifically interested in the name and para_id(if a parachain) fields.

3 Answers 3

2

I did not find a specific way to query the chainspec, but using a different approach I am able to get the parachain id and the name of the blockchain.

import { ApiPromise } from '@polkadot/api';

/* snip */

// Create an API
api = await ApiPromise.create({ provider: wsProvider, noInitWarn: true });

// Get the blockchain name:
const name = await api.rpc.system.chain();
console.log(name.toHuman());

// Get the parachain id:
const parachainId = await api.query.parachainInfo.parachainId();
console.log(parachainId.toHuman());
1
  • 1
    is this answer good enough for the original question? if you think so, please set it as the confirmed answer. Also, for completenesses, here you have all the available system RPC methods for Polkadot Relay chain: polkadot.js.org/docs/polkadot/rpc#system
    – Iker
    Jul 24 at 10:07
1

The RPC call sync_state => genSyncSpec. Unfortunately atm only available for the relay chains it seems (ref).

-1

To query the chainspec of a blockchain from an RPC, you can use the following code:

import polkadot from 'polkadot-js';

const client = polkadot.createClient();

const chainspec = await client.rpc.chainSpec();

const name = chainspec.name;

const paraId = chainspec.paraId;

console.log(The chain name is ${name});

console.log(The para chain ID is ${paraId});

This code will first create a client object for the Polkadot network. Then, it will call the rpc.chainSpec() method to get the chainspec for the current chain. Finally, it will print the name and para chain ID of the chain to the console.

For example, if you are running this code on the Polkadot mainnet, the name of the chain will be "Polkadot" and the para chain ID will be 0.

Here is a breakdown of the code:

The import polkadot from 'polkadot-js'; line imports the Polkadot JavaScript library.

The const client = polkadot.createClient(); line creates a client object for the Polkadot network.

The const chainspec = await client.rpc.chainSpec(); line calls the rpc.chainSpec() method to get the chainspec for the current chain.

The const name = chainspec.name; line gets the name of the chain from the chainspec.

The const paraId = chainspec.paraId; line gets the para chain ID of the chain from the chainspec.

The console.log(The chain name is ${name}); line prints the name of the chain to the console.

The console.log(The para chain ID is ${paraId}); line prints the para chain ID of the chain to the console.

1
  • The polkadot-js npm package does not exist.
    – Szegoo
    Jul 2 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.