Skip to content

Commit

Permalink
TOK-517: pause builder reward claim (#431)
Browse files Browse the repository at this point in the history
* refactor: pause builder reward claim

* refactor: pr comments
  • Loading branch information
franciscotobar authored and shenshin committed Dec 10, 2024
1 parent ee29ab3 commit 7d1c215
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
12 changes: 10 additions & 2 deletions src/app/collective-rewards/rewards/builders/ClaimableRewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type TokenRewardsMetricsProps = {
}

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

const { isClaimable, claimRewards } = useClaimBuilderRewardsPerToken(gauge, address)
const { isClaimable, claimRewards, isPaused } = useClaimBuilderRewardsPerToken(builder, gauge, address)
const content = isPaused ? 'You cannot be paused to claim rewards' : undefined

return withSpinner(
TokenMetricsCardRow,
Expand All @@ -55,7 +57,13 @@ const TokenRewardsMetrics: FC<TokenRewardsMetricsProps> = ({
amount,
fiatAmount,
isLoading: rewardsLoading,
children: <ClaimYourRewardsButton onClick={() => claimRewards()} disabled={!isClaimable} />,
children: (
<ClaimYourRewardsButton
onClick={() => claimRewards()}
disabled={!isClaimable || isPaused}
content={content}
/>
),
})
}

Expand Down
28 changes: 24 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,27 @@ 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>
}
className="w-full"
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,14 @@ 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 '@/app/collective-rewards/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)
const isPaused = claimingBuilder?.stateFlags?.paused ?? false

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

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

return {
claimRewards: () => claimBuilderReward(),
isPaused,
error,
isPendingTx: isPending,
isLoadingReceipt: isLoading,
Expand All @@ -41,10 +47,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 +74,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
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ const ClaimYourRewardsSvg = () => (
</svg>
)

export const ClaimYourRewardsButton: FC<Pick<ButtonProps, 'onClick' | 'disabled'>> = buttonProps => (
export const ClaimYourRewardsButton: FC<Pick<ButtonProps, 'onClick' | 'disabled'> & { content?: string }> = ({
content,
...buttonProps
}) => (
<div className="self-start justify-self-end pt-[10px]">
<Popover
content={
<div className="text-[12px] font-bold mb-1">
<p data-testid="claimYouRewardsButtonTooltip">Claim your rewards</p>
<p data-testid="claimYouRewardsButtonTooltip">{content ?? 'Claim your rewards'}</p>
</div>
}
size="small"
Expand Down

0 comments on commit 7d1c215

Please sign in to comment.