Skip to content

Commit

Permalink
refactor(base_layer): remove unnecessary dep + style
Browse files Browse the repository at this point in the history
commit-id:19ed8b4a
  • Loading branch information
Gilad Chase committed Sep 18, 2024
1 parent 7be89e7 commit 82cf061
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ license-file = "LICENSE"
# TODO: Remove this once udeps is fixed.
alloy-contract = "0.3.5"
alloy-dyn-abi = "0.8.3"
alloy-json-abi = "0.8.3"
alloy-json-rpc = "0.3.5"
alloy-primitives = "0.8.3"
alloy-provider = "0.3.5"
Expand Down
1 change: 0 additions & 1 deletion crates/papyrus_base_layer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ license-file.workspace = true
[dependencies]
alloy-contract.workspace = true
alloy-dyn-abi.workspace = true
alloy-json-abi.workspace = true
alloy-json-rpc.workspace = true
alloy-primitives.workspace = true
alloy-provider.workspace = true
Expand Down
23 changes: 12 additions & 11 deletions crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::future::IntoFuture;

use alloy_contract::{ContractInstance, Interface};
use alloy_dyn_abi::SolType;
use alloy_json_abi::JsonAbi;
use alloy_json_rpc::RpcError;
use alloy_primitives::hex::FromHexError;
use alloy_primitives::Address;
use alloy_provider::network::Ethereum;
use alloy_provider::{Provider, ProviderBuilder, RootProvider};
Expand All @@ -23,17 +24,17 @@ use crate::BaseLayerContract;
#[derive(thiserror::Error, Debug)]
pub enum EthereumBaseLayerError {
#[error(transparent)]
FromHex(#[from] alloy_primitives::hex::FromHexError),
Contract(#[from] alloy_contract::Error),
#[error(transparent)]
Url(#[from] ParseError),
FromHex(#[from] FromHexError),
#[error(transparent)]
Serde(#[from] serde_json::Error),
RpcError(#[from] RpcError<TransportErrorKind>),
#[error(transparent)]
Contract(#[from] alloy_contract::Error),
Serde(#[from] serde_json::Error),
#[error(transparent)]
TypeError(#[from] alloy_sol_types::Error),
#[error(transparent)]
RpcError(#[from] alloy_json_rpc::RpcError<TransportErrorKind>),
Url(#[from] ParseError),
}

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
Expand Down Expand Up @@ -71,18 +72,18 @@ impl Default for EthereumBaseLayerConfig {
}
}

#[derive(Debug)]
pub struct EthereumBaseLayerContract {
contract: ContractInstance<Http<Client>, RootProvider<Http<Client>>, Ethereum>,
}

impl EthereumBaseLayerContract {
pub fn new(config: EthereumBaseLayerConfig) -> Result<Self, EthereumBaseLayerError> {
let address = config.starknet_contract_address.parse::<Address>()?;
let address: Address = config.starknet_contract_address.parse()?;
let client = ProviderBuilder::new().on_http(config.node_url.parse()?);

// The solidity contract was pre-compiled, and only the relevant functions were kept.
let abi: JsonAbi =
serde_json::from_str::<JsonAbi>(include_str!("core_contract_latest_block.abi"))?;
let abi = serde_json::from_str(include_str!("core_contract_latest_block.abi"))?;
Ok(Self { contract: ContractInstance::new(address, client, Interface::new(abi)) })
}
}
Expand All @@ -93,14 +94,14 @@ impl BaseLayerContract for EthereumBaseLayerContract {

async fn latest_proved_block(
&self,
min_confirmations: Option<u64>,
finality: Option<u64>,
) -> Result<Option<(BlockNumber, BlockHash)>, Self::Error> {
let ethereum_block_number = self
.contract
.provider()
.get_block_number()
.await?
.checked_sub(min_confirmations.unwrap_or(0).into());
.checked_sub(finality.unwrap_or_default());
let Some(ethereum_block_number) = ethereum_block_number else {
return Ok(None);
};
Expand Down

0 comments on commit 82cf061

Please sign in to comment.