From 58434df320a6b538e7d6423497a3aa32a5da0fe2 Mon Sep 17 00:00:00 2001 From: KoalaSat Date: Thu, 14 Mar 2024 20:56:14 +0100 Subject: [PATCH] Fix federation summary --- frontend/src/components/Dialogs/Exchange.tsx | 21 +++++-- frontend/src/models/Exchange.model.ts | 36 +++++++----- frontend/src/models/Federation.model.ts | 1 + frontend/src/utils/aggregateInfo.ts | 58 +------------------- 4 files changed, 38 insertions(+), 78 deletions(-) diff --git a/frontend/src/components/Dialogs/Exchange.tsx b/frontend/src/components/Dialogs/Exchange.tsx index 4cb01c3c..606d6f2c 100644 --- a/frontend/src/components/Dialogs/Exchange.tsx +++ b/frontend/src/components/Dialogs/Exchange.tsx @@ -1,4 +1,4 @@ -import React, { useContext, useMemo } from 'react'; +import React, { useContext, useEffect, useMemo, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { @@ -34,11 +34,20 @@ interface Props { const ExchangeDialog = ({ open = false, onClose }: Props): JSX.Element => { const { t } = useTranslation(); - const { federation } = useContext(FederationContext); + const { federation, federationUpdatedAt } = useContext(FederationContext); + const [loadingProgress, setLoadingProgress] = useState(0); - const loadingProgress = useMemo(() => { - return (federation.exchange.onlineCoordinators / federation.exchange.totalCoordinators) * 100; - }, [federation.exchange.onlineCoordinators, federation.exchange.totalCoordinators]); + useEffect(() => federation.updateExchange(), []); + + useEffect(() => { + setLoadingProgress( + (federation.exchange.onlineCoordinators / federation.exchange.totalCoordinators) * 100, + ); + }, [ + federationUpdatedAt, + federation.exchange.onlineCoordinators, + federation.exchange.totalCoordinators, + ]); return ( @@ -69,7 +78,7 @@ const ExchangeDialog = ({ open = false, onClose }: Props): JSX.Element => { diff --git a/frontend/src/models/Exchange.model.ts b/frontend/src/models/Exchange.model.ts index 74b5a294..0c5cfa73 100644 --- a/frontend/src/models/Exchange.model.ts +++ b/frontend/src/models/Exchange.model.ts @@ -12,19 +12,17 @@ interface ExchangeInfo { version: Version; } -const defaultExchangeInfo: ExchangeInfo = { - num_public_buy_orders: 0, - num_public_sell_orders: 0, - book_liquidity: 0, - active_robots_today: 0, - last_day_nonkyc_btc_premium: 0, - last_day_volume: 0, - lifetime_volume: 0, - version: { major: 0, minor: 0, patch: 0 }, -}; - export const updateExchangeInfo = (federation: Federation): ExchangeInfo => { - const info: ExchangeInfo = defaultExchangeInfo; + const info: ExchangeInfo = { + num_public_buy_orders: 0, + num_public_sell_orders: 0, + book_liquidity: 0, + active_robots_today: 0, + last_day_nonkyc_btc_premium: 0, + last_day_volume: 0, + lifetime_volume: 0, + version: { major: 0, minor: 0, patch: 0 }, + }; const premiums: number[] = []; const volumes: number[] = []; let highestVersion: Version = { major: 0, minor: 0, patch: 0 }; @@ -39,7 +37,7 @@ export const updateExchangeInfo = (federation: Federation): ExchangeInfo => { ]; Object.values(federation.coordinators).forEach((coordinator, index) => { - if (coordinator.info !== undefined && coordinator.enabled === true) { + if (coordinator.info !== undefined) { premiums[index] = coordinator.info.last_day_nonkyc_btc_premium; volumes[index] = coordinator.info.last_day_volume; highestVersion = getHigherVer(highestVersion, coordinator.info.version); @@ -49,7 +47,6 @@ export const updateExchangeInfo = (federation: Federation): ExchangeInfo => { info[key] = Number(info[key]) + Number(coordinator.info[key]); }); } - return null; }); info.last_day_nonkyc_btc_premium = weightedMean(premiums, volumes); @@ -68,7 +65,16 @@ export interface Exchange { } export const defaultExchange: Exchange = { - info: defaultExchangeInfo, + info: { + num_public_buy_orders: 0, + num_public_sell_orders: 0, + book_liquidity: 0, + active_robots_today: 0, + last_day_nonkyc_btc_premium: 0, + last_day_volume: 0, + lifetime_volume: 0, + version: { major: 0, minor: 0, patch: 0 }, + }, enabledCoordinators: 0, onlineCoordinators: 0, loadingCoordinators: 0, diff --git a/frontend/src/models/Federation.model.ts b/frontend/src/models/Federation.model.ts index ed9a0aa8..b523b4cf 100644 --- a/frontend/src/models/Federation.model.ts +++ b/frontend/src/models/Federation.model.ts @@ -151,6 +151,7 @@ export class Federation { this.exchange.enabledCoordinators = Object.values(this.coordinators).filter( (c) => c.enabled, ).length; + this.triggerHook('onFederationUpdate'); }; } diff --git a/frontend/src/utils/aggregateInfo.ts b/frontend/src/utils/aggregateInfo.ts index ac42baa7..910a80d5 100644 --- a/frontend/src/utils/aggregateInfo.ts +++ b/frontend/src/utils/aggregateInfo.ts @@ -1,5 +1,3 @@ -import { type Coordinator } from '../models'; - interface Version { major: number | null; minor: number | null; @@ -61,58 +59,4 @@ const getHigherVer = (ver0: Version, ver1: Version): Version => { } }; -export const aggregateInfo = (federation: Coordinator[]): AggregatedInfo => { - const info = { - onlineCoordinators: 0, - totalCoordinators: 0, - num_public_buy_orders: 0, - num_public_sell_orders: 0, - book_liquidity: 0, - active_robots_today: 0, - last_day_nonkyc_btc_premium: 0, - last_day_volume: 0, - lifetime_volume: 0, - version: { major: 0, minor: 0, patch: 0 }, - }; - info.totalCoordinators = federation.length; - const addUp: toAdd[] = [ - 'num_public_buy_orders', - 'num_public_sell_orders', - 'book_liquidity', - 'active_robots_today', - 'last_day_volume', - 'lifetime_volume', - ]; - - addUp.map((key) => { - let value = 0; - federation.map((coordinator) => { - if (coordinator.info != null) { - value = value + coordinator.info[key]; - } - return null; - }); - info[key] = value; - return null; - }); - - const premiums: number[] = []; - const volumes: number[] = []; - let highestVersion = { major: 0, minor: 0, patch: 0 }; - federation.map((coordinator, index) => { - if (coordinator.info != null) { - info.onlineCoordinators = info.onlineCoordinators + 1; - premiums[index] = coordinator.info.last_day_nonkyc_btc_premium; - volumes[index] = coordinator.info.last_day_volume; - highestVersion = getHigherVer(highestVersion, coordinator.info.version); - } - return null; - }); - - info.last_day_nonkyc_btc_premium = weightedMean(premiums, volumes); - info.version = highestVersion; - - return info; -}; - -export default aggregateInfo; +export default getHigherVer;