mirror of
https://github.com/pezkuwichain/pezkuwi-telegram-miniapp.git
synced 2026-04-21 23:37:55 +00:00
feat: migrate staking to Asset Hub and add citizen count card
- HEZStakingModal: switch all staking queries/tx from RC api to assetHubApi - Add citizen count card to Rewards overview (Hejmara Kurd Le Cihane) - Add getCitizenCount() to fetch total citizens from People Chain - Add translations for citizen count card (6 languages)
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "pezkuwi-telegram-miniapp",
|
||||
"version": "1.0.213",
|
||||
"version": "1.0.214",
|
||||
"type": "module",
|
||||
"description": "Pezkuwichain Telegram Mini App - Forum, Announcements, Rewards",
|
||||
"author": "Pezkuwichain Team",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/**
|
||||
* HEZ Staking Modal for Telegram Mini App
|
||||
* Allows users to stake HEZ on Relay Chain for Trust Score
|
||||
* Allows users to stake HEZ on Asset Hub for Trust Score
|
||||
* (Staking moved from Relay Chain to Asset Hub)
|
||||
*/
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
@@ -34,7 +35,7 @@ interface HEZStakingModalProps {
|
||||
const UNITS = 1_000_000_000_000; // 10^12
|
||||
|
||||
export function HEZStakingModal({ isOpen, onClose }: HEZStakingModalProps) {
|
||||
const { api, keypair, address, balance } = useWallet();
|
||||
const { assetHubApi, keypair, address } = useWallet();
|
||||
const { hapticImpact, hapticNotification, showAlert } = useTelegram();
|
||||
const { t } = useTranslation();
|
||||
|
||||
@@ -47,21 +48,33 @@ export function HEZStakingModal({ isOpen, onClose }: HEZStakingModalProps) {
|
||||
const [bondAmount, setBondAmount] = useState('');
|
||||
const [unbondAmount, setUnbondAmount] = useState('');
|
||||
const [selectedValidators, setSelectedValidators] = useState<string[]>([]);
|
||||
const [ahBalance, setAhBalance] = useState<string | null>(null);
|
||||
|
||||
// Fetch staking info
|
||||
// Fetch staking info from Asset Hub
|
||||
const fetchStakingInfo = useCallback(async () => {
|
||||
if (!api || !address) return;
|
||||
if (!assetHubApi || !address) return;
|
||||
|
||||
setIsLoading(true);
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const stakingPallet = api.query.staking as any;
|
||||
const stakingPallet = assetHubApi.query.staking as any;
|
||||
if (!stakingPallet) {
|
||||
setError(t('staking.palletNotFound'));
|
||||
setIsLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch AH native HEZ balance
|
||||
try {
|
||||
const accountInfo = await assetHubApi.query.system.account(address);
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const free = (accountInfo as any).data.free.toString();
|
||||
const balanceNum = Number(free) / UNITS;
|
||||
setAhBalance(balanceNum.toFixed(4));
|
||||
} catch {
|
||||
setAhBalance(null);
|
||||
}
|
||||
|
||||
// Check if user has bonded
|
||||
const ledger = await stakingPallet.ledger(address);
|
||||
|
||||
@@ -93,7 +106,7 @@ export function HEZStakingModal({ isOpen, onClose }: HEZStakingModalProps) {
|
||||
setStakingInfo(null);
|
||||
}
|
||||
|
||||
// Fetch validators
|
||||
// Fetch validators from AH staking pallet
|
||||
const validatorEntries = await stakingPallet.validators.entries();
|
||||
const validatorList: ValidatorInfo[] = validatorEntries
|
||||
.slice(0, 20) // Limit to 20 validators
|
||||
@@ -120,13 +133,13 @@ export function HEZStakingModal({ isOpen, onClose }: HEZStakingModalProps) {
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [api, address]);
|
||||
}, [assetHubApi, address]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isOpen && api && address) {
|
||||
if (isOpen && assetHubApi && address) {
|
||||
fetchStakingInfo();
|
||||
}
|
||||
}, [isOpen, api, address, fetchStakingInfo]);
|
||||
}, [isOpen, assetHubApi, address, fetchStakingInfo]);
|
||||
|
||||
const formatHEZ = (amount: bigint): string => {
|
||||
const hez = Number(amount) / UNITS;
|
||||
@@ -134,7 +147,7 @@ export function HEZStakingModal({ isOpen, onClose }: HEZStakingModalProps) {
|
||||
};
|
||||
|
||||
const handleBond = async () => {
|
||||
if (!api || !keypair || !bondAmount) return;
|
||||
if (!assetHubApi || !keypair || !bondAmount) return;
|
||||
|
||||
setIsProcessing(true);
|
||||
setError(null);
|
||||
@@ -143,7 +156,7 @@ export function HEZStakingModal({ isOpen, onClose }: HEZStakingModalProps) {
|
||||
const amountBN = BigInt(Math.floor(parseFloat(bondAmount) * UNITS));
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const stakingPallet = api.tx.staking as any;
|
||||
const stakingPallet = assetHubApi.tx.staking as any;
|
||||
|
||||
let tx;
|
||||
if (stakingInfo) {
|
||||
@@ -169,7 +182,7 @@ export function HEZStakingModal({ isOpen, onClose }: HEZStakingModalProps) {
|
||||
if (dispatchError) {
|
||||
let errorMsg = t('staking.bondFailed');
|
||||
if (dispatchError.isModule) {
|
||||
const decoded = api.registry.findMetaError(dispatchError.asModule);
|
||||
const decoded = assetHubApi.registry.findMetaError(dispatchError.asModule);
|
||||
errorMsg = `${decoded.section}.${decoded.name}`;
|
||||
}
|
||||
reject(new Error(errorMsg));
|
||||
@@ -196,14 +209,14 @@ export function HEZStakingModal({ isOpen, onClose }: HEZStakingModalProps) {
|
||||
};
|
||||
|
||||
const handleNominate = async () => {
|
||||
if (!api || !keypair || selectedValidators.length === 0) return;
|
||||
if (!assetHubApi || !keypair || selectedValidators.length === 0) return;
|
||||
|
||||
setIsProcessing(true);
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const tx = (api.tx.staking as any).nominate(selectedValidators);
|
||||
const tx = (assetHubApi.tx.staking as any).nominate(selectedValidators);
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
tx.signAndSend(
|
||||
@@ -220,7 +233,7 @@ export function HEZStakingModal({ isOpen, onClose }: HEZStakingModalProps) {
|
||||
if (dispatchError) {
|
||||
let errorMsg = t('staking.nominateFailed');
|
||||
if (dispatchError.isModule) {
|
||||
const decoded = api.registry.findMetaError(dispatchError.asModule);
|
||||
const decoded = assetHubApi.registry.findMetaError(dispatchError.asModule);
|
||||
errorMsg = `${decoded.section}.${decoded.name}`;
|
||||
}
|
||||
reject(new Error(errorMsg));
|
||||
@@ -246,7 +259,7 @@ export function HEZStakingModal({ isOpen, onClose }: HEZStakingModalProps) {
|
||||
};
|
||||
|
||||
const handleUnbond = async () => {
|
||||
if (!api || !keypair || !unbondAmount) return;
|
||||
if (!assetHubApi || !keypair || !unbondAmount) return;
|
||||
|
||||
setIsProcessing(true);
|
||||
setError(null);
|
||||
@@ -255,7 +268,7 @@ export function HEZStakingModal({ isOpen, onClose }: HEZStakingModalProps) {
|
||||
const amountBN = BigInt(Math.floor(parseFloat(unbondAmount) * UNITS));
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const tx = (api.tx.staking as any).unbond(amountBN.toString());
|
||||
const tx = (assetHubApi.tx.staking as any).unbond(amountBN.toString());
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
tx.signAndSend(
|
||||
@@ -272,7 +285,7 @@ export function HEZStakingModal({ isOpen, onClose }: HEZStakingModalProps) {
|
||||
if (dispatchError) {
|
||||
let errorMsg = t('staking.unbondFailed');
|
||||
if (dispatchError.isModule) {
|
||||
const decoded = api.registry.findMetaError(dispatchError.asModule);
|
||||
const decoded = assetHubApi.registry.findMetaError(dispatchError.asModule);
|
||||
errorMsg = `${decoded.section}.${decoded.name}`;
|
||||
}
|
||||
reject(new Error(errorMsg));
|
||||
@@ -448,7 +461,7 @@ export function HEZStakingModal({ isOpen, onClose }: HEZStakingModalProps) {
|
||||
<div className="bg-secondary/50 rounded-xl p-4">
|
||||
<div className="flex justify-between text-sm mb-2">
|
||||
<span className="text-muted-foreground">{t('staking.yourBalance')}</span>
|
||||
<span>{balance || '0'} HEZ</span>
|
||||
<span>{ahBalance || '0'} HEZ</span>
|
||||
</div>
|
||||
{stakingInfo && (
|
||||
<div className="flex justify-between text-sm">
|
||||
@@ -473,7 +486,7 @@ export function HEZStakingModal({ isOpen, onClose }: HEZStakingModalProps) {
|
||||
className="w-full bg-secondary border border-border rounded-xl p-4 pr-20 text-lg"
|
||||
/>
|
||||
<button
|
||||
onClick={() => setBondAmount(balance || '0')}
|
||||
onClick={() => setBondAmount(ahBalance || '0')}
|
||||
className="absolute right-3 top-1/2 -translate-y-1/2 text-xs text-primary"
|
||||
>
|
||||
MAX
|
||||
|
||||
@@ -144,6 +144,9 @@ const ar: Translations = {
|
||||
recordingTrustScore: 'جاري تسجيل نقاط الثقة...',
|
||||
trustScoreRecorded: 'تم تسجيل نقاط الثقة!',
|
||||
trustScoreRecordFailed: 'فشل تسجيل النقاط',
|
||||
citizenCountTitle: 'الأكراد في العالم',
|
||||
citizenCountDesc: 'المواطنون المسجلون على PezkuwiChain',
|
||||
beCitizen: 'كن مواطناً',
|
||||
},
|
||||
|
||||
wallet: {
|
||||
|
||||
@@ -145,6 +145,9 @@ const ckb: Translations = {
|
||||
recordingTrustScore: 'خاڵ تۆمار دەکرێت...',
|
||||
trustScoreRecorded: 'خاڵی متمانە تۆمار کرا!',
|
||||
trustScoreRecordFailed: 'تۆمارکردنی خاڵ سەرنەکەوت',
|
||||
citizenCountTitle: 'ژمارەی کورد لە جیهان',
|
||||
citizenCountDesc: 'هاوڵاتییانی تۆمارکراو لە PezkuwiChain',
|
||||
beCitizen: 'ببە هاوڵاتی',
|
||||
},
|
||||
|
||||
wallet: {
|
||||
|
||||
@@ -144,6 +144,9 @@ const en: Translations = {
|
||||
recordingTrustScore: 'Recording trust score...',
|
||||
trustScoreRecorded: 'Trust score recorded!',
|
||||
trustScoreRecordFailed: 'Failed to record score',
|
||||
citizenCountTitle: 'Kurds in the World',
|
||||
citizenCountDesc: 'Citizens registered on PezkuwiChain',
|
||||
beCitizen: 'Be Citizen',
|
||||
},
|
||||
|
||||
wallet: {
|
||||
|
||||
@@ -144,6 +144,9 @@ const fa: Translations = {
|
||||
recordingTrustScore: 'در حال ثبت امتیاز اعتماد...',
|
||||
trustScoreRecorded: 'امتیاز اعتماد ثبت شد!',
|
||||
trustScoreRecordFailed: 'ثبت امتیاز ناموفق بود',
|
||||
citizenCountTitle: 'کوردها در جهان',
|
||||
citizenCountDesc: 'شهروندان ثبتشده در PezkuwiChain',
|
||||
beCitizen: 'شهروند شوید',
|
||||
},
|
||||
|
||||
wallet: {
|
||||
|
||||
@@ -149,6 +149,9 @@ const krd: Translations = {
|
||||
recordingTrustScore: 'Pûan tê tomarkirin...',
|
||||
trustScoreRecorded: 'Pûana pêbaweriyê hat tomarkirin!',
|
||||
trustScoreRecordFailed: 'Tomarkirina pûanê biserneket',
|
||||
citizenCountTitle: 'Hejmara Kurd Le Cîhanê',
|
||||
citizenCountDesc: 'Welatiyên ku li ser PezkuwiChain qeyd bûne',
|
||||
beCitizen: 'Bibe Welatî',
|
||||
},
|
||||
|
||||
wallet: {
|
||||
|
||||
@@ -144,6 +144,9 @@ const tr: Translations = {
|
||||
recordingTrustScore: 'Güven puanı kaydediliyor...',
|
||||
trustScoreRecorded: 'Güven puanı kaydedildi!',
|
||||
trustScoreRecordFailed: 'Puan kaydedilemedi',
|
||||
citizenCountTitle: 'Dünyadaki Kürt Sayısı',
|
||||
citizenCountDesc: "PezkuwiChain'de kayıtlı vatandaşlar",
|
||||
beCitizen: 'Vatandaş Ol',
|
||||
},
|
||||
|
||||
wallet: {
|
||||
|
||||
@@ -146,6 +146,9 @@ export interface Translations {
|
||||
recordingTrustScore: string;
|
||||
trustScoreRecorded: string;
|
||||
trustScoreRecordFailed: string;
|
||||
citizenCountTitle: string;
|
||||
citizenCountDesc: string;
|
||||
beCitizen: string;
|
||||
};
|
||||
|
||||
// Wallet section
|
||||
|
||||
@@ -91,6 +91,27 @@ export async function uploadToIPFS(_data: CitizenshipData): Promise<string> {
|
||||
return mockCID;
|
||||
}
|
||||
|
||||
// ── Citizen Count ───────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* Get total number of citizens by counting citizenNft entries on People Chain.
|
||||
* Citizen NFTs are in collection 42.
|
||||
*/
|
||||
export async function getCitizenCount(api: ApiPromise): Promise<number> {
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
if (!(api?.query as any)?.tiki?.citizenNft) {
|
||||
return 0;
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const entries = await (api.query as any).tiki.citizenNft.entries();
|
||||
return entries.length;
|
||||
} catch (error) {
|
||||
console.error('[Citizenship] Error fetching citizen count:', error);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// ── Citizenship Status ──────────────────────────────────────────────
|
||||
|
||||
export async function getCitizenshipStatus(
|
||||
|
||||
@@ -23,6 +23,7 @@ import {
|
||||
GraduationCap,
|
||||
Play,
|
||||
PenTool,
|
||||
Globe2,
|
||||
} from 'lucide-react';
|
||||
import { cn, formatAddress } from '@/lib/utils';
|
||||
import { useTelegram } from '@/hooks/useTelegram';
|
||||
@@ -50,6 +51,7 @@ import {
|
||||
} from '@/lib/subquery';
|
||||
import {
|
||||
getCitizenshipStatus,
|
||||
getCitizenCount,
|
||||
confirmCitizenship,
|
||||
type CitizenshipStatus,
|
||||
} from '@/lib/citizenship';
|
||||
@@ -75,6 +77,7 @@ export function RewardsSection() {
|
||||
const [stakingRewards, setStakingRewards] = useState<StakingRewardsResult | null>(null);
|
||||
const [scoresLoading, setScoresLoading] = useState(false);
|
||||
const [citizenshipStatus, setCitizenshipStatus] = useState<CitizenshipStatus>('NotStarted');
|
||||
const [citizenCount, setCitizenCount] = useState<number | null>(null);
|
||||
const [showConfirmAnimation, setShowConfirmAnimation] = useState(false);
|
||||
const [showTrackingAnimation, setShowTrackingAnimation] = useState(false);
|
||||
const [trackingAnimationText, setTrackingAnimationText] = useState('');
|
||||
@@ -146,10 +149,13 @@ export function RewardsSection() {
|
||||
}
|
||||
}, [activeTab, address, fetchUserScores]);
|
||||
|
||||
// Fetch citizenship status
|
||||
// Fetch citizenship status and citizen count
|
||||
useEffect(() => {
|
||||
if (!peopleApi || !address) return;
|
||||
getCitizenshipStatus(peopleApi, address).then(setCitizenshipStatus);
|
||||
if (!peopleApi) return;
|
||||
if (address) {
|
||||
getCitizenshipStatus(peopleApi, address).then(setCitizenshipStatus);
|
||||
}
|
||||
getCitizenCount(peopleApi).then(setCitizenCount);
|
||||
}, [peopleApi, address]);
|
||||
|
||||
const handleConfirmCitizenship = async () => {
|
||||
@@ -335,6 +341,36 @@ export function RewardsSection() {
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
{/* Citizen Count Card */}
|
||||
<div className="bg-gradient-to-br from-emerald-600 to-teal-700 rounded-2xl p-4 text-white">
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<div>
|
||||
<p className="text-emerald-100 text-sm font-medium">
|
||||
{t('rewards.citizenCountTitle')}
|
||||
</p>
|
||||
<p className="text-4xl font-bold mt-1">
|
||||
{citizenCount !== null ? citizenCount.toLocaleString() : '...'}
|
||||
</p>
|
||||
</div>
|
||||
<div className="w-16 h-16 rounded-full bg-white/20 flex items-center justify-center">
|
||||
<KurdistanSun size={40} />
|
||||
</div>
|
||||
</div>
|
||||
<p className="text-emerald-200/80 text-xs mb-3">
|
||||
{t('rewards.citizenCountDesc')}
|
||||
</p>
|
||||
<button
|
||||
onClick={() => {
|
||||
hapticImpact('medium');
|
||||
window.location.href = `${window.location.origin}/citizens${window.location.hash}`;
|
||||
}}
|
||||
className="w-full py-2.5 bg-white/20 hover:bg-white/30 rounded-xl text-sm font-medium flex items-center justify-center gap-2 transition-colors"
|
||||
>
|
||||
<Globe2 className="w-4 h-4" />
|
||||
{t('rewards.beCitizen')}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Score Card */}
|
||||
<div className="bg-gradient-to-br from-purple-600 to-pink-600 rounded-2xl p-4 text-white">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
|
||||
+3
-3
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"version": "1.0.213",
|
||||
"buildTime": "2026-02-19T23:27:53.391Z",
|
||||
"buildNumber": 1771543673392
|
||||
"version": "1.0.214",
|
||||
"buildTime": "2026-02-20T23:55:07.518Z",
|
||||
"buildNumber": 1771631707519
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user