Skip to content

Commit

Permalink
Merge pull request #64 from InvArch/yaki-tnkr-sub-refresh-fix-020624
Browse files Browse the repository at this point in the history
fix subscription refresh on tinkernet
  • Loading branch information
shibatales authored Feb 6, 2024
2 parents a002a7e + b2eadf4 commit 96d6425
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions src/routes/staking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import OnOffSwitch from "../components/Switch";
import { autoRestake } from "../utils/autoRestake";
import { restakeClaim } from "../utils/restakeClaim";
import { Balance } from "@polkadot/types/interfaces";
import { useBalance } from "../providers/balance";

export type UnsubFunction = () => Promise<void>;

Expand Down Expand Up @@ -159,6 +160,7 @@ export function getCoreInfo(coreEraStakeInfo: (CoreEraStakeInfoType)[], core: St
const Staking = () => {
const initialUnclaimed = useRef<BigNumber | null>(null);
const api = useApi();
const { reloadAccountInfo } = useBalance();
const setOpenModal = useModal((state) => state.setOpenModal);
const selectedAccount = useAccount((state) => state.selectedAccount);
const [isLoading, setLoading] = useState(true);
Expand Down Expand Up @@ -462,17 +464,18 @@ const Staking = () => {
return;
}

if (initialUnclaimed.current !== null) {
if (initialUnclaimed.current !== null && initialUnclaimed.current > BigNumber(0)) {
setTotalClaimed(prevTotalClaimed => prevTotalClaimed.plus(initialUnclaimed.current || new BigNumber(0)));
}

setTotalUnclaimed(new BigNumber(0));
setUnclaimedEras({ cores: [], total: 0 });
setClaimAllSuccess(true);
refreshQuery();
reloadAccountInfo();
}
});
}, [api, currentStakingEra, enableAutoRestake, selectedAccount, unclaimedEras, userStakedInfoMap, handleRestakingLogic, disableClaiming, refreshQuery]);
}, [api, currentStakingEra, enableAutoRestake, selectedAccount, unclaimedEras, userStakedInfoMap, handleRestakingLogic, disableClaiming, refreshQuery, reloadAccountInfo]);

useEffect(() => {
// Load auto-restake value from local storage
Expand Down Expand Up @@ -508,39 +511,17 @@ const Staking = () => {
useEffect(() => {
if (rewardsUserClaimedQuery.fetching || !selectedAccount) return;

if (!rewardsUserClaimedQuery.data?.stakers?.length) {
setTotalClaimed(new BigNumber(0));
setTotalUnclaimed(new BigNumber(0));
// setUnclaimedEras({ cores: [], total: 0 });
return;
}

const rewardsClaimed = new BigNumber(
rewardsUserClaimedQuery.data.stakers[0].totalRewards
);
setTotalClaimed(rewardsClaimed);

const totalUnclaimed = new BigNumber(
rewardsUserClaimedQuery.data.stakers[0].totalUnclaimed
);
setTotalUnclaimed(claimAllSuccess ? new BigNumber(0) : totalUnclaimed);

if (initialUnclaimed.current === null) {
initialUnclaimed.current = totalUnclaimed;
}
}, [selectedAccount, rewardsUserClaimedQuery.fetching, rewardsUserClaimedQuery.data, claimAllSuccess, refreshQuery]);

// useEffect(() => {
// if (rewardsCoreClaimedQuery.fetching || !rewardsCoreClaimedQuery.data?.cores?.length || !selectedAccount) return;

// const coreEraStakeInfoMap: CoreEraStakeInfoType[] = rewardsCoreClaimedQuery.data.cores;

// const uniqueCoreEraStakeInfo = coreEraStakeInfoMap.filter((core, index, self) =>
// index === self.findIndex((item) => item.coreId === core.coreId)
// );

// setCoreEraStakeInfo(uniqueCoreEraStakeInfo);
// }, [selectedAccount, stakingCores, rewardsCoreClaimedQuery.fetching, rewardsCoreClaimedQuery.data]);
setTotalClaimed((prev) => claimAllSuccess ? prev.plus(totalUnclaimed) : rewardsClaimed);
setTotalUnclaimed(claimAllSuccess ? new BigNumber(0) : totalUnclaimed);
}, [selectedAccount, rewardsUserClaimedQuery.fetching, rewardsUserClaimedQuery.data, claimAllSuccess]);

return (
<div className="mx-auto w-full flex max-w-7xl flex-col justify-between p-4 sm:px-6 lg:px-8 mt-14 md:mt-0 gap-3">
Expand Down

0 comments on commit 96d6425

Please sign in to comment.