Get Node Information
You can access all the features of the iota.rs
library using an instance of the Client
class. The Client
class
provides high-level abstraction to all interactions over IOTA network (Tangle). You have to instantiate this class
before you start any interactions with the library, or more precisely with the
IOTA nodes that power IOTA network.
The following code example will:
- Create a
Client
which will connect to the Shimmer Testnet. - Use the created client to get information about the node.
- Print the information to the console.
You can also find this guide in the native iota.js library
Code Example
- Rust
- Nodejs
- Python
- Java
This example uses dotenv, which is not safe to use in production environments.
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! Calls `GET /api/core/v2/info`.
//! Returns general information about the node.
//! Run: `cargo run --example node_api_core_get_info --release -- [NODE URL]`.
use iota_client::{Client, Result};
#[tokio::main]
async fn main() -> Result<()> {
// Take the node URL from command line argument or use one from env as default.
let node_url = std::env::args().nth(1).unwrap_or_else(|| {
// This example uses dotenv, which is not safe for use in production.
dotenv::dotenv().ok();
std::env::var("NODE_URL").unwrap()
});
// Create a client with that node.
let client = Client::builder()
.with_node(&node_url)?
.with_ignore_node_health()
.finish()?;
// Get node info.
let info = client.get_info().await?;
println!("{info:#?}");
Ok(())
}
Run the Example
Run the example by running the following command:
cargo run --example node_api_core_get_info --release -- https://api.testnet.shimmer.network
- You can replace
https://api.testnet.shimmer.network
with any node url.
// Copyright 2021-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import { Client, initLogger } from '@iota/client';
require('dotenv').config({ path: '../.env' });
// Run with command:
// node ./dist/00_get_info.js
// In this example we will get information about the node
async function run() {
initLogger();
if (!process.env.NODE_URL) {
throw new Error('.env NODE_URL is undefined, see .env.example');
}
const client = new Client({
// Insert your node URL in the .env.
nodes: [process.env.NODE_URL],
localPow: true,
});
try {
const nodeInfo = await client.getInfo();
console.log('Node info: ', nodeInfo);
} catch (error) {
console.error('Error: ', error);
}
}
run().then(() => process.exit());
// Example output:
// Node info: {
// nodeInfo: {
// name: 'HORNET',
// version: '2.0.0-alpha.25',
// status: {
// isHealthy: true,
// latestMilestone: [Object],
// confirmedMilestone: [Object],
// pruningIndex: 0
// },
// supportedProtocolVersions: [ 2 ],
// protocol: {
// version: 2,
// networkName: 'dummy-1',
// bech32Hrp: 'rms',
// minPowScore: 1500,
// rentStructure: [Object],
// tokenSupply: '1450896407249092'
// },
// pendingProtocolParameters: [],
// baseToken: {
// name: 'Shimmer',
// tickerSymbol: 'SMR',
// unit: 'SMR',
// subunit: 'glow',
// decimals: 6,
// useMetricPrefix: false
// },
// metrics: {
// blocksPerSecond: 1.2,
// referencedBlocksPerSecond: 1.2,
// referencedRate: 100
// },
// features: []
// },
// url: 'https://api.testnet.shimmer.network'
// }
You can run the example by running the following command from the bindings/node/examples/
folder:
node dist/00_get_info.js
from iota_client import IotaClient
# Create an IotaClient instance
client = IotaClient({'nodes': ['https://api.testnet.shimmer.network']})
# Get the node info
node_info = client.get_info()
print(f'{node_info}')
You can run the example by running the following command from the binding/python/examples
folder:
python3 00_get_info.py
import org.iota.Client;
import org.iota.types.ClientConfig;
import org.iota.types.expections.ClientException;
import org.iota.types.expections.InitializeClientException;
import org.iota.types.responses.NodeInfoResponse;
public class GetInfo {
public static void main(String[] args) throws ClientException, InitializeClientException {
// Build the client.
Client client = new Client(new ClientConfig().withNodes(new String[]{"https://api.testnet.shimmer.network"}));
// Get the node information for a given node.
NodeInfoResponse response = client.getNodeInfo();
// Print the URL of the node that was requested.
System.out.println(response.getNodeUrl());
// Print the node information for the requested node.
System.out.println(response.getNodeInfo());
}
}
Expected Output
- Rust
- Nodejs
- Python
- Java
NodeInfoWrapper {
node_info: InfoResponse {
name: "HORNET",
version: "2.0.0-beta.5",
status: StatusResponse {
is_healthy: true,
latest_milestone: LatestMilestoneResponse {
index: 746869,
timestamp: 1661868434,
milestone_id: "0xbab289ebb1fdcc8516772074c3c465aaad648f234a4c1ff138f16c65a88b1298",
},
confirmed_milestone: ConfirmedMilestoneResponse {
index: 746869,
timestamp: 1661868434,
milestone_id: "0xbab289ebb1fdcc8516772074c3c465aaad648f234a4c1ff138f16c65a88b1298",
},
pruning_index: 0,
},
supported_protocol_versions: [
2,
],
protocol: ProtocolResponse {
version: 2,
network_name: "testnet-1",
bech32_hrp: "rms",
min_pow_score: 1500.0,
rent_structure: RentStructureResponse {
v_byte_cost: 100,
v_byte_factor_key: 10,
v_byte_factor_data: 1,
},
token_supply: "1450896407249092",
},
pending_protocol_parameters: [],
base_token: BaseTokenResponse {
name: "Shimmer",
ticker_symbol: "SMR",
unit: "SMR",
subunit: Some(
"glow",
),
decimals: 6,
use_metric_prefix: false,
},
metrics: MetricsResponse {
blocks_per_second: 8.4,
referenced_blocks_per_second: 9.2,
referenced_rate: 109.52380952380952,
},
features: [],
},
url: "https://api.testnet.shimmer.network",
}
Node info: {
nodeInfo: {
name: 'HORNET',
version: '2.0.0-beta.5',
status: {
isHealthy: true,
latestMilestone: [Object],
confirmedMilestone: [Object],
pruningIndex: 0
},
supportedProtocolVersions: [ 2 ],
protocol: {
version: 2,
networkName: 'testnet-1',
bech32Hrp: 'rms',
minPowScore: 1500,
rentStructure: [Object],
tokenSupply: '1450896407249092'
},
pendingProtocolParameters: [],
baseToken: {
name: 'Shimmer',
tickerSymbol: 'SMR',
unit: 'SMR',
subunit: 'glow',
decimals: 6,
useMetricPrefix: false
},
metrics: {
blocksPerSecond: 10.4,
referencedBlocksPerSecond: 10,
referencedRate: 96.15384615384616
},
features: []
},
url: 'https://api.testnet.shimmer.network'
}
{
'nodeInfo': {
'name': 'HORNET',
'version': '2.0.0-beta.5',
'status': {
'isHealthy': True,
'latestMilestone': {
'index': 746844,
'timestamp': 1661868309,
'milestoneId': '0xee26fb7ff5b4bed7b89a761ca3d75a9c99f8c4589e55d3ec0598c9a37717a61b'
},
'confirmedMilestone': {
'index': 746844,
'timestamp': 1661868309,
'milestoneId': '0xee26fb7ff5b4bed7b89a761ca3d75a9c99f8c4589e55d3ec0598c9a37717a61b'
},
'pruningIndex': 0
},
'supportedProtocolVersions': [
2
],
'protocol': {
'version': 2,
'networkName': 'testnet-1',
'bech32Hrp': 'rms',
'minPowScore': 1500.0,
'rentStructure': {
'vByteCost': 100,
'vByteFactorKey': 10,
'vByteFactorData': 1
},
'tokenSupply': '1450896407249092'
},
'pendingProtocolParameters': [
],
'baseToken': {
'name': 'Shimmer',
'tickerSymbol': 'SMR',
'unit': 'SMR',
'subunit': 'glow',
'decimals': 6,
'useMetricPrefix': False
},
'metrics': {
'blocksPerSecond': 9.8,
'referencedBlocksPerSecond': 8.4,
'referencedRate': 85.71428571428571
},
'features': [
]
},
'url': 'https://api.testnet.shimmer.network'
}
{
"name": "HORNET",
"version": "2.0.0-beta.5",
"status": {
"isHealthy": true,
"latestMilestone": {
"index": 747006,
"timestamp": 1661869119,
"milestoneId": "0x34559d7ba255d1b1127529cf4e28ccc9d5a8da4fcc811d465a8fce8107a0ef53"
},
"confirmedMilestone": {
"index": 747006,
"timestamp": 1661869119,
"milestoneId": "0x34559d7ba255d1b1127529cf4e28ccc9d5a8da4fcc811d465a8fce8107a0ef53"
},
"pruningIndex": 0
},
"supportedProtocolVersions": [
2
],
"protocol": {
"version": 2,
"networkName": "testnet-1",
"bech32Hrp": "rms",
"minPowScore": 1500.0,
"rentStructure": {
"vByteCost": 100,
"vByteFactorKey": 10,
"vByteFactorData": 1
},
"tokenSupply": "1450896407249092"
},
"pendingProtocolParameters": [
],
"baseToken": {
"name": "Shimmer",
"tickerSymbol": "SMR",
"unit": "SMR",
"subunit": "glow",
"decimals": 6,
"useMetricPrefix": false
},
"metrics": {
"blocksPerSecond": 9.0,
"referencedBlocksPerSecond": 8.6,
"referencedRate": 95.55555555555556
},
"features": [
]
}