Add functions

This commit is contained in:
Nicola Benaglia 2025-06-15 09:53:39 +02:00
parent 0c42d041ba
commit f28d66ddc7
2 changed files with 129 additions and 62 deletions

View File

@ -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,9 +203,11 @@ export const Minting = ({ setIsOpenMinting, myAddress, show }) => {
}
}, []);
const getNodeHeightBlock = useCallback(async () => {
useEffect(() => {
if (nodeStatus?.height) {
const getNodeHeightBlock = async () => {
try {
const nodeBlock = parseFloat(nodeStatus?.height) - 1440;
const nodeBlock = nodeStatus.height - 1440;
const url = `${getBaseApiReact()}/blocks/byheight/${nodeBlock}`;
const response = await fetch(url);
const data = await response.json();
@ -211,7 +215,11 @@ export const Minting = ({ setIsOpenMinting, myAddress, show }) => {
} 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 }) => (
<Grid size={{ xs: 4, sm: 6 }}>
<Paper elevation={5}>
@ -654,31 +623,32 @@ export const Minting = ({ setIsOpenMinting, myAddress, show }) => {
<Typography
variant="h3"
gutterBottom
sx={{ textAlign: 'center' }}
sx={{ textAlign: 'center' }} // TODO translate
>
Blockchain Statistics
</Typography>
<Grid container spacing={2}>
<StatCard
label="Avg. Qortal Blocktime"
value="72.84 Seconds"
label="Avg. Qortal Blocktime (seconds)"
value={averageBlockTime(
adminInfo,
nodeHeightBlock
).toFixed(2)}
/>
<StatCard
label="Avg. Blocks Per Day"
value="1186.16 Blocks"
value={averageBlockDay(
adminInfo,
nodeHeightBlock
).toFixed(2)}
/>
<StatCard
label="Avg. Created QORT Per Day"
value="3558.48 QORT"
/>
<StatCard
label="Nicola: nodeInfo"
value={nodeStatus?.height}
/>
</Grid>
</Paper>
@ -695,8 +665,8 @@ export const Minting = ({ setIsOpenMinting, myAddress, show }) => {
<StatCard label="Current Status" value="(Minting)" />
<StatCard label="Current Level" value="Level 4" />
<StatCard
label="NICO: Blocks To Next Level"
value={nodeHeightBlock}
label="Blocks To Next Level"
value={levelUpBlocks(accountInfo, nodeStatus) || ''}
/>
</Grid>
@ -798,7 +768,7 @@ export const Minting = ({ setIsOpenMinting, myAddress, show }) => {
{t('group:message.generic.next_level', {
postProcess: 'capitalizeFirstChar',
})}{' '}
{_levelUpBlocks()}
{levelUpBlocks(accountInfo, nodeStatus)}
</Typography>
<Typography>

View File

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