Skip to content

Commit

Permalink
refactor: move get_distribution_roots_length to reader
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaRedHand committed Jan 6, 2025
1 parent f5aa131 commit 4618bce
Show file tree
Hide file tree
Showing 3 changed files with 293 additions and 259 deletions.
133 changes: 133 additions & 0 deletions crates/chainio/clients/elcontracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,136 @@
pub mod error;
pub mod reader;
pub mod writer;

#[cfg(test)]
pub(crate) mod test_utils {
use alloy::hex::FromHex;
use alloy_primitives::{address, Address, Bytes, FixedBytes, U256};
use eigen_logging::get_test_logger;
use eigen_testing_utils::anvil_constants::{
get_allocation_manager_address, get_avs_directory_address, get_delegation_manager_address,
get_registry_coordinator_address, get_rewards_coordinator_address,
get_strategy_manager_address,
};
use eigen_utils::{
delegationmanager::DelegationManager,
get_provider, get_signer,
irewardscoordinator::{
IRewardsCoordinator,
IRewardsCoordinatorTypes::{
EarnerTreeMerkleLeaf, RewardsMerkleClaim, TokenTreeMerkleLeaf,
},
},
};
use std::str::FromStr;

use crate::{reader::ELChainReader, writer::ELChainWriter};

pub const OPERATOR_ADDRESS: Address = address!("70997970C51812dc3A010C7d01b50e0d17dc79C8");

pub async fn build_el_chain_reader(http_endpoint: String) -> ELChainReader {
let delegation_manager_address =
get_delegation_manager_address(http_endpoint.clone()).await;
let avs_directory_address = get_avs_directory_address(http_endpoint.clone()).await;
let rewards_coordinator = get_rewards_coordinator_address(http_endpoint.clone()).await;

ELChainReader::build(
get_test_logger().clone(),
delegation_manager_address,
avs_directory_address,
rewards_coordinator,
&http_endpoint,
)
.await
.unwrap()
}

pub const ANVIL_FIRST_ADDRESS: Address = address!("f39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
pub const ANVIL_FIRST_PRIVATE_KEY: &str =
"ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";

pub async fn new_test_writer(http_endpoint: String, private_key: String) -> ELChainWriter {
let el_chain_reader = build_el_chain_reader(http_endpoint.clone()).await;
let strategy_manager = get_strategy_manager_address(http_endpoint.clone()).await;
let rewards_coordinator = get_rewards_coordinator_address(http_endpoint.clone()).await;
let delegation_manager = get_delegation_manager_address(http_endpoint.clone()).await;
let allocation_manager = get_allocation_manager_address(http_endpoint.clone()).await;
let contract_delegation_manager =
DelegationManager::new(delegation_manager, get_provider(&http_endpoint));
let permission_controller = contract_delegation_manager
.permissionController()
.call()
.await
.unwrap()
._0;
let registry_coordinator = get_registry_coordinator_address(http_endpoint.clone()).await;

ELChainWriter::new(
delegation_manager,
strategy_manager,
rewards_coordinator,
permission_controller,
allocation_manager,
registry_coordinator,
el_chain_reader,
http_endpoint.clone(),
private_key,
)
}

pub async fn new_claim(http_endpoint: &str) -> (FixedBytes<32>, RewardsMerkleClaim) {
let el_chain_writer = new_test_writer(
http_endpoint.to_string(),
ANVIL_FIRST_PRIVATE_KEY.to_string(),
)
.await;

let earner_address = address!("25a1b7322f9796b26a4bec125913b34c292b28d6");
let claim = RewardsMerkleClaim {
rootIndex: 0,
earnerIndex: 7,
earnerTreeProof: Bytes::from_hex("4bf5e16eaabbc36964f1e1639808669420f55d60e51adb7e9695b77145c479fd6777be59643947bb24d78e69d6605bf369c515b479f3a8967dd68a97c5bb4a4a262b28002eeb6cbbffb7e79e5741bf2be189a6073440a62fabcd8af4dbda94e3").unwrap(),
earnerLeaf: EarnerTreeMerkleLeaf {
earner: earner_address,
earnerTokenRoot: FixedBytes::from_hex(
"f8e7e20b32aae1d818dcb593b98982841e9a0ed12c161ad603e3ee3948746cba",
)
.unwrap(),
},
tokenIndices: vec![7],
tokenTreeProofs: vec![
Bytes::from_hex("3cd04e8fc6f23812c570fe12292a30bb9e105e00f5913ac4b4938f23e65d8d10e6b1403d58c9d5450952e7d96c81305dad9fb966e8a27d3a42058e3958a0d30033148e91b455542d05deb81b8305b672e742cd3145f7022a0089bad2e6af9173").unwrap(),
],
tokenLeaves: vec![TokenTreeMerkleLeaf {
token: address!("7fbfdd1dfd80730385aee232cc9f79b8ae12a654"),
cumulativeEarnings: U256::from_str("3000000000000000000").unwrap(),
}],
};

// Using test data taken from
// https://github.com/Layr-Labs/eigenlayer-contracts/blob/a888a1cd1479438dda4b138245a69177b125a973/src/test/test-data/rewardsCoordinator/processClaimProofs_MaxEarnerAndLeafIndices.json
let root = FixedBytes::from_hex(
"37550707c80f3d8907c467999730e52127ab89be3f17a5017a3f1ffb73a1445f",
)
.unwrap();

let key = "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
let rewards_coordinator = IRewardsCoordinator::new(
get_rewards_coordinator_address(http_endpoint.to_string()).await,
get_signer(key, http_endpoint),
);
let curr_rewards_calculation_end_timestamp = el_chain_writer
.get_curr_rewards_calculation_end_timestamp()
.await
.unwrap();
let submit_tx = rewards_coordinator
.submitRoot(root, curr_rewards_calculation_end_timestamp + 1)
.send()
.await
.unwrap();
let submit_status = submit_tx.get_receipt().await.unwrap().status();
assert!(submit_status);

(root, claim)
}
}
81 changes: 45 additions & 36 deletions crates/chainio/clients/elcontracts/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use eigen_utils::{
delegationmanager::DelegationManager,
erc20::ERC20::{self, ERC20Instance},
get_provider,
irewardscoordinator::IRewardsCoordinator,
istrategy::IStrategy::{self, IStrategyInstance},
permissioncontroller::PermissionController,
SdkProvider,
Expand Down Expand Up @@ -199,6 +200,35 @@ impl ELChainReader {
Ok(avs_hash)
}

/// Get the length of the distribution roots.
///
/// # Returns
///
/// * `Result<U256, ElContractsError>` - The length of the distribution roots if the call is successful,
/// otherwise an error.
///
/// # Errors
///
/// * `ElContractsError` - if the call to the contract fails.
pub async fn get_distribution_roots_length(&self) -> Result<U256, ElContractsError> {
let provider = get_provider(&self.provider);

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

let distribution_roots_lenght_call = contract_rewards_coordinator
.getDistributionRootsLength()
.call()
.await
.map_err(ElContractsError::AlloyContractError)?;

let IRewardsCoordinator::getDistributionRootsLengthReturn {
_0: distribution_roots_length,
} = distribution_roots_lenght_call;

Ok(distribution_roots_length)
}

/// Get the operator's shares in a strategy
///
/// # Arguments
Expand Down Expand Up @@ -1122,14 +1152,11 @@ pub struct AllocationInfo {
#[cfg(test)]
mod tests {
use super::*;
use crate::test_utils::{build_el_chain_reader, OPERATOR_ADDRESS};
use alloy::providers::Provider;
use alloy::{eips::eip1898::BlockNumberOrTag::Number, rpc::types::BlockTransactionsKind};
use alloy_primitives::{address, keccak256, Address, FixedBytes, U256};
use eigen_logging::get_test_logger;
use eigen_testing_utils::anvil_constants::{
get_allocation_manager_address, get_avs_directory_address, get_erc20_mock_strategy,
get_rewards_coordinator_address,
};
use eigen_testing_utils::anvil_constants::get_erc20_mock_strategy;
use eigen_testing_utils::{
anvil::start_anvil_container, anvil_constants::get_delegation_manager_address,
};
Expand All @@ -1140,37 +1167,6 @@ mod tests {
delegationmanager::DelegationManager::calculateDelegationApprovalDigestHashReturn,
};

const OPERATOR_ADDRESS: Address = address!("70997970C51812dc3A010C7d01b50e0d17dc79C8");

async fn build_el_chain_reader(http_endpoint: String) -> ELChainReader {
let delegation_manager_address =
get_delegation_manager_address(http_endpoint.clone()).await;
let allocation_manager_address =
get_allocation_manager_address(http_endpoint.clone()).await;
let avs_directory_address = get_avs_directory_address(http_endpoint.clone()).await;
let rewards_coordinator_address =
get_rewards_coordinator_address(http_endpoint.clone()).await;

let delegation_manager_contract =
DelegationManager::new(delegation_manager_address, get_provider(&http_endpoint));
let permission_controller_address = delegation_manager_contract
.permissionController()
.call()
.await
.unwrap()
._0;

ELChainReader::new(
get_test_logger(),
allocation_manager_address,
delegation_manager_address,
rewards_coordinator_address,
avs_directory_address,
permission_controller_address,
http_endpoint,
)
}

#[tokio::test]
async fn test_calculate_delegation_approval_digest_hash() {
let (_container, http_endpoint, _ws_endpoint) = start_anvil_container().await;
Expand Down Expand Up @@ -1262,6 +1258,19 @@ mod tests {
assert_eq!(hash, operator_hash);
}

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

let distribution_roots_length_ret = el_chain_reader
.get_distribution_roots_length()
.await
.unwrap();

assert_eq!(distribution_roots_length_ret, U256::from(1));
}

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

0 comments on commit 4618bce

Please sign in to comment.