Skip to content

Commit

Permalink
eyre
Browse files Browse the repository at this point in the history
  • Loading branch information
apoorvsadana committed Jan 10, 2024
1 parent 993b740 commit bb8b942
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 40 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ thiserror = "1.0.52"
tokio = { version = "1.35.1", features = ["rt", "rt-multi-thread", "macros"] }
toml = "0.8.8"
ethers = { git = "https://github.com/gakonst/ethers-rs", rev = "f0e5b194f09c533feb10d1a686ddb9e5946ec107" }
async-trait = "0.1.77"
async-trait = "0.1.77"
eyre = "0.6.11"
2 changes: 2 additions & 0 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub enum RunError {
FailedToRegenerateConfig(String),
#[error("Failed with DA error: {0}")]
FailedWithDaError(#[from] DaError),
#[error(transparent)]
Other(#[from] eyre::Error),
}
pub async fn run() {
match start_app_chain().await {
Expand Down
4 changes: 2 additions & 2 deletions src/da/avail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl DaClient for AvailClient {
}
}

async fn setup(&self, config: &AppChainConfig) -> Result<(), DaError> {
async fn setup(&self, _config: &AppChainConfig) -> eyre::Result<()> {
Ok(())
}
}
Expand All @@ -89,7 +89,7 @@ fn generate_config(da_config_path: &str, seed: &str, address: &str) -> Result<()
};

fs::write(da_config_path, serde_json::to_string(&avail_config).map_err(DaError::FailedToSerializeDaConfig)?)
.map_err(|e| DaError::FailedToWriteDaConfigToFile(e))?;
.map_err(DaError::FailedToWriteDaConfigToFile)?;

Ok(())
}
3 changes: 2 additions & 1 deletion src/da/da_layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::da::ethereum::EthereumError;
use crate::da::no_da::NoDAConfig;
use crate::utils::constants::APP_DA_CONFIG_NAME;
use crate::utils::paths::get_app_home;
use eyre::Result as EyreResult;

#[derive(Debug, Serialize, Deserialize, EnumIter, Display, Clone)]
pub enum DALayer {
Expand Down Expand Up @@ -51,7 +52,7 @@ pub trait DaClient {
Ok(get_app_home(&config.app_chain).map_err(DaError::FailedToReadAppHome)?.join(APP_DA_CONFIG_NAME))
}

async fn setup(&self, config: &AppChainConfig) -> Result<(), DaError>;
async fn setup(&self, config: &AppChainConfig) -> EyreResult<()>;
}

pub struct DAFactory;
Expand Down
52 changes: 18 additions & 34 deletions src/da/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ use crate::app::config::AppChainConfig;
use crate::da::da_layers::{DaClient, DaError};
use crate::utils::serde::bytes_from_hex_str;
use async_trait::async_trait;
use ethers::abi::Contract;
use ethers::contract::{abigen, ContractFactory};
use ethers::core::k256::ecdsa::SigningKey;
use eyre::Result as EyreResult;

use ethers::contract::abigen;

use ethers::middleware::SignerMiddleware;
use ethers::providers::{Http, Provider};
use ethers::signers::{LocalWallet, MnemonicBuilder, Signer, WalletError};
use ethers::utils::Anvil;
use ethers::signers::{LocalWallet, Signer, WalletError};

use serde::{Deserialize, Serialize};
use std::fs;
use std::fs::File;

use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
Expand Down Expand Up @@ -55,25 +56,25 @@ impl DaClient for EthereumClient {
};

fs::write(file_path_str, serde_json::to_string(&ethereum_config).map_err(DaError::FailedToSerializeDaConfig)?)
.map_err(|e| DaError::FailedToWriteDaConfigToFile(e))?;
.map_err(DaError::FailedToWriteDaConfigToFile)?;

Ok(())
}

