From 2952144d32399bf375d3a56c2f530183b3e6abdf Mon Sep 17 00:00:00 2001 From: Fouad Date: Tue, 1 Nov 2022 13:02:34 +0000 Subject: [PATCH] add profit margin percent to response (#1729) * add profit margin percent to response * use display percentage function * fix profit margin display * fix up filters --- explorer-api/src/mix_node/models.rs | 2 ++ explorer-api/src/mix_nodes/models.rs | 1 + explorer/src/components/MixNodes/Economics/Rows.ts | 5 ++++- explorer/src/components/MixNodes/index.ts | 3 ++- explorer/src/context/main.tsx | 6 ++++-- explorer/src/typeDefs/explorer-api.ts | 2 +- explorer/src/utils/index.ts | 8 ++++++++ 7 files changed, 22 insertions(+), 5 deletions(-) diff --git a/explorer-api/src/mix_node/models.rs b/explorer-api/src/mix_node/models.rs index 2ecb907f6a..1d709433b1 100644 --- a/explorer-api/src/mix_node/models.rs +++ b/explorer-api/src/mix_node/models.rs @@ -3,6 +3,7 @@ use crate::cache::Cache; use crate::mix_nodes::location::Location; +use contracts_common::Percent; use mixnet_contract_common::Delegation; use mixnet_contract_common::{Addr, Coin, Layer, MixId, MixNode}; use serde::Deserialize; @@ -37,6 +38,7 @@ pub(crate) struct PrettyDetailedMixNodeBond { pub estimated_operator_apy: f64, pub estimated_delegators_apy: f64, pub operating_cost: Coin, + pub profit_margin_percent: Percent, } #[derive(Clone, Debug, Deserialize, Serialize, PartialEq, JsonSchema)] diff --git a/explorer-api/src/mix_nodes/models.rs b/explorer-api/src/mix_nodes/models.rs index 69f05b84a0..07899742c9 100644 --- a/explorer-api/src/mix_nodes/models.rs +++ b/explorer-api/src/mix_nodes/models.rs @@ -154,6 +154,7 @@ impl ThreadsafeMixNodesCache { estimated_operator_apy: best_effort_small_dec_to_f64(node.estimated_operator_apy), estimated_delegators_apy: best_effort_small_dec_to_f64(node.estimated_delegators_apy), operating_cost: rewarding_info.cost_params.interval_operating_cost.clone(), + profit_margin_percent: rewarding_info.cost_params.profit_margin_percent, } } diff --git a/explorer/src/components/MixNodes/Economics/Rows.ts b/explorer/src/components/MixNodes/Economics/Rows.ts index bcc43914a5..a4f998a1f0 100644 --- a/explorer/src/components/MixNodes/Economics/Rows.ts +++ b/explorer/src/components/MixNodes/Economics/Rows.ts @@ -2,6 +2,7 @@ import { currencyToString, unymToNym } from '../../../utils/currency'; import { useMixnodeContext } from '../../../context/mixnode'; import { ApiState, MixNodeEconomicDynamicsStatsResponse } from '../../../typeDefs/explorer-api'; import { EconomicsInfoRowWithIndex } from './types'; +import { toPercentIntegerString } from '../../../utils'; const selectionChance = (economicDynamicsStats: ApiState | undefined) => { const inclusionProbability = economicDynamicsStats?.data?.active_set_inclusion_probability; @@ -29,7 +30,9 @@ export const EconomicsInfoRows = (): EconomicsInfoRowWithIndex => { const estimatedOperatorRewards = currencyToString((economicDynamicsStats?.data?.estimated_operator_reward || '').toString()) || '-'; const stakeSaturation = economicDynamicsStats?.data?.stake_saturation || '-'; - const profitMargin = mixNode?.data?.mix_node.profit_margin_percent || '-'; + const profitMargin = mixNode?.data?.profit_margin_percent + ? toPercentIntegerString(mixNode?.data?.profit_margin_percent) + : '-'; const avgUptime = economicDynamicsStats?.data?.current_interval_uptime; const opCost = mixNode?.data?.operating_cost; diff --git a/explorer/src/components/MixNodes/index.ts b/explorer/src/components/MixNodes/index.ts index bcf5b09efd..3435c3bf3f 100644 --- a/explorer/src/components/MixNodes/index.ts +++ b/explorer/src/components/MixNodes/index.ts @@ -1,5 +1,6 @@ /* eslint-disable camelcase */ import { MixNodeResponse, MixNodeResponseItem, MixnodeStatus } from '../../typeDefs/explorer-api'; +import { toPercentIntegerString } from '../../utils'; import { unymToNym } from '../../utils/currency'; export type MixnodeRowType = { @@ -29,7 +30,7 @@ export function mixNodeResponseItemToMixnodeRowType(item: MixNodeResponseItem): const delegations = Number(item.total_delegation.amount) || 0; const totalBond = pledge + delegations; const selfPercentage = ((pledge * 100) / totalBond).toFixed(2); - const profitPercentage = item.mix_node.profit_margin_percent || 0; + const profitPercentage = toPercentIntegerString(item.profit_margin_percent) || 0; const stakeSaturation = typeof item.stake_saturation === 'number' ? item.stake_saturation * 100 : 0; return { diff --git a/explorer/src/context/main.tsx b/explorer/src/context/main.tsx index a8db9e270e..42e89b32ee 100644 --- a/explorer/src/context/main.tsx +++ b/explorer/src/context/main.tsx @@ -96,15 +96,17 @@ export const MainContextProvider: React.FC = ({ children }) => { const filterMixnodes = async (filters: { [key in EnumFilterKey]: number[] }, status?: MixnodeStatus) => { setMixnodes((d) => ({ ...d, isLoading: true })); const mxns = status ? await Api.fetchMixnodesActiveSetByStatus(status) : await Api.fetchMixnodes(); + const filtered = mxns?.filter( (m) => - m.mix_node.profit_margin_percent >= filters.profitMargin[0] && - m.mix_node.profit_margin_percent <= filters.profitMargin[1] && + +m.profit_margin_percent >= filters.profitMargin[0] / 100 && + +m.profit_margin_percent <= filters.profitMargin[1] / 100 && m.stake_saturation >= filters.stakeSaturation[0] && m.stake_saturation <= filters.stakeSaturation[1] && m.avg_uptime >= filters.routingScore[0] && m.avg_uptime <= filters.routingScore[1], ); + setMixnodes({ data: filtered, isLoading: false }); }; diff --git a/explorer/src/typeDefs/explorer-api.ts b/explorer/src/typeDefs/explorer-api.ts index 68563ee82a..a01b87a1a9 100644 --- a/explorer/src/typeDefs/explorer-api.ts +++ b/explorer/src/typeDefs/explorer-api.ts @@ -30,7 +30,6 @@ export interface MixNode { sphinx_key: string; identity_key: string; version: string; - profit_margin_percent: number; location: string; } @@ -87,6 +86,7 @@ export interface MixNodeResponseItem { avg_uptime: number; stake_saturation: number; operating_cost: Amount; + profit_margin_percent: string; } export type MixNodeResponse = MixNodeResponseItem[]; diff --git a/explorer/src/utils/index.ts b/explorer/src/utils/index.ts index a44515b10c..1065c000ae 100644 --- a/explorer/src/utils/index.ts +++ b/explorer/src/utils/index.ts @@ -45,3 +45,11 @@ export const splice = (start: number, deleteCount: number, address?: string): st } return ''; }; + +/** + * Converts a stringified percentage float (0.0-1.0) to a stringified integer (0-100). + * + * @param value - the percentage to convert + * @returns A stringified integer + */ +export const toPercentIntegerString = (value: string) => Math.round(Number(value) * 100).toString();