Skip to content

Commit

Permalink
refactor: move get_root_index_from_hash to reader
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand committed Jan 6, 2025
1 parent d9ab2df commit 8dc2f64
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 53 deletions.
49 changes: 49 additions & 0 deletions crates/chainio/clients/elcontracts/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32, ElContractsError>` - 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<u32, ElContractsError> {
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
Expand Down Expand Up @@ -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;
Expand Down
53 changes: 0 additions & 53 deletions crates/chainio/clients/elcontracts/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32, ElContractsError>` - 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<u32, ElContractsError> {
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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8dc2f64

Please sign in to comment.