diff --git a/validator-api/src/node_status_api/routes.rs b/validator-api/src/node_status_api/routes.rs index b4a9530566..986c0d5919 100644 --- a/validator-api/src/node_status_api/routes.rs +++ b/validator-api/src/node_status_api/routes.rs @@ -131,18 +131,28 @@ pub(crate) async fn get_mixnode_reward_estimation( .await .map_err(|err| ErrorResponse::new(err.to_string(), Status::NotFound))?; - let (estimated_total_node_reward, estimated_operator_reward, estimated_delegators_reward) = - epoch_reward_params.estimate_reward(&bond, uptime.u8(), status.is_active()); - - Ok(Json(RewardEstimationResponse { - estimated_total_node_reward, - estimated_operator_reward, - estimated_delegators_reward, - current_epoch_start: current_epoch.start_unix_timestamp(), - current_epoch_end: current_epoch.end_unix_timestamp(), - current_epoch_uptime: uptime.u8(), - as_at, - })) + match epoch_reward_params.estimate_reward(&bond, uptime.u8(), status.is_active()) { + Ok(( + estimated_total_node_reward, + estimated_operator_reward, + estimated_delegators_reward, + )) => { + let reponse = RewardEstimationResponse { + estimated_total_node_reward, + estimated_operator_reward, + estimated_delegators_reward, + current_epoch_start: current_epoch.start_unix_timestamp(), + current_epoch_end: current_epoch.end_unix_timestamp(), + current_epoch_uptime: uptime.u8(), + as_at, + }; + Ok(Json(reponse)) + } + Err(e) => Err(ErrorResponse::new( + e.to_string(), + Status::InternalServerError, + )), + } } else { Err(ErrorResponse::new( "mixnode bond not found", diff --git a/validator-api/src/rewarding/error.rs b/validator-api/src/rewarding/error.rs index 347fa07ad6..4f20a878aa 100644 --- a/validator-api/src/rewarding/error.rs +++ b/validator-api/src/rewarding/error.rs @@ -23,6 +23,12 @@ pub(crate) enum RewardingError { #[error("Failed to query the smart contract - {0}")] ValidatorClientError(ValidatorClientError), + + #[error("Error downcasting u128 -> u64")] + DowncastingError { + #[from] + source: std::num::TryFromIntError, + }, } impl From for RewardingError { diff --git a/validator-api/src/rewarding/mod.rs b/validator-api/src/rewarding/mod.rs index 74222bb16b..fc67a7bba8 100644 --- a/validator-api/src/rewarding/mod.rs +++ b/validator-api/src/rewarding/mod.rs @@ -58,7 +58,7 @@ impl EpochRewardParams { node: &MixNodeBond, uptime: u8, in_active_set: bool, - ) -> (u128, u128, u128) { + ) -> Result<(u64, u64, u64), RewardingError> { let node_reward_params = NodeRewardParams::new( self.period_reward_pool, self.rewarded_set_size.into(), @@ -76,14 +76,15 @@ impl EpochRewardParams { let delegators_reward = node.reward_delegation(node.total_delegation().amount, &node_reward_params); - ( + Ok(( total_node_reward .reward() - .checked_to_num() - .unwrap_or_default(), - operator_reward, - delegators_reward, - ) + .checked_to_num::() + .unwrap_or_default() + .try_into()?, + operator_reward.try_into()?, + delegators_reward.try_into()?, + )) } } diff --git a/validator-api/validator-api-requests/src/models.rs b/validator-api/validator-api-requests/src/models.rs index fddd64d788..a7e5640837 100644 --- a/validator-api/validator-api-requests/src/models.rs +++ b/validator-api/validator-api-requests/src/models.rs @@ -35,9 +35,9 @@ pub struct MixnodeStatusResponse { #[derive(Clone, Copy, Debug, Serialize, Deserialize)] #[cfg_attr(feature = "ts-rs", derive(ts_rs::TS))] pub struct RewardEstimationResponse { - pub estimated_total_node_reward: u128, - pub estimated_operator_reward: u128, - pub estimated_delegators_reward: u128, + pub estimated_total_node_reward: u64, + pub estimated_operator_reward: u64, + pub estimated_delegators_reward: u64, pub current_epoch_start: i64, pub current_epoch_end: i64,