Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ pub enum Command {
#[clap(value_enum)]
shell: clap_complete::Shell,
},
#[clap(name = "epoch")]
/// Get current epoch
Epoch,
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -915,6 +918,7 @@ fn process_command(opts: Opts) -> Result<()> {
);
Ok(())
}
Command::Epoch => epoch(&opts.cfg_override),
}
}

Expand Down Expand Up @@ -4417,6 +4421,32 @@ fn create_client<U: ToString>(url: U) -> RpcClient {
RpcClient::new_with_commitment(url, CommitmentConfig::confirmed())
}

fn epoch(cfg_override: &ConfigOverride) -> Result<()> {
// Get cluster URL, handling both workspace and standalone scenarios
let cluster_url = match Config::discover(cfg_override) {
Ok(Some(cfg)) => cfg.provider.cluster.url().to_string(),
_ => {
// Not in workspace - check for cluster override or use default
if let Some(ref cluster_override) = cfg_override.cluster {
cluster_override.url().to_string()
} else {
"https://api.mainnet-beta.solana.com".to_string()
}
}
};

// Create RPC client from cluster URL
let client = RpcClient::new(cluster_url);

// Get epoch info
let epoch_info = client.get_epoch_info()?;

// Print just the epoch number (matching original behavior)
println!("{}", epoch_info.epoch);

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down