Substrate Explorer API
Selendra's Substrate layer can be queried programmatically using the WebSocket RPC API or the Polkadot.js SDK. This allows access to the native chain state, governance, staking, and other Substrate-specific features.
Overview
Unlike the EVM Explorer (Blockscout) which provides a REST API, the Substrate Explorer (Polkadot.js) interacts directly with the blockchain nodes via WebSocket or HTTP RPC.
- Interface: Selendra Portal
- RPC Endpoint:
wss://rpc.selendra.org - HTTP Endpoint:
https://rpc.selendra.org
Connecting via Polkadot.js API
The easiest way to interact with the Substrate API is using the @polkadot/api library.
Installation
npm install @polkadot/api
Basic Connection
import { ApiPromise, WsProvider } from '@polkadot/api';
async function main() {
// Initialise the provider to connect to the local node
const provider = new WsProvider('wss://rpc.selendra.org');
// Create the API and wait until ready
const api = await ApiPromise.create({ provider });
// Retrieve the chain & node information via rpc calls
const [chain, nodeName, nodeVersion] = await Promise.all([
api.rpc.system.chain(),
api.rpc.system.name(),
api.rpc.system.version()
]);
console.log(`You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`);
}
main();
Common API Queries
Get Block Information
// Get the latest block hash
const lastBlockHash = await api.rpc.chain.getFinalizedHead();
// Get the block data
const block = await api.rpc.chain.getBlock(lastBlockHash);
console.log(`Latest block number: ${block.block.header.number}`);
console.log(`Block hash: ${lastBlockHash}`);
Query Account Balance
const ADDR = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
// Retrieve the account balance & nonce via the system module
const { nonce, data: balance } = await api.query.system.account(ADDR);
console.log(`Balance: ${balance.free} and nonce: ${nonce}`);
Subscribe to New Heads
// Subscribe to the new headers
const unsub = await api.rpc.chain.subscribeNewHeads((header) => {
console.log(`New block #${header.number} has hash ${header.hash}`);
});
RPC Methods
You can also interact with the node using standard JSON-RPC calls if you are not using the JavaScript SDK.
Get Chain Info
Request:
curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "system_chain", "params":[]}' https://rpc.selendra.org
Response:
{
"jsonrpc": "2.0",
"result": "Selendra Mainnet",
"id": 1
}
Get Block Hash
Request:
curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "chain_getBlockHash", "params":[0]}' https://rpc.selendra.org
Key Substrate Concepts
Storage Queries
Substrate storage is defined in the runtime modules (pallets). You can query any storage item using api.query.<module>.<item>.
api.query.system.account(address): Account infoapi.query.staking.validators(): List of validatorsapi.query.timestamp.now(): Current block timestamp
Extrinsics (Transactions)
Transactions in Substrate are called "extrinsics".
// Create a transfer extrinsic
const transfer = api.tx.balances.transfer(recipient, amount);
// Sign and send the transaction
const hash = await transfer.signAndSend(sender);
Additional Resources
Found an issue or want to contribute?
Help us improve this documentation by editing this page on GitHub.
