mirror of
https://github.com/pezkuwichain/pwap.git
synced 2026-06-14 04:21:02 +00:00
fix: calculate total staked from ledger entries instead of erasTotalStake
This commit is contained in:
@@ -3,7 +3,6 @@ import { useTranslation } from 'react-i18next';
|
|||||||
import { ChevronRight, Shield } from 'lucide-react';
|
import { ChevronRight, Shield } from 'lucide-react';
|
||||||
import { usePezkuwi } from '../contexts/PezkuwiContext';
|
import { usePezkuwi } from '../contexts/PezkuwiContext';
|
||||||
import { formatBalance } from '@pezkuwi/lib/wallet';
|
import { formatBalance } from '@pezkuwi/lib/wallet';
|
||||||
import { getCurrentEra } from '@pezkuwi/lib/staking';
|
|
||||||
|
|
||||||
const HeroSection: React.FC = () => {
|
const HeroSection: React.FC = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@@ -52,15 +51,21 @@ const HeroSection: React.FC = () => {
|
|||||||
|
|
||||||
let tokensStaked = '0';
|
let tokensStaked = '0';
|
||||||
try {
|
try {
|
||||||
const eraIndex = await getCurrentEra(assetHubApi);
|
// Sum active stakes from all ledger entries
|
||||||
if (eraIndex > 0) {
|
const ledgers = await assetHubApi.query.staking.ledger.entries();
|
||||||
const totalStake = await assetHubApi.query.staking.erasTotalStake(eraIndex);
|
let totalActive = BigInt(0);
|
||||||
const formatted = formatBalance(totalStake.toString());
|
for (const [, ledger] of ledgers) {
|
||||||
const [whole, frac] = formatted.split('.');
|
if (!ledger.isEmpty) {
|
||||||
const formattedWhole = Number(whole).toLocaleString();
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
const formattedFrac = (frac || '00').slice(0, 2);
|
const data = (ledger as any).unwrap?.() ? (ledger as any).unwrap().toJSON() : (ledger as any).toJSON();
|
||||||
tokensStaked = `${formattedWhole}.${formattedFrac} HEZ`;
|
totalActive += BigInt(data?.active ?? data?.total ?? '0');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
const formatted = formatBalance(totalActive.toString());
|
||||||
|
const [whole, frac] = formatted.split('.');
|
||||||
|
const formattedWhole = Number(whole).toLocaleString();
|
||||||
|
const formattedFrac = (frac || '00').slice(0, 2);
|
||||||
|
tokensStaked = `${formattedWhole}.${formattedFrac} HEZ`;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (import.meta.env.DEV) console.warn('Failed to fetch total stake from AH:', err);
|
if (import.meta.env.DEV) console.warn('Failed to fetch total stake from AH:', err);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user