Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(base_layer): remove unnecessary option + dep #849

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -60,7 +60,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
2 changes: 1 addition & 1 deletion crates/papyrus_base_layer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ pub trait BaseLayerContract {
/// Optionally, require minimum confirmations.
async fn latest_proved_block(
&self,
min_confirmations: Option<u64>,
finality: Option<u64>,
) -> Result<Option<(BlockNumber, BlockHash)>, Self::Error>;
}
Loading