⚙️ examples

This commit is contained in:
Andreas Bigger 2022-12-02 08:24:03 -08:00
parent 2b1aeba534
commit cda8f39b4c
7 changed files with 81 additions and 4 deletions

10
Cargo.lock generated
View File

@ -1653,6 +1653,7 @@ dependencies = [
"consensus", "consensus",
"execution", "execution",
"eyre", "eyre",
"home",
"tokio", "tokio",
] ]
@ -1680,6 +1681,15 @@ dependencies = [
"digest 0.10.5", "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]] [[package]]
name = "http" name = "http"
version = "0.2.8" version = "0.2.8"

View File

@ -27,9 +27,17 @@ ethers = { git = "https://github.com/ncitron/ethers-rs", branch = "fix-retry" }
[dev-dependencies] [dev-dependencies]
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
eyre = "0.6.8" eyre = "0.6.8"
home = "0.5.4"
[[example]] [[example]]
name = "checkpoints" name = "checkpoints"
path = "examples/checkpoints.rs" path = "examples/checkpoints.rs"
required-features = []
[[example]]
name = "client"
path = "examples/client.rs"
[[example]]
name = "config"
path = "examples/config.rs"

View File

@ -142,6 +142,8 @@ async fn main() -> Result<()> {
// Fetch the latest mainnet checkpoint // Fetch the latest mainnet checkpoint
let mainnet_checkpoint = cf.fetch_latest_checkpoint(&networks::Network::MAINNET).await.unwrap(); let mainnet_checkpoint = cf.fetch_latest_checkpoint(&networks::Network::MAINNET).await.unwrap();
println!("Fetched latest mainnet checkpoint: {}", mainnet_checkpoint); println!("Fetched latest mainnet checkpoint: {}", mainnet_checkpoint);
Ok(())
} }
``` ```

View File

@ -53,7 +53,8 @@ impl ClientBuilder {
} }
pub fn checkpoint(mut self, checkpoint: &str) -> Self { 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.checkpoint = Some(checkpoint);
self self
} }

View File

@ -1,10 +1,10 @@
use std::{collections::HashMap, path::PathBuf}; use std::{collections::HashMap, path::PathBuf};
use figment::{providers::Serialized, value::Value}; use figment::{providers::Serialized, value::Value};
use serde::Serialize; use serde::{Deserialize, Serialize};
/// Cli Config /// Cli Config
#[derive(Serialize)] #[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct CliConfig { pub struct CliConfig {
pub execution_rpc: Option<String>, pub execution_rpc: Option<String>,
pub consensus_rpc: Option<String>, pub consensus_rpc: Option<String>,

42
examples/client.rs Normal file
View File

@ -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(())
}

14
examples/config.rs Normal file
View File

@ -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(())
}