Skip to content

Commit

Permalink
refactor: move get_current_claimable_distribution_root to reader
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand committed Jan 6, 2025
1 parent 4b60796 commit d9ab2df
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 51 deletions.
52 changes: 51 additions & 1 deletion crates/chainio/clients/elcontracts/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use eigen_utils::{
delegationmanager::DelegationManager,
erc20::ERC20::{self, ERC20Instance},
get_provider,
irewardscoordinator::IRewardsCoordinator,
irewardscoordinator::{IRewardsCoordinator, IRewardsCoordinatorTypes::DistributionRoot},
istrategy::IStrategy::{self, IStrategyInstance},
permissioncontroller::PermissionController,
SdkProvider,
Expand Down Expand Up @@ -256,6 +256,34 @@ impl ELChainReader {
Ok(end_timestamp)
}

/// Get the latest claimable distribution root.
///
/// # Returns
/// * `Result<DistributionRoot, ElContractsError>` - The latest claimable distribution root if the call is successful.
///
/// # Errors
/// * `ElContractsError` - if the call to the contract fails.
pub async fn get_current_claimable_distribution_root(
&self,
) -> Result<DistributionRoot, ElContractsError> {
let provider = get_provider(&self.provider);

let contract_rewards_coordinator =
IRewardsCoordinator::new(self.rewards_coordinator, &provider);

let cumulative_claimed_for_root_call = contract_rewards_coordinator
.getCurrentClaimableDistributionRoot()
.call()
.await
.map_err(ElContractsError::AlloyContractError)?;

let IRewardsCoordinator::getCurrentClaimableDistributionRootReturn {
_0: cumulative_claimed_for_root_ret,
} = cumulative_claimed_for_root_call;

Ok(cumulative_claimed_for_root_ret)
}

/// Get the operator's shares in a strategy
///
/// # Arguments
Expand Down Expand Up @@ -1329,6 +1357,28 @@ mod tests {
assert_eq!(end_timestamp, 1);
}

#[tokio::test]
async fn test_get_current_claimable_distribution_root() {
let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
let el_chain_reader = build_el_chain_reader(http_endpoint.to_string()).await;

let distribution_root = el_chain_reader
.get_current_claimable_distribution_root()
.await
.unwrap();
// The root starts being zero
assert_eq!(distribution_root.root, FixedBytes::ZERO);

let (root, _) = new_claim(&http_endpoint).await;

let distribution_root = el_chain_reader
.get_current_claimable_distribution_root()
.await
.unwrap();

assert_eq!(distribution_root.root, root);
}

#[tokio::test]
async fn test_is_operator_registered() {
let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
Expand Down
51 changes: 1 addition & 50 deletions crates/chainio/clients/elcontracts/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ use eigen_utils::{
delegationmanager::DelegationManager,
erc20::ERC20,
get_signer,
irewardscoordinator::{
IRewardsCoordinator,
IRewardsCoordinatorTypes::{self, RewardsMerkleClaim},
},
irewardscoordinator::{IRewardsCoordinator, IRewardsCoordinatorTypes::RewardsMerkleClaim},
permissioncontroller::PermissionController,
registrycoordinator::{IBLSApkRegistry::PubkeyRegistrationParams, RegistryCoordinator},
strategymanager::StrategyManager,
Expand Down Expand Up @@ -264,34 +261,6 @@ impl ELChainWriter {
Ok(*tx.tx_hash())
}

/// Get the latest claimable distribution root.
///
/// # Returns
/// * `Result<DistributionRoot, ElContractsError>` - The latest claimable distribution root if the call is successful.
///
/// # Errors
/// * `ElContractsError` - if the call to the contract fails.
pub async fn get_current_claimable_distribution_root(
&self,
) -> Result<IRewardsCoordinatorTypes::DistributionRoot, ElContractsError> {
let provider = get_signer(&self.signer, &self.provider);

let contract_rewards_coordinator =
IRewardsCoordinator::new(self.rewards_coordinator, &provider);

let cumulative_claimed_for_root_call = contract_rewards_coordinator
.getCurrentClaimableDistributionRoot()
.call()
.await
.map_err(ElContractsError::AlloyContractError)?;

let IRewardsCoordinator::getCurrentClaimableDistributionRootReturn {
_0: cumulative_claimed_for_root_ret,
} = cumulative_claimed_for_root_call;

Ok(cumulative_claimed_for_root_ret)
}

/// Get the root index from a given hash.
///
/// # Arguments
Expand Down Expand Up @@ -958,24 +927,6 @@ mod tests {
assert_eq!(cumulative_claimed_ret, U256::from(0));
}

#[tokio::test]
async fn test_get_cumulative_claimed_for_root() {
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 distribution_root = el_chain_writer
.get_current_claimable_distribution_root()
.await
.unwrap();

assert_eq!(distribution_root.root, root);
}

#[tokio::test]
async fn test_add_and_remove_pending_admin() {
let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
Expand Down

0 comments on commit d9ab2df

Please sign in to comment.