Skip to content

Commit

Permalink
chore: fix staking dashboard values not loading
Browse files Browse the repository at this point in the history
  • Loading branch information
shibatales committed Dec 7, 2023
1 parent dc77a52 commit b5292e6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 46 deletions.
9 changes: 5 additions & 4 deletions src/components/DashboardCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ import React, { ReactNode } from 'react';

interface DashboardCardProps {
children: ReactNode;
cardTitle: string;
cardTitle: string | ReactNode;
iconSrc?: string;
leading?: string;
}

const DashboardCard = (props: DashboardCardProps) => {
const { children, cardTitle, iconSrc } = props;
const { children, cardTitle, iconSrc, leading } = props;
return (
<div className="min-w-[151px] h-[194px] bg-tinkerDarkGrey rounded-lg p-8 flex flex-col justify-between items-center">
<div className="w-[48px] h-[48px] bg-[#393a3e] rounded-full mx-auto flex items-center justify-center">
{iconSrc && <img src={iconSrc} alt="icon" />}
</div>
<div className="font-bold text-white leading-tight text-md text-center">
<div className={`font-bold text-white leading-tight text-md text-center`}>
{children}
</div>
<div className="font-normal text-tinkerTextGrey text-[12px] text-center leading-none whitespace-nowrap">
<div className={`font-normal text-tinkerTextGrey text-[12px] text-center ${ leading ? leading : 'leading-none' } whitespace-nowrap`}>
{cardTitle}
</div>
</div>
Expand Down
27 changes: 20 additions & 7 deletions src/components/StakingDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,25 @@ const StakingDashboard = (props: StakingDashboardProps) => {
}) || "0 TNKR"}
</DashboardCard>

<DashboardCard cardTitle="Total Supply Staked" iconSrc={AggregateStakedIcon}>
{formatBalance(aggregateStaked.toString(), {
decimals: 12,
withUnit: 'TNKR',
forceUnit: "-",
}) || "0 TNKR"}
<DashboardCard
cardTitle={
<>
{"Staked from Total"}
<br />
{/* {"Circulating Supply"}
<br /> */}
{formatBalance(aggregateStaked.toString(), {
decimals: 12,
withUnit: "TNKR",
forceUnit: "-",
}) || "0"}

</>
}
iconSrc={AggregateStakedIcon}
leading="leading-tight"
>
{totalStaked && totalStaked.toNumber() > 0 && aggregateStaked && aggregateStaked.toNumber() > 0 ? totalStaked.dividedBy(aggregateStaked).times(100).toFixed(2) : 0}%
</DashboardCard>

<DashboardCard cardTitle="Staking APY" iconSrc={StakingApyIcon}>
Expand Down Expand Up @@ -106,7 +119,7 @@ const StakingDashboard = (props: StakingDashboardProps) => {
{currentStakingEra}
</DashboardCard>

<DashboardCard cardTitle="% Til Next Redeem" iconSrc={CompletionRateIcon}>
<DashboardCard cardTitle="% Til Next Era" iconSrc={CompletionRateIcon}>
{(
((currentBlock - (nextEraBlock - blocksPerEra)) /
(nextEraBlock - (nextEraBlock - blocksPerEra))) *
Expand Down
16 changes: 1 addition & 15 deletions src/modals/ManageStaking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ const ManageStaking = ({ isOpen }: { isOpen: boolean; }) => {
<Tab.Group>
{metadata?.totalUserStaked &&
metadata?.totalUserStaked.toString() !== "0" ? (
<Tab.List className="flex gap-6 space-x-1 rounded-md bg-neutral-900">
<Tab.List className="flex gap-3 space-x-1 rounded-md bg-neutral-900">
<Tab
key={mode.STAKE}
className={({ selected }) =>
Expand Down Expand Up @@ -370,13 +370,6 @@ const ManageStaking = ({ isOpen }: { isOpen: boolean; }) => {
/>

<div className="absolute inset-y-0 right-0 flex items-center gap-4 pr-3">
<span
className="pointer-events-none block text-white text-xs"
id="currency"
>
TNKR
</span>

<span
className="block cursor-pointer text-white text-xs"
id="currency"
Expand Down Expand Up @@ -430,13 +423,6 @@ const ManageStaking = ({ isOpen }: { isOpen: boolean; }) => {
/>

<div className="absolute inset-y-0 right-0 flex items-center gap-4 pr-3">
<span
className="pointer-events-none block text-white text-xs"
id="currency"
>
TNKR
</span>

<span
className="block cursor-pointer text-white text-xs"
id="currency"
Expand Down
24 changes: 4 additions & 20 deletions src/routes/staking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export type CorePrimitiveType = {
const Staking = () => {
const api = useApi();
const descriptionRef = useRef<HTMLDivElement | null>(null);
const mountedRef = useRef(false);
const setOpenModal = useModal((state) => state.setOpenModal);
const selectedAccount = useAccount((state) => state.selectedAccount);
const [hasUnbondedTokens, setHasUnbondedTokens] = useState(false);
Expand Down Expand Up @@ -238,7 +237,6 @@ const Staking = () => {

api.query.ocifStaking.ledger(selectedAccount.address, (c: Codec) => {
const ledger = c.toPrimitive() as LedgerType;

setHasUnbondedTokens(ledger.unbondingInfo.unlockingChunks.length > 0);
});

Expand All @@ -247,15 +245,12 @@ const Staking = () => {
selectedAccount.address,
(generalStakerInfo: Codec) => {
const info = generalStakerInfo.toPrimitive() as StakesInfo;

if (info.stakes.length > 0) {
const unclaimedEarliest = info.stakes.reduce((p, v) => p.era < v.era ? p : v).era;

if (parseInt(unclaimedEarliest) < currentStakingEra) {
const unclaimed = unclaimedEras;

const cores = unclaimed.cores.filter((value) => {
return value.coreId != stakingCore.key;
return value.coreId === stakingCore.key;
});

cores.push({
Expand All @@ -264,14 +259,11 @@ const Staking = () => {
});

let total = unclaimed.total;

if (currentStakingEra - parseInt(unclaimedEarliest) > total) {
total = currentStakingEra - parseInt(unclaimedEarliest);
}
total = currentStakingEra - parseInt(unclaimedEarliest);

setUnclaimedEras({
cores,
total: unclaimed.total,
total,
});
} else {
setUnclaimedEras((unclaimedEras) => ({
Expand Down Expand Up @@ -351,6 +343,7 @@ const Staking = () => {

for (const stakingCore of stakingCores) {
const coreEraStake = (await api.query.ocifStaking.coreEraStake(stakingCore.key, currentStakingEra)).toPrimitive() as CoreEraStakeType;
console.log('coreEraStake', coreEraStake);
coreEraStakeInfo.push({ coreId: stakingCore.key, account: stakingCore.account, ...coreEraStake });
}

Expand Down Expand Up @@ -512,14 +505,6 @@ const Staking = () => {
);
}

useEffect(() => {
mountedRef.current = true;

return () => {
mountedRef.current = false;
};
}, []);

useSubscription(
{
query: TotalRewardsClaimedSubscription,
Expand All @@ -534,7 +519,6 @@ const Staking = () => {
_: unknown,
result: { stakers: { latestClaimBlock: number; totalRewards: string; }[]; }
) => {
if (!mountedRef.current) return;
if (result.stakers.length === 0) return;

if (!result.stakers[0].totalRewards) return;
Expand Down

0 comments on commit b5292e6

Please sign in to comment.