Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

fix: Don't do mint calls for non bal reward gauges #1076

Merged
merged 2 commits into from
Sep 11, 2024
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
27 changes: 15 additions & 12 deletions lib/modules/pool/actions/claim/useClaimAllRewardsStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import { useUserAccount } from '../../../web3/UserAccountProvider'
import { useClaimCallDataQuery } from './useClaimCallDataQuery'
import { BalTokenRewardsResult } from '@/lib/modules/portfolio/PortfolioClaim/useBalRewards'
import { ClaimableBalancesResult } from '@/lib/modules/portfolio/PortfolioClaim/useClaimableBalances'
import { allClaimableGaugeAddressesFor } from '../../pool.helpers'
import { ClaimablePool } from './ClaimProvider'
import { Address } from 'viem'

const claimAllRewardsStepId = 'claim-all-rewards'

Expand Down Expand Up @@ -44,16 +44,19 @@ export function useClaimAllRewardsStep({

const chain = pool.chain as GqlChain
const stakingType = pool.staking?.type || GqlPoolStakingType.Gauge
const gaugeAddresses = pools.flatMap(pool => allClaimableGaugeAddressesFor(pool))
const shouldClaimMany = gaugeAddresses.length > 1

const claimRewardGauges = nonBalRewards.map(r => r.gaugeAddress)
const mintBalRewardGauges = balRewards.map(r => r.gaugeAddress as Address)
const allRewardGauges = [...claimRewardGauges, ...mintBalRewardGauges]
const shouldClaimMany = allRewardGauges.length > 1

const stakingService = selectStakingService(chain, stakingType)
const { data: claimData, isLoading } = useClaimCallDataQuery(
gaugeAddresses,
stakingService,
nonBalRewards.length > 0,
balRewards.length > 0,
isClaimQueryEnabled
)
const { data: claimData, isLoading } = useClaimCallDataQuery({
claimRewardGauges,
mintBalRewardGauges,
gaugeService: stakingService,
enabled: isClaimQueryEnabled,
})

const labels: TransactionLabels = {
init: `Claim${shouldClaimMany ? ' all' : ''}`,
Expand All @@ -72,7 +75,7 @@ export function useClaimAllRewardsStep({
chain,
claimData,
stakingType,
gaugeAddresses,
allRewardGauges,
}
)

Expand All @@ -83,7 +86,7 @@ export function useClaimAllRewardsStep({
contractAddress: networkConfigs[chain].contracts.balancer.relayerV6,
functionName: 'multicall',
args: [claimData],
enabled: gaugeAddresses.length > 0 && claimData && claimData.length > 0,
enabled: allRewardGauges.length > 0 && claimData && claimData.length > 0,
txSimulationMeta,
}

Expand Down
14 changes: 11 additions & 3 deletions lib/modules/pool/actions/claim/useClaimAllRewardsSteps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useApproveMinterStep } from '@/lib/modules/staking/gauge/useMinterAppro
import { TransactionStep } from '@/lib/modules/transactions/transaction-steps/lib'
import { useMemo } from 'react'
import { ClaimAllRewardsStepParams, useClaimAllRewardsStep } from './useClaimAllRewardsStep'
import { useApproveRelayerStep } from '@/lib/modules/relayer/useApproveRelayerStep'
import { getChainId } from '@/lib/config/app.config'

export function useClaimAllRewardsSteps(params: ClaimAllRewardsStepParams) {
const pool = params.pools[0]
Expand All @@ -10,23 +12,29 @@ export function useClaimAllRewardsSteps(params: ClaimAllRewardsStepParams) {
}

const { chain } = pool
const chainId = getChainId(pool.chain)

const { step: relayerApprovalStep, isLoading: isLoadingRelayerApprovalStep } =
useApproveRelayerStep(chainId)
const { step: minterApprovalStep, isLoading: isLoadingMinterApprovalStep } =
useApproveMinterStep(chain)

const { step: claimAllRewardsStep, isLoading: isLoadingClaimAllRewards } =
useClaimAllRewardsStep(params)

const steps = useMemo((): TransactionStep[] => {
const steps = [claimAllRewardsStep]
const steps = [relayerApprovalStep, claimAllRewardsStep]

if (params.balTokenRewardsQuery.balRewardsData.length > 0) {
steps.unshift(minterApprovalStep)
}

return steps
}, [claimAllRewardsStep, minterApprovalStep, params])
}, [relayerApprovalStep, claimAllRewardsStep, minterApprovalStep, params])

return {
isLoading: isLoadingMinterApprovalStep || isLoadingClaimAllRewards,
isLoading:
isLoadingRelayerApprovalStep || isLoadingMinterApprovalStep || isLoadingClaimAllRewards,
steps,
}
}
55 changes: 38 additions & 17 deletions lib/modules/pool/actions/claim/useClaimCallDataQuery.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,52 @@
import { useQuery } from '@tanstack/react-query'
import { Address } from 'viem'
import { Address, Hex } from 'viem'
import { GaugeService } from '@/lib/shared/services/staking/gauge.service'
import { useMemo } from 'react'

export function useClaimCallDataQuery(
gaugeAddresses: Address[],
gaugeService: GaugeService | undefined,
hasUnclaimedNonBalRewards: boolean,
hasUnclaimedBalRewards: boolean,
enabled = true
) {
const inputData = {
hasUnclaimedNonBalRewards,
hasUnclaimedBalRewards,
gauges: gaugeAddresses,
outputReference: 0n,
}
export function useClaimCallDataQuery({
claimRewardGauges,
mintBalRewardGauges,
gaugeService,
enabled = true,
}: {
claimRewardGauges: Address[]
mintBalRewardGauges: Address[]
gaugeService: GaugeService | undefined
enabled?: boolean
}) {
const allGauges = useMemo(() => {
return [...claimRewardGauges, ...mintBalRewardGauges]
}, [claimRewardGauges, mintBalRewardGauges])

const queryKey = ['claim', 'gauge', 'callData', allGauges]

const queryKey = ['claim', 'gauge', 'callData', inputData]
const queryFn = (): `0x${string}`[] => {
if (!gaugeService) return []
return gaugeService.getGaugeClaimRewardsContractCallData(inputData)

const calls: Hex[] = []

if (claimRewardGauges.length > 0) {
const claimRewardsCallData = gaugeService.getGaugeEncodeClaimRewardsCallData({
gauges: claimRewardGauges,
})
calls.push(claimRewardsCallData)
}

if (mintBalRewardGauges.length > 0) {
const mintCallData = gaugeService.getGaugeEncodeMintCallData({
gauges: mintBalRewardGauges,
outputReference: 0n,
})
calls.push(mintCallData)
}

return calls
}

const query = useQuery({
queryKey,
queryFn,
enabled: enabled && gaugeService && gaugeAddresses.length > 0,
enabled: enabled && gaugeService && allGauges.length > 0,
})

return {
Expand Down
Loading