From a9933d35dafbb77fc548b8ec9ae2ad586c6b9753 Mon Sep 17 00:00:00 2001 From: Mateusz Jasiuk Date: Tue, 30 Jul 2024 11:58:26 +0200 Subject: [PATCH 1/4] refactor: returned shared structs from services --- orm/src/balances.rs | 15 ++++++++++++++- shared/src/id.rs | 8 ++++++++ webserver/src/handler/balance.rs | 13 ++++++++++++- webserver/src/service/balance.rs | 24 ++++++------------------ 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/orm/src/balances.rs b/orm/src/balances.rs index b1c1540ee..f20c6fae5 100644 --- a/orm/src/balances.rs +++ b/orm/src/balances.rs @@ -2,7 +2,10 @@ use std::str::FromStr; use bigdecimal::BigDecimal; use diesel::{Insertable, Queryable, Selectable}; -use shared::balance::Balance; +use shared::{ + balance::{Amount, Balance}, + id::Id, +}; use crate::schema::balances; @@ -27,3 +30,13 @@ impl BalancesInsertDb { } } } + +impl From for Balance { + fn from(balance: BalanceDb) -> Self { + Self { + owner: Id::from_account_str(&balance.owner), + token: Id::from_account_str(&balance.token), + amount: Amount::from(balance.raw_amount), + } + } +} diff --git a/shared/src/id.rs b/shared/src/id.rs index 7c6864b5e..f7cce54ce 100644 --- a/shared/src/id.rs +++ b/shared/src/id.rs @@ -82,3 +82,11 @@ impl From for Id { Id::Account(value.to_string()) } } + +impl Id { + pub fn from_account_str(address: &str) -> Self { + let account = + NamadaAddress::from_str(address).expect("Invalid address"); + Self::from(account) + } +} diff --git a/webserver/src/handler/balance.rs b/webserver/src/handler/balance.rs index e7e05f963..5addbf72f 100644 --- a/webserver/src/handler/balance.rs +++ b/webserver/src/handler/balance.rs @@ -2,6 +2,7 @@ use axum::extract::{Path, State}; use axum::http::HeaderMap; use axum::Json; use axum_macros::debug_handler; +use shared::balance::DenominatedAmount; use crate::error::api::ApiError; use crate::response::balance::AddressBalance; @@ -15,5 +16,15 @@ pub async fn get_address_balance( ) -> Result>, ApiError> { let balances = state.balance_service.get_address_balances(address).await?; - Ok(Json(balances)) + let balances_response: Vec = balances + .iter() + .map(|balance| AddressBalance { + token_address: balance.token.to_string(), + // TODO: temporary solution as we only store NAM balances + balance: DenominatedAmount::native(balance.amount.clone()) + .to_string_precise(), + }) + .collect(); + + Ok(Json(balances_response)) } diff --git a/webserver/src/service/balance.rs b/webserver/src/service/balance.rs index 22a50d652..4046615c2 100644 --- a/webserver/src/service/balance.rs +++ b/webserver/src/service/balance.rs @@ -1,9 +1,8 @@ -use shared::balance::{Amount, DenominatedAmount}; +use shared::balance::{Amount, Balance, DenominatedAmount}; use crate::appstate::AppState; use crate::error::balance::BalanceError; use crate::repository::balance::{BalanceRepo, BalanceRepoTrait}; -use crate::response::balance::AddressBalance; #[derive(Clone)] pub struct BalanceService { @@ -20,27 +19,16 @@ impl BalanceService { pub async fn get_address_balances( &self, address: String, - ) -> Result, BalanceError> { - let balances = self + ) -> Result, BalanceError> { + let balances_db = self .balance_repo .get_address_balances(address) .await .map_err(BalanceError::Database)?; - // TODO: temporary solution as we only store NAM balances - let denominated_balances: Vec = balances - .iter() - .cloned() - .map(|balance| AddressBalance { - token_address: balance.token, - // TODO: change native to new once we support multiple tokens - balance: DenominatedAmount::native(Amount::from( - balance.raw_amount, - )) - .to_string_precise(), - }) - .collect(); + let balances: Vec = + balances_db.iter().cloned().map(Balance::from).collect(); - Ok(denominated_balances) + Ok(balances) } } From c4afcca321cc61aa1fa9fbfb0aab68e648878e8f Mon Sep 17 00:00:00 2001 From: Mateusz Jasiuk Date: Tue, 30 Jul 2024 12:10:23 +0200 Subject: [PATCH 2/4] feat: add from Balance to BalanceAddress response --- orm/src/balances.rs | 6 ++---- webserver/src/handler/balance.rs | 12 ++---------- webserver/src/response/balance.rs | 12 +++++++----- webserver/src/service/balance.rs | 2 +- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/orm/src/balances.rs b/orm/src/balances.rs index f20c6fae5..16b0f90c4 100644 --- a/orm/src/balances.rs +++ b/orm/src/balances.rs @@ -2,10 +2,8 @@ use std::str::FromStr; use bigdecimal::BigDecimal; use diesel::{Insertable, Queryable, Selectable}; -use shared::{ - balance::{Amount, Balance}, - id::Id, -}; +use shared::balance::{Amount, Balance}; +use shared::id::Id; use crate::schema::balances; diff --git a/webserver/src/handler/balance.rs b/webserver/src/handler/balance.rs index 5addbf72f..0ca544e92 100644 --- a/webserver/src/handler/balance.rs +++ b/webserver/src/handler/balance.rs @@ -2,7 +2,6 @@ use axum::extract::{Path, State}; use axum::http::HeaderMap; use axum::Json; use axum_macros::debug_handler; -use shared::balance::DenominatedAmount; use crate::error::api::ApiError; use crate::response::balance::AddressBalance; @@ -16,15 +15,8 @@ pub async fn get_address_balance( ) -> Result>, ApiError> { let balances = state.balance_service.get_address_balances(address).await?; - let balances_response: Vec = balances - .iter() - .map(|balance| AddressBalance { - token_address: balance.token.to_string(), - // TODO: temporary solution as we only store NAM balances - balance: DenominatedAmount::native(balance.amount.clone()) - .to_string_precise(), - }) - .collect(); + let balances_response: Vec = + balances.iter().cloned().map(AddressBalance::from).collect(); Ok(Json(balances_response)) } diff --git a/webserver/src/response/balance.rs b/webserver/src/response/balance.rs index 042343dec..fb53bb8ab 100644 --- a/webserver/src/response/balance.rs +++ b/webserver/src/response/balance.rs @@ -1,5 +1,5 @@ -use orm::balances::BalanceDb; use serde::{Deserialize, Serialize}; +use shared::balance::{Balance, DenominatedAmount}; #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] @@ -8,11 +8,13 @@ pub struct AddressBalance { pub balance: String, } -impl From for AddressBalance { - fn from(value: BalanceDb) -> Self { +impl From for AddressBalance { + fn from(value: Balance) -> Self { Self { - token_address: value.token, - balance: value.raw_amount.to_string(), + token_address: value.token.to_string(), + // TODO: temporary solution as we only store NAM balances + balance: DenominatedAmount::native(value.amount.clone()) + .to_string_precise(), } } } diff --git a/webserver/src/service/balance.rs b/webserver/src/service/balance.rs index 4046615c2..2f793c0e2 100644 --- a/webserver/src/service/balance.rs +++ b/webserver/src/service/balance.rs @@ -1,4 +1,4 @@ -use shared::balance::{Amount, Balance, DenominatedAmount}; +use shared::balance::Balance; use crate::appstate::AppState; use crate::error::balance::BalanceError; From c07b1f081e2c7db6d1d2bb91461fa041cdadf9f5 Mon Sep 17 00:00:00 2001 From: Mateusz Jasiuk Date: Mon, 9 Sep 2024 13:19:44 +0200 Subject: [PATCH 3/4] feat: return shared structures from chain service in webserver --- orm/src/parameters.rs | 26 ++++++++++++++++++++++++++ webserver/src/handler/chain.rs | 2 +- webserver/src/response/chain.rs | 24 +++++++++++++----------- webserver/src/service/chain.rs | 12 +++++++++--- 4 files changed, 49 insertions(+), 15 deletions(-) diff --git a/orm/src/parameters.rs b/orm/src/parameters.rs index 7438f8ede..a44ec1eb5 100644 --- a/orm/src/parameters.rs +++ b/orm/src/parameters.rs @@ -74,3 +74,29 @@ impl From<(Parameters, Genesis, Checksums, EpochSwitchBlocksDelay)> } } } + +impl From + for (Parameters, Genesis, Checksums, EpochSwitchBlocksDelay) +{ + fn from(parameters: ParametersDb) -> Self { + ( + Parameters { + unbonding_length: parameters.unbonding_length as u64, + pipeline_length: parameters.pipeline_length as u64, + epochs_per_year: parameters.epochs_per_year as u64, + min_num_of_blocks: parameters.min_num_of_blocks as u64, + min_duration: parameters.min_duration as u64, + max_block_time: parameters.max_block_time as u64, + apr: parameters.apr, + native_token_address: parameters.native_token_address, + }, + Genesis { + chain_id: parameters.chain_id, + genesis_time: parameters.genesis_time, + }, + serde_json::from_value(parameters.checksums) + .expect("Failed to deserialize checksums"), + parameters.epoch_switch_blocks_delay as u32, + ) + } +} diff --git a/webserver/src/handler/chain.rs b/webserver/src/handler/chain.rs index 8373d80fa..9b765940c 100644 --- a/webserver/src/handler/chain.rs +++ b/webserver/src/handler/chain.rs @@ -60,7 +60,7 @@ pub async fn get_parameters( ) -> Result, ApiError> { let parameters = state.chain_service.find_latest_parameters().await?; - Ok(Json(parameters)) + Ok(Json(Parameters::from(parameters))) } pub async fn get_rpc_url(State(state): State) -> Json { diff --git a/webserver/src/response/chain.rs b/webserver/src/response/chain.rs index b93bc3178..d57b6afb2 100644 --- a/webserver/src/response/chain.rs +++ b/webserver/src/response/chain.rs @@ -1,7 +1,8 @@ -use orm::parameters::ParametersDb; use serde::{Deserialize, Serialize}; use serde_json::Value as SerdeJSONValue; +use crate::service::chain::LatestParameters; + #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct Parameters { @@ -19,23 +20,24 @@ pub struct Parameters { pub epoch_switch_blocks_delay: String, } -impl From for Parameters { - fn from(parameters: ParametersDb) -> Self { +impl From for Parameters { + fn from( + (parameters, genesis, checksums, epoch_switch_blocks_delay): LatestParameters, + ) -> Parameters { Self { unbonding_length: parameters.unbonding_length.to_string(), pipeline_length: parameters.pipeline_length.to_string(), epochs_per_year: parameters.epochs_per_year.to_string(), - apr: parameters.apr, - native_token_address: parameters.native_token_address, - chain_id: parameters.chain_id, - genesis_time: parameters.genesis_time.to_string(), + apr: parameters.apr.to_string(), + native_token_address: parameters.native_token_address.to_string(), + chain_id: genesis.chain_id.to_string(), + genesis_time: genesis.genesis_time.to_string(), min_duration: parameters.min_duration.to_string(), min_num_of_blocks: parameters.min_num_of_blocks.to_string(), max_block_time: parameters.max_block_time.to_string(), - checksums: parameters.checksums, - epoch_switch_blocks_delay: parameters - .epoch_switch_blocks_delay - .to_string(), + checksums: serde_json::to_value(checksums) + .expect("Failed to serialize checksums"), + epoch_switch_blocks_delay: epoch_switch_blocks_delay.to_string(), } } } diff --git a/webserver/src/service/chain.rs b/webserver/src/service/chain.rs index b2ef5cfbb..10f520d1f 100644 --- a/webserver/src/service/chain.rs +++ b/webserver/src/service/chain.rs @@ -1,13 +1,19 @@ +use shared::checksums::Checksums; +use shared::genesis::Genesis; +use shared::parameters::{EpochSwitchBlocksDelay, Parameters}; + use crate::appstate::AppState; use crate::error::chain::ChainError; use crate::repository::chain::{ChainRepository, ChainRepositoryTrait}; -use crate::response::chain::Parameters; #[derive(Clone)] pub struct ChainService { chain_repo: ChainRepository, } +pub type LatestParameters = + (Parameters, Genesis, Checksums, EpochSwitchBlocksDelay); + impl ChainService { pub fn new(app_state: AppState) -> Self { Self { @@ -17,12 +23,12 @@ impl ChainService { pub async fn find_latest_parameters( &self, - ) -> Result { + ) -> Result { let parameters = self .chain_repo .find_chain_parameters() .await - .map(Parameters::from) + .map(LatestParameters::from) .map_err(ChainError::Database)?; Ok(parameters) From c79a1a5edccb2251ef5c70cf90b3cd6e76844be7 Mon Sep 17 00:00:00 2001 From: Mateusz Jasiuk Date: Mon, 9 Sep 2024 17:34:05 +0200 Subject: [PATCH 4/4] refactor: crawler state webserver service --- orm/src/crawler_state.rs | 13 +++++++++++-- shared/src/crawler_state.rs | 6 ++++++ webserver/src/handler/crawler_state.rs | 11 ++++------- webserver/src/response/crawler_state.rs | 18 +++++++++++++++++- webserver/src/service/crawler_state.rs | 11 ++++------- 5 files changed, 42 insertions(+), 17 deletions(-) diff --git a/orm/src/crawler_state.rs b/orm/src/crawler_state.rs index 90b8c6478..10355662f 100644 --- a/orm/src/crawler_state.rs +++ b/orm/src/crawler_state.rs @@ -5,8 +5,8 @@ use diesel::sql_types::Nullable; use diesel::{Insertable, Queryable, Selectable}; use serde::{Deserialize, Serialize}; use shared::crawler_state::{ - BlockCrawlerState, ChainCrawlerState, CrawlerName, EpochCrawlerState, - IntervalCrawlerState, + BlockCrawlerState, ChainCrawlerState, CrawlerName, CrawlerTimestamp, + EpochCrawlerState, IntervalCrawlerState, }; use crate::schema::crawler_state; @@ -273,3 +273,12 @@ impl From<(CrawlerName, IntervalCrawlerState)> for IntervalStateInsertDb { } } } + +impl From for CrawlerTimestamp { + fn from(value: CrawlerStateDb) -> Self { + Self { + name: value.name.to_string(), + timestamp: value.timestamp.and_utc().timestamp(), + } + } +} diff --git a/shared/src/crawler_state.rs b/shared/src/crawler_state.rs index d601001c1..8bc5b0f66 100644 --- a/shared/src/crawler_state.rs +++ b/shared/src/crawler_state.rs @@ -33,3 +33,9 @@ pub struct EpochCrawlerState { pub struct IntervalCrawlerState { pub timestamp: i64, } + +#[derive(Debug)] +pub struct CrawlerTimestamp { + pub name: String, + pub timestamp: i64, +} diff --git a/webserver/src/handler/crawler_state.rs b/webserver/src/handler/crawler_state.rs index eecbf5bf3..7bd54ee02 100644 --- a/webserver/src/handler/crawler_state.rs +++ b/webserver/src/handler/crawler_state.rs @@ -6,14 +6,14 @@ use strum::VariantArray; use crate::dto::crawler_state::{CrawlerNameDto, CrawlerStateQueryParams}; use crate::error::api::ApiError; -use crate::response::crawler_state::CrawlersTimestamps; +use crate::response::crawler_state::CrawlerTimestamp; use crate::state::common::CommonState; pub async fn get_crawlers_timestamps( _headers: HeaderMap, Query(query): Query, State(state): State, -) -> Result>, ApiError> { +) -> Result>, ApiError> { let crawler_names = query.crawler_names.unwrap_or(vec![]); let timestamps = state @@ -38,11 +38,8 @@ pub async fn get_crawlers_timestamps( .iter() .find(|timestamp| timestamp.name == variant.to_string()) .map_or_else( - || CrawlersTimestamps { - name: variant.to_string(), - timestamp: 0, - }, - |ct| ct.clone(), + || CrawlerTimestamp::empty(variant.to_string()), + CrawlerTimestamp::from, ) }) .collect::>(); diff --git a/webserver/src/response/crawler_state.rs b/webserver/src/response/crawler_state.rs index d12fe51dc..420b452e6 100644 --- a/webserver/src/response/crawler_state.rs +++ b/webserver/src/response/crawler_state.rs @@ -1,8 +1,24 @@ use serde::{Deserialize, Serialize}; +use shared::crawler_state::CrawlerTimestamp as SharedCrawlerTimestamp; #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] -pub struct CrawlersTimestamps { +pub struct CrawlerTimestamp { pub name: String, pub timestamp: i64, } + +impl CrawlerTimestamp { + pub fn empty(name: String) -> Self { + Self { name, timestamp: 0 } + } +} + +impl From<&SharedCrawlerTimestamp> for CrawlerTimestamp { + fn from(shared: &SharedCrawlerTimestamp) -> Self { + Self { + name: shared.name.clone(), + timestamp: shared.timestamp, + } + } +} diff --git a/webserver/src/service/crawler_state.rs b/webserver/src/service/crawler_state.rs index 0ca9c805c..3106a9d40 100644 --- a/webserver/src/service/crawler_state.rs +++ b/webserver/src/service/crawler_state.rs @@ -1,11 +1,11 @@ use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl}; use orm::crawler_state::{CrawlerNameDb, CrawlerStateDb}; use orm::schema::crawler_state; +use shared::crawler_state::CrawlerTimestamp; use crate::appstate::AppState; use crate::dto::crawler_state::CrawlerNameDto; use crate::error::crawler_state::CrawlerStateError; -use crate::response::crawler_state::CrawlersTimestamps; #[derive(Clone)] pub struct CrawlerStateService { @@ -20,7 +20,7 @@ impl CrawlerStateService { pub async fn get_timestamps( &self, names: Vec, - ) -> Result, CrawlerStateError> { + ) -> Result, CrawlerStateError> { let conn = self.app_state.get_db_connection().await; let names_db = names .iter() @@ -46,11 +46,8 @@ impl CrawlerStateService { crawlers_db.map(|crawlers| { crawlers .into_iter() - .map(|crawler| CrawlersTimestamps { - name: crawler.name.to_string(), - timestamp: crawler.timestamp.and_utc().timestamp(), - }) - .collect::>() + .map(CrawlerTimestamp::from) + .collect::>() }) }