Skip to content

Commit

Permalink
Add docs and error handling improved (#125)
Browse files Browse the repository at this point in the history
Co-authored-by: tomasarrachea <[email protected]>
  • Loading branch information
pablodeymo and TomasArrachea authored Sep 24, 2024
1 parent aeb6541 commit b394482
Show file tree
Hide file tree
Showing 33 changed files with 424 additions and 181 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ List of crates in the repository :-
- [eigen-contracts-bindings](https://github.com/Layr-Labs/eigensdk-rs/tree/main/crates/contracts/bindings) - Generate ethers bindings for Eigen Layer.
- [eigen-crypto-bls](https://github.com/Layr-Labs/eigensdk-rs/tree/main/crates/crypto/bls) - New bls key pair, sign message, conversion utilites between alloy and arkworks bn254.
- [eigen-crypto-bn254](https://github.com/Layr-Labs/eigensdk-rs/tree/main/crates/crypto/bn254) - verify message on G2, map to curve.
- [eigen-metrics](https://github.com/Layr-Labs/eigensdk-rs/tree/main/crates/metrics) - performance , rpc and economic metrics
- [eigen-services](https://github.com/Layr-Labs/eigensdk-rs/tree/main/crates/services) - Spawn tokio services for operators info , bls aggregation
- [eigen-metrics](https://github.com/Layr-Labs/eigensdk-rs/tree/main/crates/metrics) - performance, rpc and economic metrics
- [eigen-services](https://github.com/Layr-Labs/eigensdk-rs/tree/main/crates/services) - Spawn tokio services for operators info, bls aggregation
- [eigen-types](https://github.com/Layr-Labs/eigensdk-rs/tree/main/crates/types) - Common types
- [eigen-utils](https://github.com/Layr-Labs/eigensdk-rs/tree/main/crates/utils) - Publicly exportable `m2-mainnet` compatible alloy bindings.
- [eigen-testing-utils](https://github.com/Layr-Labs/eigensdk-rs/tree/main/testing/testing-utils) - Contains publicly exportable anvil , holesky , mainnet addresses for eigen contracts .
- [eigen-testing-utils](https://github.com/Layr-Labs/eigensdk-rs/tree/main/testing/testing-utils) - Contains publicly exportable anvil, holesky, mainnet addresses for eigen contracts.

## Examples

You can run any [example](https://github.com/Layr-Labs/eigensdk-rs/tree/main/examples) using the command cargo run --example <example-name>
You can run any [example](https://github.com/Layr-Labs/eigensdk-rs/tree/main/examples) using the command `cargo run --example <example-name>`

Example :

Expand All @@ -35,7 +35,7 @@ cargo run --example get_quorum_count

## Contributor Guidelines

We are actively looking for contributors. Thank you for your interest. We have strict ci checks in place. In case of any questions and support , feel free to raise an issue.
We are actively looking for contributors. Thank you for your interest. We have strict ci checks in place. In case of any questions and support, feel free to raise an issue.

### PR
To test locally :-
Expand Down Expand Up @@ -100,11 +100,11 @@ Rolling `MSRV` policy of 6 months. The current `MSRV` is 1.79

This software is `unaudited`.This is experimental software and is provided on an "as is" and "as available" basis and may not work at all. It should not be used in production.

# Credits
## Credits
- [eigensdk-go](https://github.com/Layr-Labs/eigensdk-go/tree/master)


# Security Bugs
## Security Bugs

Please report security vulnerabilities to <[email protected]>. Do NOT report security bugs via Github Issues.

Expand Down
4 changes: 1 addition & 3 deletions crates/chainio/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# ChainIo

Read / write / subscribe to eigen core contract and avs contract.


Read / write / subscribe to eigen core contract and avs contract.
15 changes: 5 additions & 10 deletions crates/chainio/clients/avsregistry/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,14 @@ impl AvsRegistryReader for AvsRegistryChainReader {

let contract_operator_state_retriever =
OperatorStateRetriever::new(self.operator_state_retriever, provider);
let operator_state_result = contract_operator_state_retriever
let operator_state = contract_operator_state_retriever
.getOperatorState_0(self.registry_coordinator_addr, quorum_numbers, block_number)
.call()
.await;
.await
.map_err(|_| AvsRegistryError::GetOperatorState)?;

match operator_state_result {
Ok(operator_state) => {
let OperatorStateRetriever::getOperatorState_0Return { _0: quorum } =
operator_state;
Ok(quorum)
}
Err(_) => Err(AvsRegistryError::GetOperatorState),
}
let OperatorStateRetriever::getOperatorState_0Return { _0: quorum } = operator_state;
Ok(quorum)
}

async fn get_check_signatures_indices(
Expand Down
117 changes: 116 additions & 1 deletion crates/chainio/clients/elcontracts/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ pub struct ELChainReader {
}

impl ELChainReader {
/// Creates a new `ELChainReader` instance with the given parameters.
///
/// # Arguments
///
/// * `_logger` - The logger to use for logging.
/// * `slasher` - The address of the slasher contract.
/// * `delegation_manager` - The address of the delegation manager contract.
/// * `avs_directory` - The address of the avs directory contract.
/// * `provider` - The provider to use for the RPC client.
///
/// # Returns
///
/// A new `ELChainReader` instance.
pub fn new(
_logger: SharedLogger,
slasher: Address,
Expand All @@ -33,7 +46,21 @@ impl ELChainReader {
}
}

/// Builds a new [`ELChainReader`] instance .
/// Builds a new `ELChainReader` instance, getting the slasher address from the delegation manager
/// by calling the `slasher()` function and the corresponding Contract function.
///
/// # Arguments
///
/// * `_logger` - The logger to use for logging.
/// * `delegation_manager` - The address of the delegation manager contract.
/// * `avs_directory` - The address of the avs directory contract.
/// * `client` - The provider to use for the RPC client to call the contracts.
///
/// # Returns
///
/// A new `ELChainReader` instance.
///
/// # Errors
pub async fn build(
_logger: SharedLogger,
delegation_manager: Address,
Expand Down Expand Up @@ -61,6 +88,23 @@ impl ELChainReader {
})
}

/// Calculate the delegation approval digest hash
///
/// # Arguments
///
/// * `staker` - The staker's address
/// * `operator` - The operator's address
/// * `delegation_approver` - The delegation approver's address
/// * `approve_salt` - The approve salt
/// * `expiry` - The expiry
///
/// # Returns
///
/// * `FixedBytes<32>` - The delegation approval digest hash
///
/// # Errors
///
/// * `ElContractsError` - if the call to the contract fails
pub async fn calculate_delegation_approval_digest_hash(
&self,
staker: Address,
Expand Down Expand Up @@ -89,6 +133,22 @@ impl ELChainReader {
Ok(digest_hash)
}

/// Calculate the operator avs registration digest hash
///
/// # Arguments
///
/// * `operator` - The operator's address
/// * `avs` - The avs's address
/// * `salt` - The salt
/// * `expiry` - The expiry
///
/// # Returns
///
/// * `FixedBytes<32>` - The operator avs registration digest hash
///
/// # Errors
///
/// * `ElContractsError` - if the call to the contract fails
pub async fn calculate_operator_avs_registration_digest_hash(
&self,
operator: Address,
Expand Down Expand Up @@ -122,6 +182,10 @@ impl ELChainReader {
/// # Returns
///
/// * `U256` - The operator's shares in the strategy
///
/// # Errors
///
/// * `ElContractsError` - if the call to the contract fails
pub async fn get_operator_shares_in_strategy(
&self,
operator_addr: Address,
Expand All @@ -141,6 +205,17 @@ impl ELChainReader {
Ok(shares)
}

/// Check if the operator is frozen
///
/// # Arguments
///
/// * `operator_addr` - The operator's address
///
/// # Returns
///
/// * `bool` - True if the operator is frozen, false otherwise
///
/// # Errors
pub async fn operator_is_frozen(
&self,
operator_addr: Address,
Expand All @@ -159,6 +234,20 @@ impl ELChainReader {
Ok(is_froze)
}

/// Check if the service manager can slash the operator
///
/// # Arguments
///
/// * `operator_addr` - The operator's address
/// * `service_manager_addr` - The service manager's address
///
/// # Returns
///
/// * `u32` - The block number until the operator can be slashed
///
/// # Errors
///
/// * `ElContractsError` - if the call to the contract fails
pub async fn service_manager_can_slash_operator_until_block(
&self,
operator_addr: Address,
Expand Down Expand Up @@ -222,6 +311,19 @@ impl ELChainReader {
))
}

/// Get the operator's details
///
/// # Arguments
///
/// * `operator` - The operator's address
///
/// # Returns
///
/// * `Operator` - The operator's details
///
/// # Errors
///
/// * `ElContractsError` - if the call to the contract fails
pub async fn get_operator_details(
&self,
operator: Address,
Expand Down Expand Up @@ -250,6 +352,19 @@ impl ELChainReader {
})
}

/// Check if the operator is registered
///
/// # Arguments
///
/// * `operator` - The operator's address
///
/// # Returns
///
/// * `bool` - true if the operator is registered, false otherwise
///
/// # Errors
///
/// * `ElContractsError` - if the call to the contract fails
pub async fn is_operator_registered(
&self,
operator: Address,
Expand Down
85 changes: 55 additions & 30 deletions crates/chainio/clients/elcontracts/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use DelegationManager::OperatorDetails;
/// Gas limit for registerAsOperator in [`DelegationManager`]
pub const GAS_LIMIT_REGISTER_AS_OPERATOR_DELEGATION_MANAGER: u128 = 300000;

/// Chain Writer to interact with EigenLayer contracts onchain
#[derive(Debug, Clone)]
pub struct ELChainWriter {
delegation_manager: Address,
Expand All @@ -39,6 +40,19 @@ impl ELChainWriter {
}
}

/// Register an operator to EigenLayer
///
/// # Arguments
///
/// * `operator` - The operator to register
///
/// # Returns
///
/// * `FixedBytes<32>` - The transaction hash if successful, otherwise an error
///
/// # Errors
///
/// * `ElContractsError` - if the call to the contract fails
pub async fn register_as_operator(
&self,
operator: Operator,
Expand Down Expand Up @@ -78,6 +92,19 @@ impl ELChainWriter {
Ok(hash)
}

/// Update operator details on EigenLayer
///
/// # Arguments
///
/// * `operator` - The operator to update
///
/// # Returns
///
/// * `TxHash` - The transaction hash if successful, otherwise an error
///
/// # Errors
///
/// * `ElContractsError` - if the call to the contract fails
pub async fn update_operator_details(
&self,
operator: Operator,
Expand Down Expand Up @@ -113,50 +140,48 @@ impl ELChainWriter {
Ok(*metadata_tx.tx_hash())
}

/// Deposit ERC20 tokens into a strategy on EigenLayer
///
/// # Arguments
///
/// * `strategy_addr` - The address of the strategy to deposit into
/// * `amount` - The amount of tokens to deposit
///
/// # Returns
///
/// * `TxHash` - The transaction hash if successful, otherwise an error
///
/// # Errors
///
/// * `ElContractsError` - if the call to the contract fails
pub async fn deposit_erc20_into_strategy(
&self,
strategy_addr: Address,
amount: U256,
) -> Result<TxHash, ElContractsError> {
info!(
"depositing {:?} tokens into strategy {:?}",
amount, strategy_addr
);
let tokens_result = self
info!("depositing {amount:?} tokens into strategy {strategy_addr:?}");
let tokens = self
.el_chain_reader
.get_strategy_and_underlying_erc20_token(strategy_addr)
.await;
match tokens_result {
Ok(tokens) => {
let (_, underlying_token_contract, underlying_token) = tokens;
let provider = get_signer(self.signer.clone(), &self.provider);
.await?;
let (_, underlying_token_contract, underlying_token) = tokens;
let provider = get_signer(self.signer.clone(), &self.provider);

let contract_underlying_token = IERC20::new(underlying_token_contract, &provider);
let contract_underlying_token = IERC20::new(underlying_token_contract, &provider);

let contract_call =
contract_underlying_token.approve(self.strategy_manager, amount);
let contract_call = contract_underlying_token.approve(self.strategy_manager, amount);

let _approve = contract_call.send().await?;
let _approve = contract_call.send().await?;

let contract_strategy_manager =
StrategyManager::new(self.strategy_manager, &provider);
let contract_strategy_manager = StrategyManager::new(self.strategy_manager, &provider);

let deposit_contract_call = contract_strategy_manager.depositIntoStrategy(
strategy_addr,
underlying_token,
amount,
);
let deposit_contract_call =
contract_strategy_manager.depositIntoStrategy(strategy_addr, underlying_token, amount);

let tx = deposit_contract_call.send().await?;
let tx = deposit_contract_call.send().await?;

info!(
"deposited {:?} tokens into strategy {:?}",
amount, strategy_addr
);
Ok(*tx.tx_hash())
}
Err(e) => Err(e),
}
info!("deposited {amount:?} tokens into strategy {strategy_addr:?}");
Ok(*tx.tx_hash())
}
}

Expand Down
Loading

0 comments on commit b394482

Please sign in to comment.