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

feat(base_layer): add types to config #855

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: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/papyrus_base_layer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ serde_json.workspace = true
starknet_api.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["full", "sync"] }
url.workspace = true
url = { workspace = true, features = ["serde"] }

[dev-dependencies]
ethers-core.workspace = true
Expand Down
15 changes: 10 additions & 5 deletions crates/papyrus_base_layer/src/base_layer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ use starknet_api::felt;
use tar::Archive;
use tempfile::{tempdir, TempDir};

use crate::ethereum_base_layer_contract::{EthereumBaseLayerConfig, EthereumBaseLayerContract};
use crate::ethereum_base_layer_contract::{
EthereumBaseLayerConfig,
EthereumBaseLayerContract,
EthereumContractAddress,
};
use crate::BaseLayerContract;

type EthereumContractAddress = String;
type TestEthereumNodeHandle = (GanacheInstance, TempDir);

const MINIMAL_GANACHE_VERSION: u8 = 7;
Expand Down Expand Up @@ -65,16 +68,18 @@ fn get_test_ethereum_node() -> (TestEthereumNodeHandle, EthereumContractAddress)
let db_path = ganache_db.path().join(DB_NAME);
let ganache = Ganache::new().args(["--db", db_path.to_str().unwrap()]).spawn();

((ganache, ganache_db), SN_CONTRACT_ADDR.to_owned())
((ganache, ganache_db), SN_CONTRACT_ADDR.to_string().parse().unwrap())
}

#[test_with::executable(ganache)]
#[tokio::test]
// Note: the test requires ganache-cli installed, otherwise it is ignored.
async fn latest_proved_block_ethereum() {
let (node_handle, starknet_contract_address) = get_test_ethereum_node();
let config =
EthereumBaseLayerConfig { node_url: node_handle.0.endpoint(), starknet_contract_address };
let config = EthereumBaseLayerConfig {
node_url: node_handle.0.endpoint().parse().unwrap(),
starknet_contract_address,
};
let contract = EthereumBaseLayerContract::new(config).unwrap();

let first_sn_state_update = (BlockNumber(100), BlockHash(felt!("0x100")));
Expand Down
34 changes: 18 additions & 16 deletions crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::future::IntoFuture;
use alloy_contract::{ContractInstance, Interface};
use alloy_dyn_abi::SolType;
use alloy_json_rpc::RpcError;
use alloy_primitives::hex::FromHexError;
use alloy_primitives::Address;
pub(crate) use alloy_primitives::Address as EthereumContractAddress;
use alloy_provider::network::Ethereum;
use alloy_provider::{Provider, ProviderBuilder, RootProvider};
use alloy_sol_types::sol_data;
Expand All @@ -17,7 +16,7 @@ use papyrus_config::{ParamPath, ParamPrivacyInput, SerializationType, Serialized
use serde::{Deserialize, Serialize};
use starknet_api::block::{BlockHash, BlockNumber};
use starknet_api::hash::StarkHash;
use url::ParseError;
use url::Url;

use crate::BaseLayerContract;

Expand All @@ -26,22 +25,17 @@ pub enum EthereumBaseLayerError {
#[error(transparent)]
Contract(#[from] alloy_contract::Error),
#[error(transparent)]
FromHex(#[from] FromHexError),
#[error(transparent)]
RpcError(#[from] RpcError<TransportErrorKind>),
#[error(transparent)]
Serde(#[from] serde_json::Error),
#[error(transparent)]
TypeError(#[from] alloy_sol_types::Error),
#[error(transparent)]
Url(#[from] ParseError),
}

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
pub struct EthereumBaseLayerConfig {
// TODO(yair): consider using types.
pub node_url: String,
pub starknet_contract_address: String,
pub node_url: Url,
pub starknet_contract_address: EthereumContractAddress,
}

impl SerializeConfig for EthereumBaseLayerConfig {
Expand All @@ -55,7 +49,7 @@ impl SerializeConfig for EthereumBaseLayerConfig {
),
ser_param(
"starknet_contract_address",
&self.starknet_contract_address,
&self.starknet_contract_address.to_string(),
"Starknet contract address in ethereum.",
ParamPrivacyInput::Public,
),
Expand All @@ -65,9 +59,12 @@ impl SerializeConfig for EthereumBaseLayerConfig {

impl Default for EthereumBaseLayerConfig {
fn default() -> Self {
let starknet_contract_address =
"0xc662c410C0ECf747543f5bA90660f6ABeBD9C8c4".parse().unwrap();

Self {
node_url: "https://mainnet.infura.io/v3/<your_api_key>".to_string(),
starknet_contract_address: "0xc662c410C0ECf747543f5bA90660f6ABeBD9C8c4".to_string(),
node_url: "https://mainnet.infura.io/v3/<your_api_key>".parse().unwrap(),
starknet_contract_address,
}
}
}
Expand All @@ -79,12 +76,17 @@ pub struct EthereumBaseLayerContract {

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

// The solidity contract was pre-compiled, and only the relevant functions were kept.
let abi = serde_json::from_str(include_str!("core_contract_latest_block.abi"))?;
Ok(Self { contract: ContractInstance::new(address, client, Interface::new(abi)) })
Ok(Self {
contract: ContractInstance::new(
config.starknet_contract_address,
client,
Interface::new(abi),
),
})
}
}

Expand Down
Loading