Skip to content

Commit

Permalink
Refactor: Init Beerus from config only (#520)
Browse files Browse the repository at this point in the history
* refactor beerus ctor

* refactor after fetch main

* refactor README.md

* add test of new ctor to keep test coverage

* clean up

---------

Co-authored-by: drspacemn <dontpanicdao@gmail.com>
  • Loading branch information
tonypony220 and drspacemn authored Aug 7, 2023
1 parent bde8f66 commit b743176
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 209 deletions.
17 changes: 3 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,29 +241,18 @@ BEERUS_CONFIG=path/to/config.toml cargo run -p beerus-core --example basic
Beerus can be imported into any Rust project.

```rust
use beerus_core::{config::Config, lightclient::beerus::BeerusLightClient};
use env_logger::Env;
use eyre::Result;
use beerus_core::{
config::Config,
lightclient::{
beerus::BeerusLightClient, ethereum::helios_lightclient::HeliosLightClient,
starknet::StarkNetLightClientImpl,
},
};

#[tokio::main]
async fn main() -> Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let config = Config::from_env();

let ethereum_lightclient = HeliosLightClient::new(config.clone()).await?;
let starknet_lightclient = StarkNetLightClientImpl::new(&config)?;
let mut beerus = BeerusLightClient::new(
config.clone(),
Box::new(ethereum_lightclient),
Box::new(starknet_lightclient),
);
let mut beerus = BeerusLightClient::new(config.clone()).await?;
beerus.start().await?;

let current_starknet_block = beerus.starknet_lightclient.block_number().await?;
println!("{:?}", current_starknet_block);

Expand Down
16 changes: 2 additions & 14 deletions crates/beerus-core/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
use beerus_core::{
config::Config,
lightclient::{
beerus::BeerusLightClient, ethereum::helios_lightclient::HeliosLightClient,
starknet::StarkNetLightClientImpl,
},
};
use beerus_core::{config::Config, lightclient::beerus::BeerusLightClient};
use env_logger::Env;
use eyre::Result;

Expand All @@ -13,13 +7,7 @@ async fn main() -> Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let config = Config::from_env();

let ethereum_lightclient = HeliosLightClient::new(config.clone()).await?;
let starknet_lightclient = StarkNetLightClientImpl::new(&config)?;
let mut beerus = BeerusLightClient::new(
config.clone(),
Box::new(ethereum_lightclient),
Box::new(starknet_lightclient),
);
let mut beerus = BeerusLightClient::new(config.clone()).await?;
beerus.start().await?;

let current_starknet_block = beerus.starknet_lightclient.block_number().await?;
Expand Down
16 changes: 2 additions & 14 deletions crates/beerus-core/examples/call.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
use beerus_core::{
config::Config,
lightclient::{
beerus::BeerusLightClient, ethereum::helios_lightclient::HeliosLightClient,
starknet::StarkNetLightClientImpl,
},
};
use beerus_core::{config::Config, lightclient::beerus::BeerusLightClient};
use env_logger::Env;
use eyre::Result;
use starknet::{
Expand All @@ -18,13 +12,7 @@ async fn main() -> Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();

let config = Config::from_env();
let ethereum_lightclient = HeliosLightClient::new(config.clone()).await?;
let starknet_lightclient = StarkNetLightClientImpl::new(&config)?;
let mut beerus = BeerusLightClient::new(
config.clone(),
Box::new(ethereum_lightclient),
Box::new(starknet_lightclient),
);
let mut beerus = BeerusLightClient::new(config.clone()).await?;
beerus.start().await?;

let starkgate_addr = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7";
Expand Down
16 changes: 2 additions & 14 deletions crates/beerus-core/examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
use beerus_core::{
config::Config,
lightclient::{
beerus::BeerusLightClient, ethereum::helios_lightclient::HeliosLightClient,
starknet::StarkNetLightClientImpl,
},
};
use beerus_core::{config::Config, lightclient::beerus::BeerusLightClient};
use env_logger::Env;
use eyre::Result;
use std::env;
Expand Down Expand Up @@ -44,13 +38,7 @@ async fn main() -> Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
let config = Config::from_env();

let ethereum_lightclient = HeliosLightClient::new(config.clone()).await?;
let starknet_lightclient = StarkNetLightClientImpl::new(&config)?;
let _beerus = BeerusLightClient::new(
config.clone(),
Box::new(ethereum_lightclient),
Box::new(starknet_lightclient),
);
let _beerus = BeerusLightClient::new(config.clone()).await?;
println!("Constructed Beerus client!");
Ok(())
}
29 changes: 26 additions & 3 deletions crates/beerus-core/src/lightclient/beerus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ use crate::stdlib::vec::Vec;
use crate::stdlib::{collections::BTreeMap, sync::Arc};

use super::{ethereum::EthereumLightClient, starknet::StarkNetLightClient};
use crate::{config::Config, ethers_helper};
use crate::{
config::Config,
ethers_helper,
lightclient::{
ethereum::helios_lightclient::HeliosLightClient, starknet::StarkNetLightClientImpl,
},
};
use ethabi::Uint as U256;
use ethers::{abi::Abi, types::H160};
use eyre::Result as EyreResult;
Expand Down Expand Up @@ -87,11 +93,28 @@ pub struct BeerusLightClient {

impl BeerusLightClient {
/// Create a new Beerus Light Client service.
pub fn new(
pub async fn new(config: Config) -> EyreResult<Self> {
info!("creating Ethereum(Helios) lightclient...");
let ethereum_lightclient_raw = HeliosLightClient::new(config.clone()).await?;

info!("creating Starknet lightclient...");
let starknet_lightclient_raw = StarkNetLightClientImpl::new(&config)?;

let beerus = BeerusLightClient::new_from_clients(
config.clone(),
Box::new(ethereum_lightclient_raw),
Box::new(starknet_lightclient_raw),
);
Ok(beerus)
}

/// Create a new Beerus Light Client service with the provided
/// custom Ethereum and Starknet clients
pub fn new_from_clients(
config: Config,
//TODO: Check if we should just have &str as arguments
ethereum_lightclient_raw: Box<dyn EthereumLightClient>,
starknet_lightclient_raw: Box<dyn StarkNetLightClient>,
//TODO: Check if we should just have &str as arguments
) -> Self {
// Create a new Ethereum light client.
let ethereum_lightclient = Arc::new(Mutex::new(ethereum_lightclient_raw));
Expand Down
Loading

0 comments on commit b743176

Please sign in to comment.