Connecting to a Node
Open a JSON-RPC connection to an Aztec node and verify health.
Basic Connection
#![allow(unused)]
fn main() {
use aztec_rs::node::{create_aztec_node_client, wait_for_node, AztecNode};
async fn run() -> Result<(), aztec_rs::Error> {
let node = create_aztec_node_client("http://localhost:8080");
let info = wait_for_node(&node).await?;
println!("node v{}", info.node_version);
Ok(()) }
}
wait_for_node polls until the node responds and returns NodeInfo.
Overriding the URL
The built-in examples read AZTEC_NODE_URL from the environment.
You can follow the same pattern in your own code:
#![allow(unused)]
fn main() {
let url = std::env::var("AZTEC_NODE_URL")
.unwrap_or_else(|_| "http://localhost:8080".to_string());
}
Querying State
#![allow(unused)]
fn main() {
use aztec_rs::node::{create_aztec_node_client, AztecNode};
async fn run() -> Result<(), aztec_rs::Error> {
let node = create_aztec_node_client("http://localhost:8080");
let block = node.get_block_number().await?;
let _ = block; Ok(()) }
}
Full Runnable Example
Source: examples/node_info.rs.
//! Connect to a local Aztec network and print basic node metadata.
//!
//! Run with:
//! ```bash
//! aztec start --local-network
//! cargo run --example node_info
//! ```
#![allow(clippy::print_stdout, clippy::wildcard_imports)]
mod common;
use common::*;
#[tokio::main]
async fn main() -> Result<(), aztec_rs::Error> {
let url = node_url();
let node = create_aztec_node_client(&url);
let info = wait_for_node(&node).await?;
println!("Node URL: {url}");
println!("Node version: {}", info.node_version);
println!("L1 chain ID: {}", info.l1_chain_id);
println!("Rollup version: {}", info.rollup_version);
println!("Real proofs: {}", info.real_proofs);
println!("Current block: {}", node.get_block_number().await?);
println!(
"Proven block: {}",
node.get_proven_block_number().await?
);
if let Some(enr) = info.enr {
println!("ENR: {enr}");
}
if let Some(inbox) = info
.l1_contract_addresses
.get("inboxAddress")
.and_then(|value| value.as_str())
{
println!("L1 inbox: {inbox}");
}
Ok(())
}