use thiserror::Error;
#[derive(Error, Debug)]
pub enum EthereumError {
#[error("RPC error: {0}")]
RpcError(String),
#[error("Storage slot already used: {0}")]
SlotUsed(String),
#[error("Invalid receipt proof: {0}")]
InvalidReceiptProof(String),
#[error("Reorg detected at block {block}, depth {depth}")]
ReorgDetected { block: u64, depth: u64 },
#[error("Insufficient confirmations: got {got}, need {need}")]
InsufficientConfirmations { got: u64, need: u64 },
#[error(transparent)]
CoreError(#[from] csv_adapter_core::AdapterError),
}
impl EthereumError {
pub fn is_transient(&self) -> bool {
match self {
EthereumError::RpcError(_) => true,
EthereumError::InsufficientConfirmations { .. } => true,
EthereumError::ReorgDetected { .. } => true,
EthereumError::SlotUsed(_) => false,
EthereumError::InvalidReceiptProof(_) => false,
EthereumError::CoreError(_) => false,
}
}
}
impl From<EthereumError> for csv_adapter_core::AdapterError {
fn from(err: EthereumError) -> Self {
match err {
EthereumError::CoreError(e) => e,
EthereumError::RpcError(msg) => csv_adapter_core::AdapterError::NetworkError(msg),
EthereumError::SlotUsed(msg) => csv_adapter_core::AdapterError::InvalidSeal(msg),
EthereumError::InvalidReceiptProof(msg) => {
csv_adapter_core::AdapterError::InclusionProofFailed(msg)
}
EthereumError::ReorgDetected { block, depth } => {
csv_adapter_core::AdapterError::ReorgInvalid(format!(
"Reorg at block {}, depth {}",
block, depth
))
}
EthereumError::InsufficientConfirmations { got, need } => {
csv_adapter_core::AdapterError::FinalityNotReached(format!(
"Got {} confirmations, need {}",
got, need
))
}
}
}
}
pub type EthereumResult<T> = Result<T, EthereumError>;