/** * @packageDocumentation * @module API-Info */ import AvalancheCore from "../../avalanche" import { JRPCAPI } from "../../common/jrpcapi" import { RequestResponseData } from "../../common/apibase" import BN from "bn.js" import { GetBlockchainIDParams, IsBootstrappedParams, PeersParams, PeersResponse, UptimeResponse } from "./interfaces" /** * Class for interacting with a node's InfoAPI. * * @category RPCAPIs * * @remarks This extends the [[JRPCAPI]] class. This class should not be directly called. Instead, use the [[Avalanche.addAPI]] function to register this interface with Avalanche. */ export class InfoAPI extends JRPCAPI { /** * Fetches the blockchainID from the node for a given alias. * * @param alias The blockchain alias to get the blockchainID * * @returns Returns a Promise containing the base 58 string representation of the blockchainID. */ getBlockchainID = async (alias: string): Promise => { const params: GetBlockchainIDParams = { alias } const response: RequestResponseData = await this.callMethod( "info.getBlockchainID", params ) return response.data.result.blockchainID } /** * Fetches the networkID from the node. * * @returns Returns a Promise of the networkID. */ getNetworkID = async (): Promise => { const response: RequestResponseData = await this.callMethod( "info.getNetworkID" ) return response.data.result.networkID } /** * Fetches the network name this node is running on * * @returns Returns a Promise containing the network name. */ getNetworkName = async (): Promise => { const response: RequestResponseData = await this.callMethod( "info.getNetworkName" ) return response.data.result.networkName } /** * Fetches the nodeID from the node. * * @returns Returns a Promise of the nodeID. */ getNodeID = async (): Promise => { const response: RequestResponseData = await this.callMethod( "info.getNodeID" ) return response.data.result.nodeID } /** * Fetches the version of Gecko this node is running * * @returns Returns a Promise containing the version of Gecko. */ getNodeVersion = async (): Promise => { const response: RequestResponseData = await this.callMethod( "info.getNodeVersion" ) return response.data.result.version } /** * Fetches the transaction fee from the node. * * @returns Returns a Promise of the transaction fee in nAVAX. */ getTxFee = async (): Promise<{ txFee: BN creationTxFee: BN createAssetTxFee: BN createSubnetTxFee: BN createBlockchainTxFee: BN }> => { // TODO - Add `GetTxFee` response interface const response: RequestResponseData = await this.callMethod("info.getTxFee") return { txFee: new BN(response.data.result.txFee, 10), creationTxFee: new BN(response.data.result.creationTxFee, 10), createAssetTxFee: new BN(response.data.result.createAssetTxFee, 10), createSubnetTxFee: new BN(response.data.result.createSubnetTxFee, 10), createBlockchainTxFee: new BN( response.data.result.createBlockchainTxFee, 10 ) } } /** * Check whether a given chain is done bootstrapping * @param chain The ID or alias of a chain. * * @returns Returns a Promise of whether the chain has completed bootstrapping. */ isBootstrapped = async (chain: string): Promise => { const params: IsBootstrappedParams = { chain } const response: RequestResponseData = await this.callMethod( "info.isBootstrapped", params ) return response.data.result.isBootstrapped } /** * Returns the peers connected to the node. * @param nodeIDs an optional parameter to specify what nodeID's descriptions should be returned. * If this parameter is left empty, descriptions for all active connections will be returned. * If the node is not connected to a specified nodeID, it will be omitted from the response. * * @returns Promise for the list of connected peers in PeersResponse format. */ peers = async (nodeIDs: string[] = []): Promise => { const params: PeersParams = { nodeIDs } const response: RequestResponseData = await this.callMethod( "info.peers", params ) return response.data.result.peers } /** * Returns the network's observed uptime of this node. * * @returns Returns a Promise which contains rewardingStakePercentage and weightedAveragePercentage. */ uptime = async (): Promise => { const response: RequestResponseData = await this.callMethod("info.uptime") return response.data.result } /** * This class should not be instantiated directly. Instead use the [[Avalanche.addAPI]] method. * * @param core A reference to the Avalanche class * @param baseURL Defaults to the string "/ext/info" as the path to rpc's baseURL */ constructor(core: AvalancheCore, baseURL: string = "/ext/info") { super(core, baseURL) } }