Websocket API Guide for OKTC: Ethereum-Compatible Subscriptions

ยท

OKTC leverages Tendermint Core as its consensus engine and is built on the Cosmos SDK framework. While inheriting Tendermint's event format, OKTC adapts these responses to Ethereum-compatible types to support Web3 websocket functionality via Ethereum's PubSub API. This guide covers WebSocket setup, subscriptions, and supported events.

Getting Started with Websockets

To initiate an Ethereum-compatible WebSocket connection:

  1. Start the REST server using the --wsport flag (default port: 8546)
  2. Use a WebSocket client like ws to establish a connection

๐Ÿ‘‰ Explore OKTC's developer tools for seamless integration.

Mainnet Websocket Endpoint

Connect to: wss://mainnet.oktc.io/ws (replace with actual OKTC mainnet URL)

Managing Subscriptions

Creating Subscriptions

Use eth_subscribe RPC call with:

Example Request:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "eth_subscribe",
  "params": ["logs", {"address": "0x00..."}]
}

Canceling Subscriptions

Use eth_unsubscribe with:

Example Request:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "eth_unsubscribe",
  "params": ["0xab12..."]
}

Supported Subscription Types

1. New Block Headers (newHeads)

Triggers on new chain headers, including reorganizations.

Parameters: None
Example Output:

{
  "jsonrpc": "2.0",
  "method": "eth_subscription",
  "params": {
    "subscription": "0xab12...",
    "result": {
      "number": "0x1b4",
      "hash": "0x00...",
      "parentHash": "0x00..."
    }
  }
}

2. Logs

Filters and streams contract events.

Parameters:

Example:

{
  "address": ["0x00..."],
  "topics": ["0x00..."]
}

3. Pending Transactions (newPendingTransactions)

Emits hashes of signed transactions entering mempool.

Parameters: None
Example Output: Transaction hash string

4. Sync Status (syncing)

Tracks node synchronization state.

Parameters: None
Output: Boolean or sync progress object

FAQs

Q: What's the difference between Tendermint and Ethereum WebSocket events?

A: OKTC translates Tendermint's native events into Ethereum-compatible formats for Web3 tooling interoperability.

Q: Can I filter logs by multiple contracts?

A: Yes, pass an array of addresses in the address parameter.

Q: How are chain reorganizations handled?

A: For logs subscriptions, removed logs include "removed": true. For newHeads, new canonical chain headers emit normally.

๐Ÿ‘‰ Need advanced WebSocket configurations? Check OKTC's developer portal.

Best Practices

This implementation combines Cosmos SDK efficiency with Ethereum developer familiarity, enabling seamless dApp integration.