Skip to content

Commit

Permalink
fix(cli): return non-ABI error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
merklefruit committed Dec 16, 2024
1 parent 3e9cfa7 commit f2898cb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 46 deletions.
22 changes: 21 additions & 1 deletion bolt-cli/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::path::PathBuf;

use alloy::primitives::{Address, FixedBytes, B256, U256};
use alloy::{
primitives::{Address, FixedBytes, B256, U256},
providers::Provider,
transports::Transport,
};
use clap::{
builder::styling::{AnsiColor, Color, Style},
Parser, Subcommand, ValueEnum,
Expand Down Expand Up @@ -466,6 +470,7 @@ impl Chain {
}
}

/// Get the chain ID for the given chain. Returns `None` if the chain ID is not supported.
pub fn from_id(id: u64) -> Option<Self> {
match id {
1 => Some(Self::Mainnet),
Expand All @@ -475,6 +480,21 @@ impl Chain {
_ => None,
}
}

/// Get the chain ID for the given chain. Returns an error if the chain ID is not supported.
pub fn try_from_id(id: u64) -> eyre::Result<Self> {
Self::from_id(id).ok_or_else(|| eyre::eyre!("chain id {} not supported", id))
}

/// Tries to get the chain ID from an online provider.
pub async fn try_from_provider<T, P>(provider: &P) -> eyre::Result<Self>
where
T: Transport + Clone,
P: Provider<T>,
{
let chain_id = provider.get_chain_id().await?;
Self::try_from_id(chain_id)
}
}

