diff --git a/Cargo.lock b/Cargo.lock index d61a25b..657f4e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1653,6 +1653,7 @@ dependencies = [ "consensus", "execution", "eyre", + "home", "tokio", ] @@ -1680,6 +1681,15 @@ dependencies = [ "digest 0.10.5", ] +[[package]] +name = "home" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "747309b4b440c06d57b0b25f2aee03ee9b5e5397d288c60e21fc709bb98a7408" +dependencies = [ + "winapi", +] + [[package]] name = "http" version = "0.2.8" diff --git a/Cargo.toml b/Cargo.toml index e199da1..a5cabc1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,9 +27,17 @@ ethers = { git = "https://github.com/ncitron/ethers-rs", branch = "fix-retry" } [dev-dependencies] tokio = { version = "1", features = ["full"] } eyre = "0.6.8" +home = "0.5.4" [[example]] name = "checkpoints" path = "examples/checkpoints.rs" -required-features = [] + +[[example]] +name = "client" +path = "examples/client.rs" + +[[example]] +name = "config" +path = "examples/config.rs" diff --git a/README.md b/README.md index 495886b..b6129c8 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,8 @@ async fn main() -> Result<()> { // Fetch the latest mainnet checkpoint let mainnet_checkpoint = cf.fetch_latest_checkpoint(&networks::Network::MAINNET).await.unwrap(); println!("Fetched latest mainnet checkpoint: {}", mainnet_checkpoint); + + Ok(()) } ``` diff --git a/client/src/client.rs b/client/src/client.rs index f879a98..23c435c 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -53,7 +53,8 @@ impl ClientBuilder { } pub fn checkpoint(mut self, checkpoint: &str) -> Self { - let checkpoint = hex::decode(checkpoint).expect("cannot parse checkpoint"); + let checkpoint = hex::decode(checkpoint.strip_prefix("0x").unwrap_or(checkpoint)) + .expect("cannot parse checkpoint"); self.checkpoint = Some(checkpoint); self } diff --git a/config/src/cli.rs b/config/src/cli.rs index 2afca4d..6c88f06 100644 --- a/config/src/cli.rs +++ b/config/src/cli.rs @@ -1,10 +1,10 @@ use std::{collections::HashMap, path::PathBuf}; use figment::{providers::Serialized, value::Value}; -use serde::Serialize; +use serde::{Deserialize, Serialize}; /// Cli Config -#[derive(Serialize)] +#[derive(Serialize, Deserialize, Debug, Clone, Default)] pub struct CliConfig { pub execution_rpc: Option, pub consensus_rpc: Option, diff --git a/examples/client.rs b/examples/client.rs new file mode 100644 index 0000000..cbd3013 --- /dev/null +++ b/examples/client.rs @@ -0,0 +1,42 @@ +use std::path::PathBuf; + +use eyre::Result; + +use helios::prelude::*; + +#[tokio::main] +async fn main() -> Result<()> { + // Create a new Helios Client Builder + let mut builder = ClientBuilder::new(); + + // Set the network to mainnet + builder = builder.network(networks::Network::MAINNET); + + // Set the consensus rpc url + builder = builder.consensus_rpc("https://www.lightclientdata.org"); + + // Set the execution rpc url + builder = builder.execution_rpc("https://eth-mainnet.g.alchemy.com/v2/XXXXX"); + + // Set the checkpoint to the last known checkpoint + builder = + builder.checkpoint("85e6151a246e8fdba36db27a0c7678a575346272fe978c9281e13a8b26cdfa68"); + + // Set the rpc port + builder = builder.rpc_port(8545); + + // Set the data dir + builder = builder.data_dir(PathBuf::from("/tmp/helios")); + + // Set the fallback service + builder = builder.fallback("https://sync-mainnet.beaconcha.in"); + + // Enable lazy checkpoints + builder = builder.load_external_fallback(); + + // Build the client + let _client = builder.build().unwrap(); + println!("Constructed client!"); + + Ok(()) +} diff --git a/examples/config.rs b/examples/config.rs new file mode 100644 index 0000000..c055477 --- /dev/null +++ b/examples/config.rs @@ -0,0 +1,14 @@ +use config::CliConfig; +use eyre::Result; + +use helios::prelude::*; + +#[tokio::main] +async fn main() -> Result<()> { + // Load the config from the global config file + let config_path = home::home_dir().unwrap().join(".helios/helios.toml"); + let config = Config::from_file(&config_path, "mainnet", &CliConfig::default()); + println!("Constructed config: {:#?}", config); + + Ok(()) +}