Skip to content

Commit

Permalink
Use function
Browse files Browse the repository at this point in the history
Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo committed Nov 21, 2024
1 parent 7f9a999 commit a6f57cb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 62 deletions.
2 changes: 1 addition & 1 deletion contracts
72 changes: 11 additions & 61 deletions zkstack_cli/crates/common/src/ethereum/chain_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,21 @@ use std::sync::Arc;
use ethers::{
abi::Address,
contract::abigen,
middleware::Middleware,
prelude::{Http, Provider},
types::{BlockNumber, H256, U64},
};

abigen!(
ChainRegistar,
r"[
struct RegisteredChainConfig {address pendingChainAdmin;address chainAdmin;address diamondProxy;address l2BridgeAddress;}
function getRegisteredChainConfig(uint256 chainId) public view returns (RegisteredChainConfig memory)
event NewChainDeployed(uint256 indexed chainId, address author, address diamondProxy, address chainAdmin)
event NewChainRegistrationProposal(uint256 indexed chainId, address author, bytes32 key)
event SharedBridgeRegistered(uint256 indexed chainId, address l2Address)
]"
);

#[derive(Clone, Copy)]
struct ChainRegistrationResultBuilder {
pub diamond_proxy: Option<Address>,
pub chain_admin: Option<Address>,
pub l2_shared_bridge: Option<Address>,
}

impl ChainRegistrationResultBuilder {
fn new() -> Self {
Self {
diamond_proxy: None,
chain_admin: None,
l2_shared_bridge: None,
}
}
fn with_diamond_proxy(mut self, address: Address) -> Self {
self.diamond_proxy = Some(address);
self
}
fn with_chain_admin(mut self, address: Address) -> Self {
self.chain_admin = Some(address);
self
}

fn with_l2_shared_bridge(mut self, address: Address) -> Self {
self.l2_shared_bridge = Some(address);
self
}

fn build(self) -> ChainRegistrationResult {
ChainRegistrationResult {
diamond_proxy: self.diamond_proxy.unwrap(),
chain_admin: self.chain_admin.unwrap(),
l2_shared_bridge: self.l2_shared_bridge.unwrap(),
}
}
}

pub struct ChainRegistrationResult {
pub diamond_proxy: Address,
pub chain_admin: Address,
Expand All @@ -67,27 +30,14 @@ pub async fn load_contracts_for_chain(
l2_chain_id: u64,
) -> anyhow::Result<ChainRegistrationResult> {
let client = Arc::new(Provider::<Http>::try_from(l1_rpc)?);
let block = client.get_block_number().await?;
let contract = ChainRegistar::new(chain_registrar, client);
let events = contract
.events()
.from_block(block.saturating_sub(U64::from(100000)))
.to_block(BlockNumber::Latest)
.topic1(H256::from_low_u64_be(l2_chain_id));
let result = events.query().await?;
let mut builder = ChainRegistrationResultBuilder::new();
for event in &result {
match event {
ChainRegistarEvents::NewChainDeployedFilter(event) => {
builder = builder
.with_diamond_proxy(event.diamond_proxy)
.with_chain_admin(event.chain_admin);
}
ChainRegistarEvents::SharedBridgeRegisteredFilter(event) => {
builder = builder.with_l2_shared_bridge(event.l_2_address);
}
_ => {}
}
}
Ok(builder.build())
let (pending_chain_admin, _chain_admin, diamond_proxy, l2_bridge_address) = contract
.get_registered_chain_config(l2_chain_id.into())
.await?;

Ok(ChainRegistrationResult {
diamond_proxy,
chain_admin: pending_chain_admin,
l2_shared_bridge: l2_bridge_address,
})
}

0 comments on commit a6f57cb

Please sign in to comment.