From 8dc2f64e6436008960598b13802fe8f06ece316e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Gr=C3=BCner?= <47506558+MegaRedHand@users.noreply.github.com> Date: Mon, 6 Jan 2025 17:17:33 -0300 Subject: [PATCH] refactor: move `get_root_index_from_hash` to reader --- .../chainio/clients/elcontracts/src/reader.rs | 49 +++++++++++++++++ .../chainio/clients/elcontracts/src/writer.rs | 53 ------------------- 2 files changed, 49 insertions(+), 53 deletions(-) diff --git a/crates/chainio/clients/elcontracts/src/reader.rs b/crates/chainio/clients/elcontracts/src/reader.rs index de28bfa4..ef799916 100644 --- a/crates/chainio/clients/elcontracts/src/reader.rs +++ b/crates/chainio/clients/elcontracts/src/reader.rs @@ -284,6 +284,41 @@ impl ELChainReader { Ok(cumulative_claimed_for_root_ret) } + /// Get the root index from a given hash. + /// + /// # Arguments + /// + /// * `hash` - The hash to get the root index from. + /// + /// # Returns + /// + /// * `Result` - The root index if the + /// call is successful. + /// + /// # Errors + /// + /// * `ElContractsError` - if the call to the contract fails. + pub async fn get_root_index_from_hash( + &self, + hash: FixedBytes<32>, + ) -> Result { + let provider = get_provider(&self.provider); + + let contract_rewards_coordinator = + IRewardsCoordinator::new(self.rewards_coordinator, &provider); + + let get_root_index_from_hash_call = contract_rewards_coordinator + .getRootIndexFromHash(hash) + .call() + .await + .map_err(ElContractsError::AlloyContractError)?; + + let IRewardsCoordinator::getRootIndexFromHashReturn { _0: root_index } = + get_root_index_from_hash_call; + + Ok(root_index) + } + /// Get the operator's shares in a strategy /// /// # Arguments @@ -1379,6 +1414,20 @@ mod tests { assert_eq!(distribution_root.root, root); } + #[tokio::test] + async fn test_get_root_index_from_hash() { + let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await; + let el_chain_reader = build_el_chain_reader(http_endpoint.to_string()).await; + let (root, _) = new_claim(&http_endpoint).await; + + let index = el_chain_reader + .get_root_index_from_hash(root) + .await + .unwrap(); + + assert_eq!(index, 0); + } + #[tokio::test] async fn test_is_operator_registered() { let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await; diff --git a/crates/chainio/clients/elcontracts/src/writer.rs b/crates/chainio/clients/elcontracts/src/writer.rs index 4729d5ef..2841cf2e 100644 --- a/crates/chainio/clients/elcontracts/src/writer.rs +++ b/crates/chainio/clients/elcontracts/src/writer.rs @@ -261,41 +261,6 @@ impl ELChainWriter { Ok(*tx.tx_hash()) } - /// Get the root index from a given hash. - /// - /// # Arguments - /// - /// * `hash` - The hash to get the root index from. - /// - /// # Returns - /// - /// * `Result` - The root index if the - /// call is successful. - /// - /// # Errors - /// - /// * `ElContractsError` - if the call to the contract fails. - pub async fn get_root_index_from_hash( - &self, - hash: FixedBytes<32>, - ) -> Result { - let provider = get_signer(&self.signer, &self.provider); - - let contract_rewards_coordinator = - IRewardsCoordinator::new(self.rewards_coordinator, &provider); - - let get_root_index_from_hash_call = contract_rewards_coordinator - .getRootIndexFromHash(hash) - .call() - .await - .map_err(ElContractsError::AlloyContractError)?; - - let IRewardsCoordinator::getRootIndexFromHashReturn { _0: root_index } = - get_root_index_from_hash_call; - - Ok(root_index) - } - /// Check if a claim would currently pass the validations in `process_claim` /// /// # Arguments @@ -882,24 +847,6 @@ mod tests { assert!(valid_claim); } - #[tokio::test] - async fn test_get_root_index_from_hash() { - let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await; - let el_chain_writer = new_test_writer( - http_endpoint.to_string(), - ANVIL_FIRST_PRIVATE_KEY.to_string(), - ) - .await; - let (root, _) = new_claim(&http_endpoint).await; - - let index = el_chain_writer - .get_root_index_from_hash(root) - .await - .unwrap(); - - assert_eq!(index, 0); - } - #[tokio::test] async fn test_get_cumulative_claimed() { let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;