XDC3 HTTP Provider

1.3.13420 · active · verified Wed Apr 22

xdc3-providers-http is a core sub-package within the XDC3 ecosystem, providing the essential functionality to establish RPC connections over HTTP(S) for interacting with the XinFin blockchain. As of version 1.3.13420, it is a stable component, primarily used in Node.js environments (requiring Node.js >= 8.0.0). It enables developers to configure various HTTP request parameters such as `keepAlive`, `timeout`, and custom `headers`, along with advanced `agent` options, for reliable and efficient communication with XDC network nodes. The library ships with TypeScript typings, facilitating its integration into modern TypeScript projects. While its exact release cadence isn't explicitly stated, the versioning suggests continuous maintenance and minor enhancements within the 1.x series, aligning with the broader XDC3 library development. Its key differentiator is its tight integration and optimization for the XinFin blockchain, offering a robust and configurable HTTP interface.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate and configure `Web3HttpProvider` with custom options (like timeout and headers) and integrate it with the `XDC3` library to fetch the current block number and chain ID from an XDC node, showcasing basic connectivity.

import Web3HttpProvider from 'xdc3-providers-http';
import XDC3 from 'xdc3'; // Ensure 'xdc3' package is also installed

// --- Configuration ---
// It's good practice to get URLs from environment variables
const xdcNodeUrl = process.env.XDC_NODE_URL ?? 'http://localhost:8545';

// --- Provider Options (HttpProviderOptions type) ---
const providerOptions = {
    keepAlive: true,
    timeout: 30000, // RPC call timeout in milliseconds
    headers: [
        { name: 'X-App-Name', value: 'MyXDC3App' },
        { name: 'Authorization', value: `Bearer ${process.env.XDC_AUTH_TOKEN ?? ''}` } // Example for authenticated nodes
    ],
    // For custom HTTP agent configuration in Node.js, e.g., to increase maxSockets:
    // agent: { http: new (require('http').Agent)({ maxSockets: 10 }), https: new (require('https').Agent)({ maxSockets: 10 }) },
};

// --- Instantiate the Provider ---
const provider = new Web3HttpProvider(xdcNodeUrl, providerOptions);

// --- Instantiate XDC3 with the provider ---
const xdc3 = new XDC3(provider);

// --- Example Usage: Get the current block number and chain ID ---
async function getCurrentBlockchainInfo() {
    try {
        const blockNumber = await xdc3.eth.getBlockNumber();
        const chainId = await xdc3.eth.getChainId();
        console.log(`Successfully connected to XDC node at ${xdcNodeUrl}`);
        console.log(`Current block number: ${blockNumber}`);
        console.log(`Connected to Chain ID: ${chainId}`);

    } catch (error) {
        console.error('Failed to connect or retrieve data from XDC node:', error);
        if (error instanceof Error) {
             console.error(`Error details: ${error.message}`);
        }
    }
}

getCurrentBlockchainInfo();

// To run this example:
// 1. Install packages: npm install xdc3 xdc3-providers-http
// 2. Ensure an XDC node is accessible at the specified URL (e.g., http://localhost:8545).
// 3. If using TypeScript, you can run directly with 'npx ts-node your-script.ts' (install ts-node).

view raw JSON →