Skip to content

Commit

Permalink
feat: return shared structures from chain service in webserver
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Sep 9, 2024
1 parent c4afcca commit c07b1f0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
26 changes: 26 additions & 0 deletions orm/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,29 @@ impl From<(Parameters, Genesis, Checksums, EpochSwitchBlocksDelay)>
}
}
}

impl From<ParametersDb>
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,
)
}
}
2 changes: 1 addition & 1 deletion webserver/src/handler/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub async fn get_parameters(
) -> Result<Json<Parameters>, 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<CommonState>) -> Json<RpcUrl> {
Expand Down
24 changes: 13 additions & 11 deletions webserver/src/response/chain.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -19,23 +20,24 @@ pub struct Parameters {
pub epoch_switch_blocks_delay: String,
}

impl From<ParametersDb> for Parameters {
fn from(parameters: ParametersDb) -> Self {
impl From<LatestParameters> 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(),
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions webserver/src/service/chain.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -17,12 +23,12 @@ impl ChainService {

pub async fn find_latest_parameters(
&self,
) -> Result<Parameters, ChainError> {
) -> Result<LatestParameters, ChainError> {
let parameters = self
.chain_repo
.find_chain_parameters()
.await
.map(Parameters::from)
.map(LatestParameters::from)
.map_err(ChainError::Database)?;

Ok(parameters)
Expand Down

0 comments on commit c07b1f0

Please sign in to comment.