mirror of
https://github.com/pezkuwichain/pwap.git
synced 2026-04-22 09:07:55 +00:00
feat: add frontend fallback for staking and trust scores
Until runtime upgrade is deployed, calculate scores on frontend: shared/lib/scores.ts: - getFrontendStakingScore: Read stake from Relay Chain, track in localStorage - getFrontendTrustScore: Calculate using pallet formula - getAllScoresWithFallback: Combined score fetching with fallback Formula (matching pezpallet-trust): - weighted_sum = staking*100 + referral*300 + perwerde*300 + tiki*300 - trust_score = (staking * weighted_sum) / 100 Components updated: - AccountBalance.tsx: Use getAllScoresWithFallback - HeroSection.tsx: Use getTrustScoreWithFallback
This commit is contained in:
@@ -8,7 +8,7 @@ import { AddTokenModal } from './AddTokenModal';
|
||||
import { TransferModal } from './TransferModal';
|
||||
import { XCMTeleportModal } from './XCMTeleportModal';
|
||||
import { LPStakeModal } from './LPStakeModal';
|
||||
import { getAllScores, type UserScores } from '@pezkuwi/lib/scores';
|
||||
import { getAllScoresWithFallback, type UserScores } from '@pezkuwi/lib/scores';
|
||||
|
||||
interface TokenBalance {
|
||||
assetId: number;
|
||||
@@ -554,7 +554,7 @@ export const AccountBalance: React.FC = () => {
|
||||
fetchBalance();
|
||||
fetchTokenPrices(); // Fetch token USD prices from pools
|
||||
|
||||
// Fetch All Scores from blockchain
|
||||
// Fetch All Scores from blockchain with frontend fallback
|
||||
const fetchAllScores = async () => {
|
||||
if (!api || !isApiReady || !selectedAccount?.address) {
|
||||
setScores({
|
||||
@@ -569,7 +569,12 @@ export const AccountBalance: React.FC = () => {
|
||||
|
||||
setLoadingScores(true);
|
||||
try {
|
||||
const userScores = await getAllScores(api, selectedAccount.address);
|
||||
// Use fallback function: peopleApi for on-chain scores, api (Relay) for staking data
|
||||
const userScores = await getAllScoresWithFallback(
|
||||
peopleApi || null, // People Chain for referral, tiki, perwerde
|
||||
api, // Relay Chain for staking data
|
||||
selectedAccount.address
|
||||
);
|
||||
setScores(userScores);
|
||||
} catch (err) {
|
||||
if (import.meta.env.DEV) console.error('Failed to fetch scores:', err);
|
||||
|
||||
@@ -4,10 +4,11 @@ import { ChevronRight, Shield } from 'lucide-react';
|
||||
import { usePezkuwi } from '../contexts/PezkuwiContext';
|
||||
import { useWallet } from '../contexts/WalletContext'; // Import useWallet
|
||||
import { formatBalance } from '@pezkuwi/lib/wallet';
|
||||
import { getTrustScoreWithFallback } from '@pezkuwi/lib/scores';
|
||||
|
||||
const HeroSection: React.FC = () => {
|
||||
const { t } = useTranslation();
|
||||
const { api, isApiReady } = usePezkuwi();
|
||||
const { api, isApiReady, peopleApi } = usePezkuwi();
|
||||
const { selectedAccount } = useWallet(); // Use selectedAccount from WalletContext
|
||||
const [stats, setStats] = useState({
|
||||
activeProposals: 0,
|
||||
@@ -23,11 +24,13 @@ const HeroSection: React.FC = () => {
|
||||
let currentTrustScore = 0; // Default if not fetched or no account
|
||||
if (selectedAccount?.address) {
|
||||
try {
|
||||
// Assuming pallet-staking-score has a storage item for trust scores
|
||||
// The exact query might need adjustment based on chain metadata
|
||||
const rawTrustScore = await api.query.stakingScore.trustScore(selectedAccount.address);
|
||||
// Assuming trustScore is a simple number or a wrapper around it
|
||||
currentTrustScore = rawTrustScore.isSome ? rawTrustScore.unwrap().toNumber() : 0;
|
||||
// Use frontend fallback for trust score
|
||||
const trustResult = await getTrustScoreWithFallback(
|
||||
peopleApi || null,
|
||||
api,
|
||||
selectedAccount.address
|
||||
);
|
||||
currentTrustScore = trustResult.trustScore;
|
||||
} catch (err) {
|
||||
if (import.meta.env.DEV) console.warn('Failed to fetch trust score:', err);
|
||||
currentTrustScore = 0;
|
||||
@@ -91,7 +94,7 @@ const HeroSection: React.FC = () => {
|
||||
};
|
||||
|
||||
fetchStats();
|
||||
}, [api, isApiReady, selectedAccount]); // Add selectedAccount to dependencies
|
||||
}, [api, isApiReady, peopleApi, selectedAccount]); // Add peopleApi to dependencies
|
||||
|
||||
return (
|
||||
<section className="relative min-h-screen flex items-center justify-start overflow-hidden bg-gray-950">
|
||||
|
||||
Reference in New Issue
Block a user