/// Styles for the CLI application.
Expand Down
35 changes: 12 additions & 23 deletions bolt-cli/src/commands/operators/eigenlayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloy::{
utils::{format_ether, Unit},
Bytes, Uint,
},
providers::{Provider, ProviderBuilder, WalletProvider},
providers::ProviderBuilder,
signers::{local::PrivateKeySigner, SignerSync},
sol_types::SolInterface,
};
Expand Down Expand Up @@ -40,11 +40,9 @@ impl EigenLayerSubcommand {
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(EthereumWallet::from(signer))
.on_http(rpc_url.clone());
.on_http(rpc_url);

let chain_id = provider.get_chain_id().await?;
let chain = Chain::from_id(chain_id)
.unwrap_or_else(|| panic!("chain id {} not supported", chain_id));
let chain = Chain::try_from_provider(&provider).await?;

let deployments = deployments_for_chain(chain);

Expand All @@ -63,13 +61,9 @@ impl EigenLayerSubcommand {

request_confirmation();

let token_erc20 = IERC20Instance::new(token, provider.clone());
let token_erc20 = IERC20Instance::new(token, provider);

let balance = token_erc20
.balanceOf(provider.clone().default_signer_address())
.call()
.await?
._0;
let balance = token_erc20.balanceOf(operator).call().await?._0;

info!("Operator token balance: {}", format_ether(balance));

Expand Down Expand Up @@ -103,11 +97,9 @@ impl EigenLayerSubcommand {
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(EthereumWallet::from(signer.clone()))
.on_http(rpc_url.clone());
.on_http(rpc_url);

let chain_id = provider.get_chain_id().await?;
let chain = Chain::from_id(chain_id)
.unwrap_or_else(|| panic!("chain id {} not supported", chain_id));
let chain = Chain::try_from_provider(&provider).await?;

info!(operator = %signer.address(), rpc = %operator_rpc, ?chain, "Registering EigenLayer operator");

Expand All @@ -120,10 +112,10 @@ impl EigenLayerSubcommand {
BoltEigenLayerMiddleware::new(bolt_avs_address, provider.clone());

let avs_directory =
AVSDirectory::new(deployments.eigen_layer.avs_directory, provider.clone());
AVSDirectory::new(deployments.eigen_layer.avs_directory, provider);
let signature_digest_hash = avs_directory
.calculateOperatorAVSRegistrationDigestHash(
provider.clone().default_signer_address(),
signer.address(),
bolt_avs_address,
salt,
expiry,
Expand Down Expand Up @@ -183,9 +175,7 @@ impl EigenLayerSubcommand {
.wallet(EthereumWallet::from(signer))
.on_http(rpc_url);

let chain_id = provider.get_chain_id().await?;
let chain = Chain::from_id(chain_id)
.unwrap_or_else(|| panic!("chain id {} not supported", chain_id));
let chain = Chain::try_from_provider(&provider).await?;

info!(operator = %address, ?chain, "Deregistering EigenLayer operator");

Expand Down Expand Up @@ -229,9 +219,8 @@ impl EigenLayerSubcommand {

Self::Status { rpc_url: rpc, address } => {
let provider = ProviderBuilder::new().on_http(rpc.clone());
let chain_id = provider.get_chain_id().await?;
let chain = Chain::from_id(chain_id)
.unwrap_or_else(|| panic!("chain id {} not supported", chain_id));

let chain = Chain::try_from_provider(&provider).await?;

let deployments = deployments_for_chain(chain);
let bolt_manager =
Expand Down
16 changes: 6 additions & 10 deletions bolt-cli/src/commands/operators/symbiotic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloy::{
network::EthereumWallet,
primitives::{utils::Unit, Uint},
providers::{Provider, ProviderBuilder},
providers::ProviderBuilder,
signers::local::PrivateKeySigner,
sol_types::SolInterface,
};
Expand Down Expand Up @@ -31,9 +31,7 @@ impl SymbioticSubcommand {
.wallet(EthereumWallet::from(signer.clone()))
.on_http(rpc_url);

let chain_id = provider.get_chain_id().await?;
let chain = Chain::from_id(chain_id)
.unwrap_or_else(|| panic!("chain id {} not supported", chain_id));
let chain = Chain::try_from_provider(&provider).await?;

let deployments = deployments_for_chain(chain);

Expand Down Expand Up @@ -96,16 +94,15 @@ impl SymbioticSubcommand {
Self::Deregister { rpc_url, operator_private_key } => {
let signer = PrivateKeySigner::from_bytes(&operator_private_key)
.wrap_err("valid private key")?;

let address = signer.address();

let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(EthereumWallet::from(signer))
.on_http(rpc_url);

let chain_id = provider.get_chain_id().await?;
let chain = Chain::from_id(chain_id)
.unwrap_or_else(|| panic!("chain id {} not supported", chain_id));
let chain = Chain::try_from_provider(&provider).await?;

let deployments = deployments_for_chain(chain);

Expand Down Expand Up @@ -145,9 +142,8 @@ impl SymbioticSubcommand {

Self::Status { rpc_url, address } => {
let provider = ProviderBuilder::new().on_http(rpc_url.clone());
let chain_id = provider.get_chain_id().await?;
let chain = Chain::from_id(chain_id)
.unwrap_or_else(|| panic!("chain id {} not supported", chain_id));

let chain = Chain::try_from_provider(&provider).await?;

let deployments = deployments_for_chain(chain);
let bolt_manager =
Expand Down
16 changes: 6 additions & 10 deletions bolt-cli/src/commands/validators.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use alloy::{
network::EthereumWallet,
providers::{Provider, ProviderBuilder},
signers::local::PrivateKeySigner,
network::EthereumWallet, providers::ProviderBuilder, signers::local::PrivateKeySigner,
sol_types::SolInterface,
};
use ethereum_consensus::crypto::PublicKey as BlsPublicKey;
Expand Down Expand Up @@ -33,11 +31,9 @@ impl ValidatorsCommand {
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(EthereumWallet::from(signer))
.on_http(rpc_url.clone());
.on_http(rpc_url);

let chain_id = provider.get_chain_id().await?;
let chain = Chain::from_id(chain_id)
.unwrap_or_else(|| panic!("chain id {} not supported", chain_id));
let chain = Chain::try_from_provider(&provider).await?;

let bolt_validators_address = deployments_for_chain(chain).bolt.validators;

Expand Down Expand Up @@ -105,9 +101,9 @@ impl ValidatorsCommand {

ValidatorsSubcommand::Status { rpc_url, pubkeys_path, pubkeys } => {
let provider = ProviderBuilder::new().on_http(rpc_url);
let chain_id = provider.get_chain_id().await?;
let chain = Chain::from_id(chain_id)
.unwrap_or_else(|| panic!("chain id {} not supported", chain_id));

let chain = Chain::try_from_provider(&provider).await?;

let registry = deployments_for_chain(chain).bolt.validators;

let mut bls_pubkeys = Vec::new();
Expand Down
11 changes: 9 additions & 2 deletions bolt-cli/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloy::{
use ethereum_consensus::crypto::PublicKey as BlsPublicKey;
use eyre::{Context, Result};
use serde::Serialize;
use tracing::info;
use tracing::{error, info};

/// BoltManager contract bindings.
pub mod bolt_manager;
Expand Down Expand Up @@ -69,12 +69,19 @@ pub fn write_to_file<T: Serialize>(out: &str, data: &T) -> Result<()> {
pub fn try_parse_contract_error<T: SolInterface>(error: ContractError) -> Result<T, ContractError> {
match error {
ContractError::TransportError(TransportError::ErrorResp(resp)) => {
let data = resp.data.unwrap_or_default();
let data = resp.data.clone().unwrap_or_default();
let data = data.get().trim_matches('"');
let data = Bytes::from_str(data).unwrap_or_default();

if data.is_empty() {
error!("Could not decode error data from transport response.");
return Err(ContractError::TransportError(TransportError::ErrorResp(resp)));
}

T::abi_decode(&data, true).map_err(Into::into)
}

// For any other error, return the original error
_ => Err(error),
}
}
Expand Down

0 comments on commit f2898cb

Please sign in to comment.