Skip to content

Commit

Permalink
Refactor and clarify errors in ethereum client
Browse files Browse the repository at this point in the history
  • Loading branch information
mertwole committed Aug 20, 2024
1 parent 1327fba commit d681cd8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 48 deletions.
21 changes: 11 additions & 10 deletions ethereum/client/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use alloy::transports::{RpcError, TransportErrorKind};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
#[error("Not implemented")]
NotImplemented,
#[error("Error in HTTP transport")]
ErrorInHTTPTransport,
#[error("Error in HTTP transport: {0}")]
ErrorInHTTPTransport(RpcError<TransportErrorKind>),
#[error("Wrong address")]
WrongAddress,
#[error("Wrong node URL")]
Expand All @@ -16,6 +15,8 @@ pub enum Error {
ErrorDuringContractExecution(alloy::contract::Error),
#[error("Error sending transaction: {0}")]
ErrorSendingTransaction(alloy::contract::Error),
#[error("Error querying event: {0}")]
ErrorQueryingEvent(alloy::contract::Error),
#[error("Error waiting transaction receipt")]
ErrorWaitingTransactionReceipt,
#[error("Error fetching transaction")]
Expand All @@ -24,10 +25,10 @@ pub enum Error {
ErrorFetchingTransactionReceipt,
#[error("Error fetching block")]
ErrorFetchingBlock,
#[error("Wrong path to file")]
WrongPathToFile,
#[error("Wrong JSON format")]
WrongJsonFormation,
#[error("Cannot convert to U256")]
UnableToConvertToU256,
}

impl From<RpcError<TransportErrorKind>> for Error {
fn from(value: RpcError<TransportErrorKind>) -> Self {
Self::ErrorInHTTPTransport(value)
}
}
42 changes: 16 additions & 26 deletions ethereum/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,9 @@ where
}

pub async fn get_approx_balance(&self, address: Address) -> Result<f64, Error> {
let balance = self.provider.get_balance(address).latest().await;

if let Ok(balance) = balance {
let balance: f64 = balance.into();
Ok(balance / 1_000_000_000_000_000_000.0)
} else {
Err(Error::ErrorInHTTPTransport)
}
let balance = self.provider.get_balance(address).latest().await?;
let balance: f64 = balance.into();
Ok(balance / 1_000_000_000_000_000_000.0)
}

pub async fn provide_merkle_root(
Expand Down Expand Up @@ -254,18 +249,11 @@ where
}

pub async fn block_number(&self) -> Result<u64, Error> {
self.provider
.get_block_number()
.await
.map_err(|_| Error::ErrorInHTTPTransport)
self.provider.get_block_number().await.map_err(|e| e.into())
}

pub async fn fetch_merkle_roots(&self, depth: u64) -> Result<Vec<MerkleRootEntry>, Error> {
let current_block: u64 = self
.provider
.get_block_number()
.await
.map_err(|_| Error::ErrorInHTTPTransport)?;
let current_block: u64 = self.provider.get_block_number().await?;

self.fetch_merkle_roots_in_range(
current_block.checked_sub(depth).unwrap_or_default(),
Expand All @@ -287,15 +275,17 @@ where

let event: Event<T, P, MerkleRoot, Ethereum> = Event::new(self.provider.clone(), filter);

match event.query().await {
Ok(logs) => Ok(logs
.iter()
.map(|(event, _)| MerkleRootEntry {
block_number: event.blockNumber.to(),
})
.collect()),
Err(_) => Err(Error::ErrorInHTTPTransport),
}
let logs = event
.query()
.await
.map_err(|e| Error::ErrorQueryingEvent(e))?;

Ok(logs
.iter()
.map(|(event, _)| MerkleRootEntry {
block_number: event.blockNumber.to(),
})
.collect())
}

#[allow(clippy::too_many_arguments)]
Expand Down
15 changes: 3 additions & 12 deletions ethereum/client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ where
erc20_treasury_proxy: *erc20_treasury_proxy.address(),
};

let nonce = provider
.get_transaction_count(signer_address)
.await
.map_err(|_| Error::ErrorInHTTPTransport)?;
let nonce = provider.get_transaction_count(signer_address).await?;

let relayer_proxy = ProxyContract::new(deployment.relayer_proxy, provider.clone());

Expand All @@ -158,10 +155,7 @@ where
.await
.map_err(|_| Error::ErrorWaitingTransactionReceipt)?;

let nonce = provider
.get_transaction_count(signer_address)
.await
.map_err(|_| Error::ErrorInHTTPTransport)?;
let nonce = provider.get_transaction_count(signer_address).await?;

let pending_tx = message_queue_proxy
.upgradeToAndCall(deployment.message_queue, Bytes::new())
Expand All @@ -177,10 +171,7 @@ where
.await
.map_err(|_| Error::ErrorWaitingTransactionReceipt)?;

let nonce = provider
.get_transaction_count(signer_address)
.await
.map_err(|_| Error::ErrorInHTTPTransport)?;
let nonce = provider.get_transaction_count(signer_address).await?;

let pending_tx = erc20_treasury_proxy
.upgradeToAndCall(deployment.erc20_treasury, Bytes::new())
Expand Down

0 comments on commit d681cd8

Please sign in to comment.