UNPKG

@cheqd/sdk

Version:

A TypeScript SDK built with CosmJS to interact with the cheqd network ledger

142 lines 6.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CheqdQuerier = void 0; const stargate_cjs_1 = require("@cosmjs/stargate-cjs"); const tendermint_rpc_cjs_1 = require("@cosmjs/tendermint-rpc-cjs"); const types_1 = require("./types"); const query_1 = require("cosmjs-types-cjs/cosmos/base/tendermint/v1beta1/query"); /** * Extended QueryClient specifically designed for the Cheqd blockchain network. * Provides enhanced querying capabilities with support for custom extensions * and consensus parameter retrieval. */ class CheqdQuerier extends stargate_cjs_1.QueryClient { /** * Constructs a new CheqdQuerier instance with the provided Tendermint client. * * @param tmClient - Tendermint client (v34 or v37) for blockchain communication */ constructor(tmClient) { super(tmClient); } /** * Retrieves the consensus parameters from the blockchain network. * This method creates a temporary connection to fetch the latest block results * and extract consensus update information. * * @param url - RPC URL of the blockchain node * @returns Promise resolving to consensus parameters or undefined if not available */ static async getConsensusParameters(url) { // connect to comet rpc const cometClient = await tendermint_rpc_cjs_1.Tendermint37Client.connect(url); // get block results const result = await cometClient.blockResults(); // disconnect comet client cometClient.disconnect(); // return consensus parameters return result.consensusUpdates; } /** * Queries the node information over RPC to retrieve binary (application) version details. * Uses the Cosmos base tendermint service which returns both application and consensus versions. * * @param url - RPC URL of the blockchain node * @returns Promise resolving to GetNodeInfoResponse containing version info */ static async getNodeInfo(url) { const tmClient = await tendermint_rpc_cjs_1.Tendermint37Client.connect(url); try { const baseQuerier = new stargate_cjs_1.QueryClient(tmClient); const rpcClient = (0, stargate_cjs_1.createProtobufRpcClient)(baseQuerier); const tendermintService = new query_1.ServiceClientImpl(rpcClient); return await tendermintService.GetNodeInfo({}); } finally { tmClient.disconnect(); } } /** * Resolves the Cheqd network (mainnet/testnet) from the node info response. * @param nodeInfo - Node info response to inspect * @returns Detected network or undefined if it cannot be inferred */ static resolveNetworkFromNodeInfo(nodeInfo) { const chainId = nodeInfo.defaultNodeInfo?.network || nodeInfo.applicationVersion?.appName || nodeInfo.applicationVersion?.name || nodeInfo.applicationVersion?.version; if (!chainId) return undefined; const chainIdLower = chainId.toLowerCase(); if (chainIdLower.includes(types_1.CheqdNetwork.Mainnet)) { return types_1.CheqdNetwork.Mainnet; } if (chainIdLower.includes(types_1.CheqdNetwork.Testnet)) { return types_1.CheqdNetwork.Testnet; } return undefined; } /** * Detects the network (mainnet/testnet) by querying the node info over RPC. * @param url - RPC URL of the blockchain node * @returns Resolved CheqdNetwork value */ static async detectNetwork(url) { const nodeInfo = await CheqdQuerier.getNodeInfo(url); return CheqdQuerier.resolveNetworkFromNodeInfo(nodeInfo) ?? types_1.CheqdNetwork.Testnet; } /** * Creates a new CheqdQuerier instance by establishing a connection to the specified RPC URL. * This is the primary method for creating a querier instance for blockchain communication. * * @param url - RPC URL of the blockchain node to connect to * @returns Promise resolving to a connected CheqdQuerier instance */ static async connect(url) { const tmClient = await tendermint_rpc_cjs_1.Tendermint37Client.connect(url); return new CheqdQuerier(tmClient); } /** * Creates a CheqdQuerier instance from an existing Tendermint client. * Useful when you already have an established client connection. * * @param client - Existing Tendermint client (v34 or v37) * @returns Promise resolving to a CheqdQuerier instance using the provided client */ static async fromClient(client) { return new CheqdQuerier(client); } /** * Creates a CheqdQuerier instance with a single query extension. * Extensions provide specialized query capabilities for specific blockchain modules. * * @param url - RPC URL of the blockchain node to connect to * @param extension - Query extension setup to add specialized query functionality * @returns Promise resolving to a CheqdQuerier instance with the specified extension */ static async connectWithExtension(url, extension) { const tmClient = await tendermint_rpc_cjs_1.Tendermint37Client.connect(url); return CheqdQuerier.withExtensions(tmClient, extension); } /** * Creates a CheqdQuerier instance with multiple query extensions. * This method supports adding multiple specialized query capabilities for different * blockchain modules in a single operation. For single extensions, it delegates * to connectWithExtension for efficiency. * * @param url - RPC URL of the blockchain node to connect to * @param extensions - Variable number of query extension setups to add functionality * @returns Promise resolving to a CheqdQuerier instance with all specified extensions */ static async connectWithExtensions(url, ...extensions) { if (extensions.length === 1) { return CheqdQuerier.connectWithExtension(url, extensions[0]); } const tmClient = await tendermint_rpc_cjs_1.Tendermint37Client.connect(url); const tupleLike = extensions; return CheqdQuerier.withExtensions(tmClient, ...tupleLike); } } exports.CheqdQuerier = CheqdQuerier; //# sourceMappingURL=querier.js.map