Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add useBalance hook to Staking component #64

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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