11import {
22 useSendClaimRewardsTransaction ,
3+ useSendPoolWideClaimRewardsTransaction ,
34 useToken
45} from '@generationsoftware/hyperstructure-react-hooks'
56import { useAddRecentTransaction , useChainModal , useConnectModal } from '@rainbow-me/rainbowkit'
67import { TokenAmount , TransactionButton } from '@shared/react-components'
7- import { getSecondsSinceEpoch } from '@shared/utilities'
8+ import { getSecondsSinceEpoch , lower } from '@shared/utilities'
89import classNames from 'classnames'
910import { useTranslations } from 'next-intl'
1011import { useMemo } from 'react'
1112import { Address , formatUnits } from 'viem'
1213import { useAccount } from 'wagmi'
14+ import { useUserClaimablePoolWidePromotions } from '@hooks/useUserClaimablePoolWidePromotions'
1315import { useUserClaimablePromotions } from '@hooks/useUserClaimablePromotions'
16+ import { useUserClaimedPoolWidePromotions } from '@hooks/useUserClaimedPoolWidePromotions'
1417import { useUserClaimedPromotions } from '@hooks/useUserClaimedPromotions'
1518
1619interface AccountPromotionClaimActionsProps {
1720 chainId : number
1821 promotionId : bigint
19- address ?: Address
22+ userAddress ?: Address
23+ vaultAddress ?: Address
24+ isPoolWide ?: boolean
2025 fullSized ?: boolean
2126 className ?: string
2227}
2328
2429export const AccountPromotionClaimActions = ( props : AccountPromotionClaimActionsProps ) => {
25- const { chainId, promotionId, address, fullSized, className } = props
30+ const { chainId, promotionId, userAddress, vaultAddress, isPoolWide, fullSized, className } =
31+ props
2632
2733 const { address : _userAddress } = useAccount ( )
28- const userAddress = address ?? _userAddress
2934
3035 if ( ! ! userAddress ) {
3136 return (
3237 < ClaimRewardsButton
3338 chainId = { chainId }
3439 promotionId = { promotionId }
35- userAddress = { userAddress }
40+ userAddress = { userAddress ?? _userAddress }
41+ vaultAddress = { vaultAddress }
42+ isPoolWide = { isPoolWide }
3643 fullSized = { fullSized }
3744 className = { className }
3845 />
@@ -46,12 +53,15 @@ interface ClaimRewardsButtonProps {
4653 chainId : number
4754 promotionId : bigint
4855 userAddress : Address
56+ vaultAddress ?: Address
57+ isPoolWide ?: boolean
4958 fullSized ?: boolean
5059 className ?: string
5160}
5261
5362const ClaimRewardsButton = ( props : ClaimRewardsButtonProps ) => {
54- const { chainId, promotionId, userAddress, fullSized, className } = props
63+ const { chainId, promotionId, userAddress, vaultAddress, isPoolWide, fullSized, className } =
64+ props
5565
5666 const t_common = useTranslations ( 'Common' )
5767 const t_account = useTranslations ( 'Account' )
@@ -62,36 +72,78 @@ const ClaimRewardsButton = (props: ClaimRewardsButtonProps) => {
6272 const addRecentTransaction = useAddRecentTransaction ( )
6373
6474 const { refetch : refetchClaimed } = useUserClaimedPromotions ( userAddress )
75+ const { refetch : refetchPoolWideClaimed } = useUserClaimedPoolWidePromotions ( userAddress )
76+
6577 const {
6678 data : allClaimable ,
6779 isFetched : isFetchedAllClaimable ,
6880 refetch : refetchClaimable
6981 } = useUserClaimablePromotions ( userAddress )
82+ const {
83+ data : allPoolWideClaimable ,
84+ isFetched : isFetchedAllPoolWideClaimable ,
85+ refetch : refetchPoolWideClaimable
86+ } = useUserClaimablePoolWidePromotions ( userAddress )
7087
7188 const promotion = useMemo ( ( ) => {
72- return allClaimable . find (
73- ( promotion ) => promotion . chainId === chainId && promotion . promotionId === promotionId
89+ return ( isPoolWide ? allPoolWideClaimable : allClaimable ) . find (
90+ ( promotion ) =>
91+ promotion . chainId === chainId &&
92+ promotion . promotionId === promotionId &&
93+ ( ! vaultAddress || lower ( promotion . vault ) === lower ( vaultAddress ) )
7494 )
75- } , [ allClaimable ] )
95+ } , [ isPoolWide , allClaimable , allPoolWideClaimable , chainId , promotionId , vaultAddress ] )
7696
7797 const { data : token } = useToken ( chainId , promotion ?. token ! )
7898
7999 const epochsToClaim =
80- ! ! promotion && isFetchedAllClaimable
100+ ! ! promotion && ( isPoolWide ? isFetchedAllPoolWideClaimable : isFetchedAllClaimable )
81101 ? Object . keys ( promotion . epochRewards ) . map ( ( k ) => parseInt ( k ) )
82102 : [ ]
83- const { isWaiting, isConfirming, isSuccess, txHash, sendClaimRewardsTransaction } =
84- useSendClaimRewardsTransaction (
85- chainId ,
86- userAddress ,
87- { [ promotionId . toString ( ) ] : epochsToClaim } ,
88- {
89- onSuccess : ( ) => {
90- refetchClaimed ( )
91- refetchClaimable ( )
92- }
103+
104+ const {
105+ isWaiting : isWaitingClaimRewards ,
106+ isConfirming : isConfirmingClaimRewards ,
107+ isSuccess : isSuccessClaimRewards ,
108+ txHash : txHashClaimRewards ,
109+ sendClaimRewardsTransaction
110+ } = useSendClaimRewardsTransaction (
111+ chainId ,
112+ userAddress ,
113+ { [ promotionId . toString ( ) ] : epochsToClaim } ,
114+ {
115+ onSuccess : ( ) => {
116+ refetchClaimed ( )
117+ refetchClaimable ( )
93118 }
94- )
119+ }
120+ )
121+
122+ const {
123+ isWaiting : isWaitingPoolWideClaimRewards ,
124+ isConfirming : isConfirmingPoolWideClaimRewards ,
125+ isSuccess : isSuccessPoolWideClaimRewards ,
126+ txHash : txHashPoolWideClaimRewards ,
127+ sendPoolWideClaimRewardsTransaction
128+ } = useSendPoolWideClaimRewardsTransaction (
129+ chainId ,
130+ userAddress ,
131+ { [ promotionId . toString ( ) ] : { vaultAddress : promotion ?. vault ! , epochs : epochsToClaim } } ,
132+ {
133+ onSuccess : ( ) => {
134+ refetchPoolWideClaimed ( )
135+ refetchPoolWideClaimable ( )
136+ }
137+ }
138+ )
139+
140+ const isWaiting = isPoolWide ? isWaitingPoolWideClaimRewards : isWaitingClaimRewards
141+ const isConfirming = isPoolWide ? isConfirmingPoolWideClaimRewards : isConfirmingClaimRewards
142+ const isSuccess = isPoolWide ? isSuccessPoolWideClaimRewards : isSuccessClaimRewards
143+ const txHash = isPoolWide ? txHashPoolWideClaimRewards : txHashClaimRewards
144+ const sendTransaction = isPoolWide
145+ ? sendPoolWideClaimRewardsTransaction
146+ : sendClaimRewardsTransaction
95147
96148 if ( ! ! promotion && ! ! token ) {
97149 const claimableAmount = Object . values ( promotion . epochRewards ) . reduce ( ( a , b ) => a + b , 0n )
@@ -110,7 +162,7 @@ const ClaimRewardsButton = (props: ClaimRewardsButtonProps) => {
110162 chainId = { chainId }
111163 isTxLoading = { isWaiting || isConfirming }
112164 isTxSuccess = { isSuccess }
113- write = { sendClaimRewardsTransaction }
165+ write = { sendTransaction }
114166 txHash = { txHash }
115167 txDescription = { t_account ( 'claimRewardsTx' , { symbol : token . symbol } ) }
116168 openConnectModal = { openConnectModal }
0 commit comments