Skip to content

Commit

Permalink
fix: Migrate to thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
ankurdubey521 committed Jun 19, 2024
1 parent 56b96c8 commit d9c6d0d
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 95 deletions.
1 change: 1 addition & 0 deletions crates/routing-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ derive_more = { version = "1.0.0-beta.6", features = ["from", "into", "display"]
reqwest = "0.12.4"
ruint = "1.12.3"
linreg = "0.2.0"
thiserror = "1.0.61"

# workspace dependencies
account-aggregation = { workspace = true }
Expand Down
7 changes: 4 additions & 3 deletions crates/routing-engine/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use futures::stream::StreamExt;

use config::config::BucketConfig;

use crate::{estimator, source, token_price, CostType, Route, RouteError};
use crate::{CostType, estimator, Route, RouteError, source, token_price};

const SOURCE_FETCH_PER_BUCKET_RATE_LIMIT: usize = 10;
const BUCKET_PROCESSING_RATE_LIMIT: usize = 5;
Expand Down Expand Up @@ -212,15 +212,16 @@ enum IndexerErrors<
#[cfg(test)]
mod tests {
use std::env;
use std::fmt::Error;

use config::Config;
use storage::{ControlFlow, MessageQueue, Msg, RoutingModelStore};

use crate::CostType;
use crate::estimator::{Estimator, LinearRegressionEstimator};
use crate::indexer::Indexer;
use crate::source::BungeeClient;
use crate::token_price::TokenPriceProvider;
use crate::CostType;

#[derive(Debug)]
struct ModelStoreStub;
Expand Down Expand Up @@ -264,7 +265,7 @@ mod tests {
#[derive(Debug)]
struct TokenPriceProviderStub;
impl TokenPriceProvider for TokenPriceProviderStub {
type Error = String;
type Error = Error;

async fn get_token_price(&self, token_symbol: &String) -> Result<f64, Self::Error> {
Ok(1.0) // USDC
Expand Down
13 changes: 7 additions & 6 deletions crates/routing-engine/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
use derive_more::{Display, From};
use thiserror::Error;

use config::config::{BucketConfig, ChainConfig, Config, TokenConfig};
pub use indexer::Indexer;

// use route_fee_bucket::RouteFeeBucket;
pub mod engine;
pub mod route_fee_bucket;
Expand All @@ -7,16 +13,11 @@ mod traits;
mod tests;
pub mod token_price;

use derive_more::{Display, From};

use config::config::{BucketConfig, ChainConfig, Config, TokenConfig};
pub use indexer::Indexer;

pub mod estimator;
pub mod indexer;
mod source;

#[derive(Debug, Display)]
#[derive(Debug, Error, Display)]
enum CostType {
Fee,
BridgingTime,
Expand Down
28 changes: 14 additions & 14 deletions crates/routing-engine/src/source/bungee/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use config::config::BungeeConfig;
use derive_more::{Display, From};
use reqwest;
use reqwest::header;
use ruint::aliases::U256;
use thiserror::Error;

use config::config::BungeeConfig;
use types::*;

use crate::source::{Calldata, RouteSource};
use crate::{CostType, Route};
use crate::source::{Calldata, RouteSource};

mod types;

Expand Down Expand Up @@ -45,22 +45,22 @@ impl BungeeClient {

const ADDRESS_ZERO: &'static str = "0x0000000000000000000000000000000000000000";

#[derive(Debug, Display, From)]
#[derive(Debug, Error)]
pub enum BungeeFetchRouteCostError {
#[display("Configuration Missing for token {} on chain {}", _1, _0)]
#[error("Configuration Missing for token {} on chain {}", _1, _0)]
MissingChainForTokenInConfigError(u32, String),

#[display("Error while making request: Request error: {}", _0)]
BungeeClientError(BungeeClientError),
#[error("Error while making request: Request error: {}", _0)]
BungeeClientError(#[from] BungeeClientError),

#[display("Failure indicated in bungee response")]
#[error("Failure indicated in bungee response")]
FailureIndicatedInResponseError(),

#[display("No valid routes returned by Bungee API")]
#[error("No valid routes returned by Bungee API")]
NoValidRouteError(),

#[display("The estimation type {} is not implemented", _0)]
EstimationTypeNotImplementedError(CostType),
#[error("The estimation type {} is not implemented", _0)]
EstimationTypeNotImplementedError(#[from] CostType),
}

impl RouteSource for BungeeClient {
Expand Down Expand Up @@ -138,7 +138,7 @@ impl RouteSource for BungeeClient {
&self,
route: &Route<'_>,
) -> Result<Calldata, Self::GenerateRouteCalldataError> {
unimplemented!()
todo!()
}
}

Expand All @@ -150,9 +150,9 @@ mod tests {

use config::Config;

use crate::source::bungee::types::GetQuoteRequest;
use crate::source::{BungeeClient, RouteSource};
use crate::{CostType, Route};
use crate::source::{BungeeClient, RouteSource};
use crate::source::bungee::types::GetQuoteRequest;

fn setup() -> (Config, BungeeClient) {
// let config = config::Config::from_file("../../config.yaml").unwrap();
Expand Down
16 changes: 6 additions & 10 deletions crates/routing-engine/src/source/bungee/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use derive_more::{Display, From};
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Deserialize, Debug)]
pub struct BungeeResponse<T> {
Expand Down Expand Up @@ -101,18 +101,14 @@ pub struct GetQuoteResponseRefuelGasFee {
}

// Errors
#[derive(Debug, Display, From)]
#[derive(Debug, Error)]
pub enum BungeeClientError {
#[display(
"Error while deserializing response: Deserialization error: {}. Response: {}",
_1,
_0
)]
#[error("Error while deserializing response: {0}")]
DeserializationError(String, serde_json::Error),

#[display("Error while making request: Request error: {}", _0)]
RequestError(reqwest::Error),
#[error("Error while making request: Request error: {}", _0)]
RequestError(#[from] reqwest::Error),

#[display("No route returned by Bungee API")]
#[error("No route returned by Bungee API")]
NoRouteError,
}
3 changes: 2 additions & 1 deletion crates/routing-engine/src/token_price/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::error::Error;
use std::fmt::{Debug, Display};

pub mod utils;

pub trait TokenPriceProvider: Debug {
type Error: Display + Debug;
type Error: Error + Display + Debug;

async fn get_token_price(&self, token_symbol: &String) -> Result<f64, Self::Error>;
}
22 changes: 13 additions & 9 deletions crates/routing-engine/src/token_price/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use derive_more::{Display, From};
use ruint;
use ruint::aliases::U256;
use ruint::Uint;
use thiserror::Error;

use crate::token_price::TokenPriceProvider;

Expand All @@ -12,7 +13,10 @@ pub async fn get_token_amount_from_value_in_usd<'config, T: TokenPriceProvider>(
chain_id: u32,
value_in_usd: f64,
) -> Result<U256, Errors<T::Error>> {
let token_price = token_price_provider.get_token_price(token_symbol).await?;
let token_price = token_price_provider
.get_token_price(token_symbol)
.await
.map_err(Errors::<T::Error>::TokenPriceProviderError)?;

let token_config = config.tokens.get(token_symbol);
if token_config.is_none() {
Expand All @@ -34,22 +38,22 @@ pub async fn get_token_amount_from_value_in_usd<'config, T: TokenPriceProvider>(
Ok(token_amount_in_wei)
}

#[derive(Debug, Display, From)]
#[derive(Debug, Error)]
pub(crate) enum Errors<T: Display> {
#[display("Token price provider error: {}", _0)]
TokenPriceProviderError(T),
#[error("Token price provider error: {}", _0)]
TokenPriceProviderError(#[from] T),

#[display("Could not find token configuration for {}", _0)]
#[from(ignore)]
#[error("Could not find token configuration for {}", _0)]
TokenConfigurationNotFound(String),

#[display("Could not find token configuration for {} on chain {}", _0, _1)]
#[from(ignore)]
#[error("Could not find token configuration for {} on chain {}", _0, _1)]
TokenConfigurationNotFoundForChain(String, u32),
}

#[cfg(test)]
mod tests {
use std::fmt::Error;

use ruint::Uint;

use config::Config;
Expand Down Expand Up @@ -101,7 +105,7 @@ indexer_config:
struct TokenPriceProviderStub;

impl TokenPriceProvider for TokenPriceProviderStub {
type Error = String;
type Error = Error;

async fn get_token_price(&self, _: &String) -> Result<f64, Self::Error> {
Ok(0.1)
Expand Down
51 changes: 0 additions & 51 deletions crates/storage/src/account.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pub mod db_provider;
pub mod errors;
pub mod mongodb_provider;

mod account;
mod redis;

pub trait RoutingModelStore {
Expand Down

0 comments on commit d9c6d0d

Please sign in to comment.