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

SOV-3117: reward claiming #550

Merged
merged 7 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/friendly-laws-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sovryn/ui': patch
---

feat: allow to hide table header row
5 changes: 5 additions & 0 deletions .changeset/nice-crabs-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'frontend': patch
---

feat: claim all rewards with single tx
5 changes: 5 additions & 0 deletions .changeset/smart-llamas-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sovryn/contracts': patch
---

feat: contracts exports ethers contract instance too
158 changes: 105 additions & 53 deletions apps/frontend/src/app/5_pages/RewardsPage/components/Staking/Staking.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { FC, useMemo } from 'react';

import { BigNumber } from 'ethers';
import { formatUnits } from 'ethers/lib/utils';
import { t } from 'i18next';

Expand All @@ -12,11 +13,11 @@ import { getTokenDisplayName } from '../../../../../constants/tokens';
import { useAccount } from '../../../../../hooks/useAccount';
import { translations } from '../../../../../locales/i18n';
import { decimalic } from '../../../../../utils/math';
import { EarnedFee } from '../../RewardsPage.types';
import { useGetFeesEarned } from '../../hooks/useGetFeesEarned';
import { useGetLiquidSovClaimAmount } from '../../hooks/useGetLiquidSovClaimAmount';
import { columns } from './Staking.constants';
import { getStakingRevenueType } from './Staking.utils';
import { WithdrawFee } from './components/WithdrawFee/WithdrawFee';
import { WithdrawAllFees } from './components/WithdrawAllFees/WithdrawAllFees';
import { WithdrawLiquidFee } from './components/WithdrawLiquidFee/WithdrawLiquidFee';

export const Staking: FC = () => {
Expand All @@ -29,65 +30,116 @@ export const Staking: FC = () => {
refetch: refetchLiquidSovClaim,
} = useGetLiquidSovClaimAmount();

const rows = useMemo(() => {
const noRewards =
!earnedFees.some(earnedFee => decimalic(earnedFee.value).gt(0)) &&
!decimalic(liquidSovClaimAmount).gt(0);
const hasEarnedFees = useMemo(
() => earnedFees.some(earnedFee => decimalic(earnedFee.value).gt(0)),
[earnedFees],
);

const hasLiquidSov = useMemo(
() => decimalic(liquidSovClaimAmount).gt(0),
[liquidSovClaimAmount],
);

const noRewards = useMemo(
() => (!hasEarnedFees && !hasLiquidSov) || !account,
[hasEarnedFees, hasLiquidSov, account],
);

const earnedFeesSum = useMemo(() => {
const btcFees = earnedFees.filter(
earnedFee =>
earnedFee.token === SupportedTokens.rbtc ||
earnedFee.token === SupportedTokens.wrbtc,
);

if (!account || loading || noRewards) {
return [];
if (!btcFees.length) {
return earnedFees;
}

return [
...earnedFees.map(earnedFee => ({
type: getStakingRevenueType(earnedFee.token),
amount: (
<AmountRenderer
value={formatUnits(earnedFee.value, 18)}
suffix={getTokenDisplayName(earnedFee.token)}
precision={BTC_RENDER_PRECISION}
dataAttribute={`${earnedFee.token}-rewards-amount`}
/>
),
action: <WithdrawFee {...earnedFee} refetch={refetch} />,
key: `${earnedFee.token}-fee`,
})),
{
type: t(translations.rewardPage.staking.stakingSubsidies),
amount: (
<AmountRenderer
value={formatUnits(liquidSovClaimAmount, 18)}
suffix={getTokenDisplayName(SupportedTokens.sov)}
precision={BTC_RENDER_PRECISION}
dataAttribute={`${SupportedTokens.sov}-liquid-amount`}
/>
),
action: (
<WithdrawLiquidFee
amountToClaim={liquidSovClaimAmount}
lastWithdrawalInterval={lastWithdrawalInterval}
refetch={refetchLiquidSovClaim}
/>
),
key: `${SupportedTokens.sov}-liquid-fee`,
},
];
}, [
account,
earnedFees,
lastWithdrawalInterval,
liquidSovClaimAmount,
loading,
refetch,
refetchLiquidSovClaim,
]);
const otherFees = earnedFees.filter(
earnedFee =>
earnedFee.token !== SupportedTokens.rbtc &&
earnedFee.token !== SupportedTokens.wrbtc,
);

const btcFeesSum: EarnedFee = {
token: SupportedTokens.rbtc,
value: btcFees
.reduce((sum, fee) => sum.add(fee.value), BigNumber.from(0))
.toString(),
contractAddress: '',
rbtcValue: 0,
};

return [btcFeesSum, ...otherFees];
}, [earnedFees]);

const rows = useMemo(
() => [
...(hasEarnedFees
? [
{
type: t(translations.rewardPage.staking.stakingRevenue),
amount: (
<div className="flex flex-col gap-1 my-4 text-left">
{earnedFeesSum.map(fee => (
<AmountRenderer
key={fee.token}
value={formatUnits(fee.value, 18)}
suffix={getTokenDisplayName(fee.token)}
precision={BTC_RENDER_PRECISION}
dataAttribute={`${fee.token}-rewards-amount`}
/>
))}
</div>
),
action: <WithdrawAllFees fees={earnedFees} refetch={refetch} />,
key: `all-fee`,
},
]
: []),
...(hasLiquidSov
? [
{
type: t(translations.rewardPage.staking.stakingSubsidies),
amount: (
<AmountRenderer
value={formatUnits(liquidSovClaimAmount, 18)}
suffix={getTokenDisplayName(SupportedTokens.sov)}
precision={BTC_RENDER_PRECISION}
dataAttribute={`${SupportedTokens.sov}-liquid-amount`}
/>
),
action: (
<WithdrawLiquidFee
amountToClaim={liquidSovClaimAmount}
lastWithdrawalInterval={lastWithdrawalInterval}
refetch={refetchLiquidSovClaim}
/>
),
key: `${SupportedTokens.sov}-liquid-fee`,
},
]
: []),
],
[
hasEarnedFees,
earnedFeesSum,
earnedFees,
refetch,
hasLiquidSov,
liquidSovClaimAmount,
lastWithdrawalInterval,
refetchLiquidSovClaim,
],
);

return (
<div className="flex flex-col items-center w-full">
<div className="flex flex-col items-center w-full gap-y-8">
<div className="lg:bg-gray-80 lg:py-4 lg:px-4 rounded w-full">
<Table
columns={columns}
rows={rows}
rows={noRewards ? [] : rows}
isLoading={!!account ? loading : false}
rowKey={row => row.key}
noData={
Expand Down
Loading