From f28d66ddc7704144fa6b907719bf07a93b519360 Mon Sep 17 00:00:00 2001 From: Nicola Benaglia Date: Sun, 15 Jun 2025 09:53:39 +0200 Subject: [PATCH] Add functions --- src/components/Minting/Minting.tsx | 94 ++++++++++------------------- src/components/Minting/Stats.tsx | 97 ++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 62 deletions(-) create mode 100644 src/components/Minting/Stats.tsx diff --git a/src/components/Minting/Minting.tsx b/src/components/Minting/Minting.tsx index 1745c2f..73d1aa7 100644 --- a/src/components/Minting/Minting.tsx +++ b/src/components/Minting/Minting.tsx @@ -42,6 +42,8 @@ import { useAtom, useSetAtom } from 'jotai'; import { memberGroupsAtom, txListAtom } from '../../atoms/global'; import { useTranslation } from 'react-i18next'; import { TransitionUp } from '../../common/Transitions.tsx'; +import { averageBlockDay, averageBlockTime, levelUpBlocks } from './Stats.tsx'; +import { node } from 'slate'; export const Minting = ({ setIsOpenMinting, myAddress, show }) => { const setTxList = useSetAtom(txListAtom); @@ -55,7 +57,7 @@ export const Minting = ({ setIsOpenMinting, myAddress, show }) => { const [openSnack, setOpenSnack] = useState(false); const [isLoading, setIsLoading] = useState(false); const [adminInfo, setAdminInfo] = useState({}); - const [nodeHeightBlock, setNodeHeightBlock] = useState(null); + const [nodeHeightBlock, setNodeHeightBlock] = useState({}); const [valueMintingTab, setValueMintingTab] = useState(0); const { isShow: isShowNext, onOk, show: showNext } = useModal(); const theme = useTheme(); @@ -201,17 +203,23 @@ export const Minting = ({ setIsOpenMinting, myAddress, show }) => { } }, []); - const getNodeHeightBlock = useCallback(async () => { - try { - const nodeBlock = parseFloat(nodeStatus?.height) - 1440; - const url = `${getBaseApiReact()}/blocks/byheight/${nodeBlock}`; - const response = await fetch(url); - const data = await response.json(); - setNodeHeightBlock(data); - } catch (error) { - console.error('Request failed', error); + useEffect(() => { + if (nodeStatus?.height) { + const getNodeHeightBlock = async () => { + try { + const nodeBlock = nodeStatus.height - 1440; + const url = `${getBaseApiReact()}/blocks/byheight/${nodeBlock}`; + const response = await fetch(url); + const data = await response.json(); + setNodeHeightBlock(data); + } catch (error) { + console.error('Request failed', error); + } + }; + + getNodeHeightBlock(); } - }, []); + }, [nodeStatus]); const getAddressLevel = useCallback(async () => { try { @@ -487,7 +495,6 @@ export const Minting = ({ setIsOpenMinting, myAddress, show }) => { getAddressLevel(); getAdminInfo(); getMintingAccounts(); - getNodeHeightBlock(); getNodeStatus(); }, []); @@ -497,30 +504,6 @@ export const Minting = ({ setIsOpenMinting, myAddress, show }) => { getAccountInfo(myAddress); }, [myAddress]); - const _blocksNeed = () => { - if (accountInfo?.level === 0) { - return 7200; // TODO manage these magic numbers in a proper location - } else if (accountInfo?.level === 1) { - return 72000; - } else if (accountInfo?.level === 2) { - return 201600; - } else if (accountInfo?.level === 3) { - return 374400; - } else if (accountInfo?.level === 4) { - return 618400; - } else if (accountInfo?.level === 5) { - return 964000; - } else if (accountInfo?.level === 6) { - return 1482400; - } else if (accountInfo?.level === 7) { - return 2173600; - } else if (accountInfo?.level === 8) { - return 3037600; - } else if (accountInfo?.level === 9) { - return 4074400; - } - }; - const handleClose = () => { setOpenSnack(false); setTimeout(() => { @@ -528,20 +511,6 @@ export const Minting = ({ setIsOpenMinting, myAddress, show }) => { }, 250); }; - const _levelUpBlocks = () => { - if ( - accountInfo?.blocksMinted === undefined || - nodeStatus?.height === undefined - ) - return null; - let countBlocks = - _blocksNeed() - - (accountInfo?.blocksMinted + accountInfo?.blocksMintedAdjustment); - - let countBlocksString = countBlocks.toString(); - return '' + countBlocksString; - }; - const StatCard = ({ label, value }: { label: string; value: string }) => ( @@ -654,31 +623,32 @@ export const Minting = ({ setIsOpenMinting, myAddress, show }) => { Blockchain Statistics - - @@ -695,8 +665,8 @@ export const Minting = ({ setIsOpenMinting, myAddress, show }) => { @@ -798,7 +768,7 @@ export const Minting = ({ setIsOpenMinting, myAddress, show }) => { {t('group:message.generic.next_level', { postProcess: 'capitalizeFirstChar', })}{' '} - {_levelUpBlocks()} + {levelUpBlocks(accountInfo, nodeStatus)} diff --git a/src/components/Minting/Stats.tsx b/src/components/Minting/Stats.tsx new file mode 100644 index 0000000..a05d072 --- /dev/null +++ b/src/components/Minting/Stats.tsx @@ -0,0 +1,97 @@ +export const averageBlockDay = (adminInfo, nodeHeightBlock) => { + const time = adminInfo.currentTimestamp - nodeHeightBlock.timestamp; + const average: number = time / 1000 / 1440; + const averageBlockDay = 86400 / average; + return averageBlockDay; +}; + +const accountTargetBlocks = (level: number) => { + if (level === 0) { + return 7200; + } else if (level === 1) { + return 72000; + } else if (level === 2) { + return 201600; + } else if (level === 3) { + return 374400; + } else if (level === 4) { + return 618400; + } else if (level === 5) { + return 964000; + } else if (level === 6) { + return 1482400; + } else if (level === 7) { + return 2173600; + } else if (level === 8) { + return 3037600; + } else if (level === 9) { + return 4074400; + } else { + return 0; // fallback: should never reach this point + } +}; + +export const accountLevel = (level: number) => { + if (level === 0) { + return '1'; + } else if (level === 1) { + return '2'; + } else if (level === 2) { + return '3'; + } else if (level === 3) { + return '4'; + } else if (level === 4) { + return '5'; + } else if (level === 5) { + return '6'; + } else if (level === 6) { + return '7'; + } else if (level === 7) { + return '8'; + } else if (level === 8) { + return '9'; + } else if (level === 9) { + return '10'; + } +}; + +export const levelUpBlocks = (accountInfo, nodeStatus) => { + if ( + accountInfo?.blocksMinted === undefined || + nodeStatus?.height === undefined + ) + return null; + + const countBlocks = + accountTargetBlocks(accountInfo?.level) - + (accountInfo?.blocksMinted + accountInfo?.blocksMintedAdjustment); + + const countBlocksString = countBlocks.toString(); + return countBlocksString; +}; + +export const levelUpDays = ( + accountInfo, + adminInfo, + nodeHeightBlock, + nodeStatus +) => { + if ( + accountInfo?.blocksMinted === undefined || + nodeStatus?.height === undefined + ) + return null; + + const countBlocks = + accountTargetBlocks(accountInfo?.level) - + (accountInfo?.blocksMinted + accountInfo?.blocksMintedAdjustment); + + const countDays = countBlocks / averageBlockDay(adminInfo, nodeHeightBlock); + return countDays.toFixed(2); +}; + +export const averageBlockTime = (adminInfo, nodeHeightBlock) => { + const avgBlockString = adminInfo.currentTimestamp - nodeHeightBlock.timestamp; + const averageTimeString = avgBlockString / 1000 / 1440; + return averageTimeString; +};