fn confirm_minimum_balance(&self, config: &AppChainConfig) -> Result<(), DaError> {
fn confirm_minimum_balance(&self, _config: &AppChainConfig) -> Result<(), DaError> {
Ok(())
}

async fn setup(&self, config: &AppChainConfig) -> Result<(), DaError> {
async fn setup(&self, config: &AppChainConfig) -> EyreResult<()> {
let ethereum_config_path = self.get_da_config_path(config)?;
let ethereum_config: EthereumConfig = serde_json::from_str(
fs::read_to_string(ethereum_config_path).map_err(DaError::FailedToReadDaConfigFile)?.as_str(),
)
.map_err(DaError::FailedToDeserializeDaConfig)?;

// get wallet
let wallet = LocalWallet::from_str(&ethereum_config.sequencer_key)
.map_err(|e| EthereumError::FailedToCreateWallet(e))?;
let wallet =
LocalWallet::from_str(&ethereum_config.sequencer_key).map_err(EthereumError::FailedToCreateWallet)?;

// connect to the network
let provider = Provider::<Http>::try_from(ethereum_config.http_provider.as_str())
Expand All @@ -85,11 +86,10 @@ impl DaClient for EthereumClient {

// deploye Starknet core contract
abigen!(Starknet, "src/assets/Starknet.json");
let starknet_contract = Starknet::deploy(client.clone(), ()).unwrap().send().await.unwrap();
let starknet_contract = Starknet::deploy(client.clone(), ())?.send().await?;

abigen!(UnsafeProxy, "src/assets/UnsafeProxy.json");
let proxy_contract =
UnsafeProxy::deploy(client.clone(), starknet_contract.address()).unwrap().send().await.unwrap();
let proxy_contract = UnsafeProxy::deploy(client.clone(), starknet_contract.address())?.send().await?;

abigen!(
StarknetInitializer,
Expand All @@ -102,35 +102,19 @@ impl DaClient for EthereumClient {

let mut bytes = [0u8; 7 * 32];
bytes[32..64].copy_from_slice(
bytes_from_hex_str::<32, true>("0x41fc2a467ef8649580631912517edcab7674173f1dbfa2e9b64fbcd82bc4d79")
.unwrap()
bytes_from_hex_str::<32, true>("0x41fc2a467ef8649580631912517edcab7674173f1dbfa2e9b64fbcd82bc4d79")?
.as_slice(),
);
bytes[96..128].copy_from_slice(
bytes_from_hex_str::<32, true>("0x036f5e4ea4dd042801c8841e3db8e654124305da0f11824fc1db60c405dbb39f")
.unwrap()
bytes_from_hex_str::<32, true>("0x036f5e4ea4dd042801c8841e3db8e654124305da0f11824fc1db60c405dbb39f")?
.as_slice(),
);

// 1. Provide Starknet OS program/config and genesis state
initializer
.initialize(bytes.into())
.send()
.await
.expect("Failed to call `initialize`")
.await
.expect("Ethereum poll update error")
.unwrap();
initializer.initialize(bytes.into()).send().await?.await?;

// 2. Add our EOA as Starknet operator
initializer
.register_operator(wallet.address())
.send()
.await
.expect("Failed to call `register_operator`")
.await
.expect("Ethereum poll update error")
.unwrap();
initializer.register_operator(wallet.address()).send().await?.await?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/da/no_da.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl DaClient for NoDAConfig {
Ok(())
}

async fn setup(&self, config: &AppChainConfig) -> Result<(), DaError> {
async fn setup(&self, _config: &AppChainConfig) -> eyre::Result<()> {
Ok(())
}
}
7 changes: 6 additions & 1 deletion src/utils/serde.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// Copied from starknet_api

#[derive(Clone, Debug)]
use thiserror::Error;

#[derive(Clone, Debug, Error)]
pub enum InnerDeserializationError {
/// Error parsing the hex string.
#[error("error converting from hex")]
FromHex(hex::FromHexError),
/// Missing 0x prefix in the hex string.
#[error("missing hex prefix in {hex_str}")]
MissingPrefix { hex_str: String },
/// Unexpected input byte count.
#[error("bad input when converting to bytes (expected {expected_byte_count} bytes, found {string_found})")]
BadInput { expected_byte_count: usize, string_found: String },
}

Expand Down

0 comments on commit bb8b942

Please sign in to comment.