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

TOK-517: pause builder reward claim #431

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 18 additions & 2 deletions src/app/collective-rewards/rewards/builders/ClaimableRewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { withSpinner } from '@/components/LoadingSpinner/withLoadingSpinner'
import { usePricesContext } from '@/shared/context/PricesContext'
import { FC } from 'react'
import { Address } from 'viem'
import { Popover } from '../../../../components/Popover'

type TokenRewardsMetricsProps = {
builder: Address
Expand All @@ -24,6 +25,7 @@ type TokenRewardsMetricsProps = {
}

const TokenRewardsMetrics: FC<TokenRewardsMetricsProps> = ({
builder,
gauge,
token: { address, symbol },
currency = 'USD',
Expand All @@ -46,7 +48,7 @@ const TokenRewardsMetrics: FC<TokenRewardsMetricsProps> = ({
currency,
)

const { isClaimable, claimRewards } = useClaimBuilderRewardsPerToken(gauge, address)
const { isClaimable, claimRewards, isPaused } = useClaimBuilderRewardsPerToken(builder, gauge, address)

return withSpinner(
TokenMetricsCardRow,
Expand All @@ -55,7 +57,21 @@ const TokenRewardsMetrics: FC<TokenRewardsMetricsProps> = ({
amount,
fiatAmount,
isLoading: rewardsLoading,
children: <ClaimYourRewardsButton onClick={() => claimRewards()} disabled={!isClaimable} />,
children: (
<Popover
content={
<div className="text-[12px] font-bold mb-1">
<p data-testid="adjustBackerRewardPctTooltip">You cannot be paused to claim rewards</p>
</div>
}
size="small"
position="top"
trigger="hover"
disabled={!isPaused}
>
<ClaimYourRewardsButton onClick={() => claimRewards()} disabled={!isClaimable || isPaused} />
</Popover>
),
})
}

Expand Down
27 changes: 23 additions & 4 deletions src/app/collective-rewards/rewards/builders/Rewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ import { Button } from '@/components/Button'
import { PricesContextProvider } from '@/shared/context/PricesContext'
import { FC } from 'react'
import { Address } from 'viem'
import { Popover } from '../../../../components/Popover'

type RewardsProps = RewardDetails & { gauge: Address }

const RewardsContent: FC<RewardsProps> = props => {
const {
builder,
gauge,
tokens: { rif, rbtc },
} = props
const { isClaimable, claimRewards } = useClaimBuilderRewards(gauge, {
const { isClaimable, claimRewards, isPaused } = useClaimBuilderRewards(builder, gauge, {
rif: rif.address,
rbtc: rbtc.address,
})
Expand All @@ -32,9 +34,26 @@ const RewardsContent: FC<RewardsProps> = props => {
<div className="min-h-[190px] w-full">
<BuilderClaimableRewards {...props} />
</div>
<Button className="w-full" onClick={() => claimRewards()} disabled={!isClaimable} variant="primary">
Claim all
</Button>
<Popover
content={
<div className="text-[12px] font-bold mb-1">
<p data-testid="adjustBackerRewardPctTooltip">You cannot be paused to claim rewards</p>
</div>
}
size="small"
position="top"
trigger="hover"
disabled={!isPaused}
>
<Button
className="w-full"
onClick={() => claimRewards()}
disabled={!isClaimable || isPaused}
variant="primary"
>
Claim all
</Button>
</Popover>
</MetricContainer>
<MetricContainer>
<BuilderLastCycleRewards {...props} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ import { GaugeAbi } from '@/lib/abis/v2/GaugeAbi'
import { Address } from 'viem'
import { useWaitForTransactionReceipt, useWriteContract } from 'wagmi'
import { useGetBuilderRewards } from '@/app/collective-rewards/rewards'
import { useBuilderContext } from '../../../user'

const useClaimBuilderReward = (gauge: Address, rewardToken?: Address) => {
const useClaimBuilderReward = (builder: Address, gauge: Address, rewardToken?: Address) => {
const { writeContractAsync, error: executionError, data: hash, isPending } = useWriteContract()
const { getBuilderByAddress } = useBuilderContext()

const claimingBuilder = getBuilderByAddress(builder)
console.log('🚀 ~ useClaimBuilderReward ~ claimingBuilder:', claimingBuilder)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log('🚀 ~ useClaimBuilderReward ~ claimingBuilder:', claimingBuilder)

const isPaused = !!claimingBuilder?.stateFlags?.paused
jurajpiar marked this conversation as resolved.
Show resolved Hide resolved

const { isLoading, isSuccess, data, error: receiptError } = useWaitForTransactionReceipt({ hash })

Expand Down Expand Up @@ -33,6 +39,7 @@ const useClaimBuilderReward = (gauge: Address, rewardToken?: Address) => {

return {
claimRewards: () => claimBuilderReward(),
isPaused,
error,
isPendingTx: isPending,
isLoadingReceipt: isLoading,
Expand All @@ -41,10 +48,22 @@ const useClaimBuilderReward = (gauge: Address, rewardToken?: Address) => {
}
}

export const useClaimBuilderRewards = (gauge: Address, { rif, rbtc }: { rif: Address; rbtc: Address }) => {
const { error: claimBuilderRewardError, ...rest } = useClaimBuilderReward(gauge)
const { isClaimable: rifClaimable, error: claimRifError } = useClaimBuilderRewardsPerToken(gauge, rif)
const { isClaimable: rbtcClaimable, error: claimRbtcError } = useClaimBuilderRewardsPerToken(gauge, rbtc)
export const useClaimBuilderRewards = (
builder: Address,
gauge: Address,
{ rif, rbtc }: { rif: Address; rbtc: Address },
) => {
const { error: claimBuilderRewardError, ...rest } = useClaimBuilderReward(builder, gauge)
const { isClaimable: rifClaimable, error: claimRifError } = useClaimBuilderRewardsPerToken(
builder,
gauge,
rif,
)
const { isClaimable: rbtcClaimable, error: claimRbtcError } = useClaimBuilderRewardsPerToken(
builder,
gauge,
rbtc,
)

const isClaimable = rifClaimable || rbtcClaimable
const error = claimBuilderRewardError ?? claimRifError ?? claimRbtcError
Expand All @@ -56,8 +75,8 @@ export const useClaimBuilderRewards = (gauge: Address, { rif, rbtc }: { rif: Add
}
}

export const useClaimBuilderRewardsPerToken = (gauge: Address, rewardToken: Address) => {
const { error: claimBuilderRewardError, ...rest } = useClaimBuilderReward(gauge, rewardToken)
export const useClaimBuilderRewardsPerToken = (builder: Address, gauge: Address, rewardToken: Address) => {
const { error: claimBuilderRewardError, ...rest } = useClaimBuilderReward(builder, gauge, rewardToken)
const { data: rewards, isLoading, error: getBuilderRewardsError } = useGetBuilderRewards(rewardToken, gauge)

const isClaimable = !isLoading && rewards !== 0n
Expand Down