Skip to content

Commit

Permalink
feat(base_layer): add types to config
Browse files Browse the repository at this point in the history
- implement the TODO for adding types to the Config, rather than working
  with raw strings.
- using `Url` for url and **re-exporting** `alloy`'s `Address` type in order
  to not externalize this implementation detail type unnecessarily.
- Remove 2 errors that are now no longer the responsibility of this
  module, the callsite will have to handle the casting into the correct
  types.

commit-id:0a043060
  • Loading branch information
Gilad Chase committed Sep 19, 2024
1 parent 87ae7b8 commit d70e877
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 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.

4 changes: 2 additions & 2 deletions crates/papyrus_base_layer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ alloy-json-rpc.workspace = true
alloy-primitives.workspace = true
alloy-provider.workspace = true
alloy-sol-types.workspace = true
alloy-transport.workspace = true
alloy-transport-http.workspace = true
alloy-transport.workspace = true
async-trait.workspace = true
ethers.workspace = true
papyrus_config.workspace = true
Expand All @@ -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

0 comments on commit d70e877

Please sign in to comment.