Intro to RPC Calls

Interacting with the Toto Chain via RPC

For any external application (like a wallet, a block explorer, or a decentralized application) to communicate with the Toto Chain, it needs a way to read data from the chain and submit transactions. This is accomplished using Remote Procedure Calls (RPCs).

This document explains the standard methods for interacting with a live Toto Chain node.

Communication Protocol

A Toto Chain node exposes its RPC functionality over standard web protocols, giving developers two main ways to connect:

  1. WebSockets (WS): This is the preferred and most powerful method. WebSockets provide a persistent, two-way communication channel between your application and the Toto Chain node. This is essential for building dynamic dApps because it allows you to not only query data but also subscribe to on-chain events in real-time. For example, you can get instant notifications when a new block is produced, a transaction is finalized, or your account balance changes.

  2. HTTP/HTTPS: This is a standard request-response protocol. Your application can send a request to the node's HTTP endpoint and will get a single response back. It's useful for simple, one-off queries (like checking the current block number), but it is less efficient for applications that need continuous, real-time updates from the chain.

Message Format: JSON-RPC

All communication, whether over WebSockets or HTTP, follows the JSON-RPC 2.0 specification. This is a lightweight and standardized format where the application sends a JSON object to the node specifying:

  • jsonrpc: The protocol version, which is always "2.0".

  • id: A unique identifier for the request, so the application can match responses to the requests it sent.

  • method: The name of the function to be called on the node (e.g., chain_getBlock, system_chain).

  • params: An array of parameters that the function requires.

The Toto Chain node processes the request and sends a JSON-formatted response back to the application.

How to Interact with the Toto Chain

The Easy Way: Using the Polkadot-JS API

While you can manually craft and send JSON-RPC requests, this is complex and error-prone. The vast majority of developers in the ecosystem use the Polkadot-JS API, a powerful JavaScript/TypeScript library that handles all the low-level communication for you.

This library provides a convenient, high-level interface that:

  • Connects seamlessly to a Toto Chain node's WebSocket endpoint.

  • Automatically handles the JSON-RPC message formatting.

  • Exposes all the available RPC methods as simple JavaScript functions.

  • Makes it easy to subscribe to chain events.

Example Workflow using Polkadot-JS API:

  1. Installation: A developer adds the library to their project: npm install @polkadot/api

  2. Connection: In their application code, they create an API instance and connect to a Toto Chain node.

    import { ApiPromise, WsProvider } from '@polkadot/api';
    
    // Connect to a Toto Chain node's WebSocket endpoint
    const wsProvider = new WsProvider('ws://127.0.0.1:9944'); // Replace with your node's address
    const api = await ApiPromise.create({ provider: wsProvider });
    
  3. Making RPC Calls: Once connected, the developer can call any RPC method. For example, to get the chain's name:

    // This call sends a `system_chain` RPC request under the hood
    const chainName = await api.rpc.system.chain();
    console.log(`You are connected to the ${chainName} network.`);
    

The Manual Way: Without Polkadot-JS

Yes, you can absolutely interact with a Toto Chain node without the Polkadot-JS library. This requires you to handle the JSON-RPC communication yourself. This approach gives you more control and can be done from any programming language that has libraries for WebSockets or HTTP requests.

Here’s how you would do it manually using a simple tool like curl for an HTTP request, or any WebSocket client for a WS connection.

Example: Manual HTTP Request with curl

Let's say you want to get the hash of the latest block. The JSON-RPC method for this is chain_getHead.

You would use a tool like curl to send a POST request to the node's HTTP endpoint (usually on port 9933).

curl -H "Content-Type: application/json" -d \
  '{
     "id":1,
     "jsonrpc":"2.0",
     "method":"chain_getHead",
     "params":[]
   }' \
  http://127.0.0.1:9933/

The Toto Chain node would respond with a JSON object like this:

{
  "jsonrpc": "2.0",
  "result": "0x1a2b3c...", // This is the hash of the latest block
  "id": 1
}

Example: Manual WebSocket Interaction

Using a WebSocket client (e.g., in Python, Go, or even a browser's developer console), the process would be:

  1. Connect to the node's WebSocket endpoint (e.g., ws://127.0.0.1:9944).

  2. Once connected, send a JSON-RPC message as a string: '{"id":1, "jsonrpc":"2.0", "method":"chain_subscribeNewHeads", "params":[]}'

  3. Because this is a subscription method, the node will start sending you a new message with the latest block header every time a new block is produced.

While this manual approach works, it requires you to handle connection management, request IDs, and parsing the JSON responses yourself, which is why libraries like Polkadot-JS are highly recommended for most development on the Toto Chain.

Last updated