Fix federation summary

This commit is contained in:
KoalaSat
2024-03-14 20:56:14 +01:00
parent 0ac68d78c8
commit 58434df320
4 changed files with 38 additions and 78 deletions

View File

@ -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<number>(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 (
<Dialog open={open} onClose={onClose}>
@ -69,7 +78,7 @@ const ExchangeDialog = ({ open = false, onClose }: Props): JSX.Element => {
</ListItemIcon>
<ListItemText
primary={federation.exchange.totalCoordinators}
primary={federation.exchange.enabledCoordinators}
secondary={t('Enabled RoboSats coordinators')}
/>
</ListItem>

View File

@ -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,

View File

@ -151,6 +151,7 @@ export class Federation {
this.exchange.enabledCoordinators = Object.values(this.coordinators).filter(
(c) => c.enabled,
).length;
this.triggerHook('onFederationUpdate');
};
}

View File

@ -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;