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

hotfix: rewards #631

Merged
merged 1 commit into from
Jan 10, 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
83 changes: 78 additions & 5 deletions src/pages/account/Rewards/Rewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,28 @@ import { TokenAmount } from '@components/TokenAmount';
import { Spinner, Typography } from '@subql/components';
import { TableTitle } from '@subql/components';
import { GetEraRewardsByIndexerAndPageQuery } from '@subql/network-query';
import { renderAsync, useGetEraRewardsByIndexerAndPageLazyQuery, useGetRewardsQuery } from '@subql/react-hooks';
import {
renderAsync,
useGetEraRewardsByIndexerAndPageLazyQuery,
useGetFilteredDelegationsQuery,
useGetRewardsQuery,
} from '@subql/react-hooks';
import { ExcludeNull, formatEther, notEmpty } from '@utils';
import { useMount, useUpdate } from 'ahooks';
import { Table, TableProps, Tag, Tooltip } from 'antd';
import dayjs from 'dayjs';
import { BigNumber } from 'ethers';
import { useAccount } from 'wagmi';

import { useWeb3Store } from 'src/stores';

import { ClaimRewards } from './ClaimRewards';
import styles from './Rewards.module.css';

export const Rewards: React.FC = () => {
const { address: account } = useAccount();

const { contracts } = useWeb3Store();
const update = useUpdate();
const filterParams = { address: account || '' };
const rewards = useGetRewardsQuery({ variables: filterParams, fetchPolicy: 'network-only' });
Expand Down Expand Up @@ -110,6 +118,64 @@ export const Rewards: React.FC = () => {
update();
};

// this is a hotfix for unclaimed rewards.
const [unClaimedRewardsFromContracts, setUnClaimedRewardsFromContracts] = React.useState<{
indexers: string[];
amount: BigNumber;
}>({
indexers: [],
amount: BigNumber.from(0),
});
const delegations = useGetFilteredDelegationsQuery({
variables: { delegator: account ?? '', filterIndexer: account ?? '', offset: 0 },
});
const indexerIds = React.useMemo(() => {
return delegations.data?.delegations?.nodes
.map((i) => i?.indexerId)
.filter((i) => !unclaimedRewards?.indexers.includes(i));
}, [delegations.data?.delegations, unclaimedRewards]);
const getUnclaimedRewards = async () => {
if (!account || !contracts || !indexerIds?.length) return;

const inner = async (indexerId: string | undefined) => {
if (!indexerId) return true;
const res = await contracts?.rewardsDistributor.userRewards(indexerId, account);
return [res, indexerId];
};

const res = await Promise.allSettled(indexerIds.map((indexer) => inner(indexer)));

const unClaimedRewardsIndexers = res
.map((i) => {
if (i.status === 'fulfilled') {
const [amount, indexerAddress] = i.value as [BigNumber, string];

if (!amount.eq(0)) return indexerAddress;
}

return undefined;
})
.filter((i) => i);
const unClaimedRewardsAmount = res.reduce((cur, add) => {
if (add.status === 'fulfilled') {
const [amount] = add.value as [BigNumber, string];

return cur.add(amount);
}
return cur;
}, BigNumber.from(0));

setUnClaimedRewardsFromContracts({
indexers: unClaimedRewardsIndexers as string[],
amount: unClaimedRewardsAmount,
});
};

React.useEffect(() => {
getUnclaimedRewards();
}, [indexerIds, account, unclaimedRewards]);
// the above codes is a hotfix

React.useEffect(() => {
if (!account) return;
queryParams.current.offset = 0;
Expand All @@ -135,12 +201,19 @@ export const Rewards: React.FC = () => {
<InfoCircleOutlined style={{ fontSize: 14, color: '#3AA0FF', marginRight: 8 }} />
<Typography type="secondary">{t('rewards.info')}</Typography>
<span style={{ flex: 1 }}></span>
{totalUnclaimedRewards > 0 && unclaimedRewards?.indexers && (
{(totalUnclaimedRewards > 0 && unclaimedRewards?.indexers) ||
unClaimedRewardsFromContracts.indexers.length ? (
<ClaimRewards
indexers={unclaimedRewards?.indexers as string[]}
indexers={
(unclaimedRewards?.indexers || []).concat(unClaimedRewardsFromContracts.indexers) as string[]
}
account={account ?? ''}
totalUnclaimed={formatEther(unclaimedRewards?.totalAmount)}
totalUnclaimed={formatEther(
unclaimedRewards?.totalAmount.add(unClaimedRewardsFromContracts.amount),
)}
/>
) : (
''
)}
</div>
<div className={styles.claim}>
Expand All @@ -153,7 +226,7 @@ export const Rewards: React.FC = () => {
columns={columns}
dataSource={filterEmptyData || []}
scroll={{ x: 600 }}
rowKey="id"
rowKey={(record) => `${record.eraId}${record.delegatorId}${record.indexerId}`}
pagination={{
current: Math.floor(queryParams.current.offset / queryParams.current.pageSize) + 1,
pageSize: queryParams.current.pageSize,
Expand Down
Loading