diff --git a/frontend/src/components/Dialogs/Exchange.tsx b/frontend/src/components/Dialogs/Exchange.tsx index 4cb01c3c..04e1ef8c 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,14 @@ interface Props { const ExchangeDialog = ({ open = false, onClose }: Props): JSX.Element => { const { t } = useTranslation(); - const { federation } = useContext(FederationContext); + const { federation, coordinatorUpdatedAt, 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(() => { + const loadedCoordinators = + federation.exchange.enabledCoordinators - federation.exchange.loadingCoordinators; + setLoadingProgress((loadedCoordinators / federation.exchange.enabledCoordinators) * 100); + }, [open, coordinatorUpdatedAt, federationUpdatedAt]); return ( @@ -69,7 +72,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..5a4158a0 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 }; @@ -38,19 +36,20 @@ export const updateExchangeInfo = (federation: Federation): ExchangeInfo => { 'lifetime_volume', ]; - Object.values(federation.coordinators).forEach((coordinator, index) => { - if (coordinator.info !== undefined && coordinator.enabled === true) { - premiums[index] = coordinator.info.last_day_nonkyc_btc_premium; - volumes[index] = coordinator.info.last_day_volume; - highestVersion = getHigherVer(highestVersion, coordinator.info.version); - active_robots_today = Math.max(active_robots_today, coordinator.info.active_robots_today); + Object.values(federation.coordinators) + .filter((coor) => coor.isUpdated()) + .forEach((coordinator, index) => { + 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); + active_robots_today = Math.max(active_robots_today, coordinator.info.active_robots_today); - aggregations.forEach((key: any) => { - info[key] = Number(info[key]) + Number(coordinator.info[key]); - }); - } - return null; - }); + aggregations.forEach((key: any) => { + info[key] = Number(info[key]) + Number(coordinator.info[key]); + }); + } + }); info.last_day_nonkyc_btc_premium = weightedMean(premiums, volumes); info.version = highestVersion; @@ -68,7 +67,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..25e95b78 100644 --- a/frontend/src/models/Federation.model.ts +++ b/frontend/src/models/Federation.model.ts @@ -65,10 +65,8 @@ export class Federation { this.exchange.loadingCoordinators = this.exchange.loadingCoordinators < 1 ? 0 : this.exchange.loadingCoordinators - 1; this.loading = this.exchange.loadingCoordinators > 0; - if (Object.values(this.coordinators).every((coor) => coor.isUpdated())) { - this.updateExchange(); - this.triggerHook('onFederationUpdate'); - } + this.updateExchange(); + this.triggerHook('onFederationUpdate'); }; // Setup @@ -98,6 +96,16 @@ export class Federation { update = async (): Promise => { this.loading = true; + this.exchange.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 }, + }; this.exchange.loadingCoordinators = Object.keys(this.coordinators).length; for (const coor of Object.values(this.coordinators)) { await coor.update(() => { @@ -151,6 +159,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;