Skip to content

Commit

Permalink
feat(cr_v2): all time rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscotobar committed Nov 11, 2024
1 parent 622adbc commit 8766ed7
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
88 changes: 88 additions & 0 deletions src/app/collective-rewards/rewards/AllTimeRewards.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { Address } from 'viem'
import { usePricesContext } from '@/shared/context/PricesContext'
import { FC } from 'react'
import { formatBalanceToHuman } from '@/app/user/Balances/balanceUtils'
import {
BuilderRewardsClaimedEventLog,
useGetBuilderRewardsClaimedLogs,
MetricsCardWithSpinner,
formatMetrics,
useGetBuilderRewards,
} from '@/app/collective-rewards/rewards'
import { useHandleErrors } from '@/app/collective-rewards/utils'

type Token = {
symbol: string
address: Address
}

type AllTimeRewardsProps = {
gauge: Address
currency?: string
data: {
[token: string]: Token
}
}

const useGetRewardMetrics = (
gauge: Address,
tokenAddress: Address,
symbol: string,
currency: string,
logs?: BuilderRewardsClaimedEventLog,
) => {
const { data: claimableRewards, isLoading, error } = useGetBuilderRewards(tokenAddress, gauge)
const { prices } = usePricesContext()

const totalClaimedRewards =
logs?.reduce((acc, event) => {
const amount = event.args.amount_ ?? 0n
return acc + amount
}, 0n) ?? 0n

const totalRewards = totalClaimedRewards + (claimableRewards ?? 0n)
const totalRewardsInHuman = Number(formatBalanceToHuman(totalRewards))
const price = prices[symbol]?.price ?? 0

return {
data: formatMetrics(totalRewardsInHuman, price, symbol, currency),
isLoading,
error,
}
}

export const AllTimeRewards: FC<AllTimeRewardsProps> = ({ gauge, data: { rif, rbtc }, currency = 'USD' }) => {
const {
data: rewardsPerToken,
isLoading: logsLoading,
error: rewardsError,
} = useGetBuilderRewardsClaimedLogs(gauge)

const {
data: rifRewardsMetrics,
isLoading: rifLoading,
error: rifError,
} = useGetRewardMetrics(gauge, rif.address, rif.symbol, currency, rewardsPerToken[rif.address])
const {
data: rbtcRewardsMetrics,
isLoading: rbtcLoading,
error: rbtcError,
} = useGetRewardMetrics(gauge, rbtc.address, rbtc.symbol, currency, rewardsPerToken[rbtc.address])

useHandleErrors([
{ error: rewardsError, title: 'Error loading rewards' },
{ error: rifError, title: 'Error loading builder rif rewards' },
{ error: rbtcError, title: 'Error loading builder rbtc rewards' },
])

const isLoading = rifLoading || rbtcLoading || logsLoading

return (
<MetricsCardWithSpinner
title="All time rewards"
data={{ rif: rifRewardsMetrics, rbtc: rbtcRewardsMetrics }}
isLoading={isLoading}
borderless
/>
)
}
3 changes: 2 additions & 1 deletion src/app/collective-rewards/rewards/MyRewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useClaimStateReporting,
LastCycleRewards,
ClaimableRewards,
AllTimeRewards,
} from '@/app/collective-rewards/rewards'
import { CycleContextProvider } from '@/app/collective-rewards/metrics'
import { PricesContextProvider } from '@/shared/context/PricesContext'
Expand Down Expand Up @@ -48,7 +49,7 @@ export const Rewards: FC<{ builder: Address; gauge: Address }> = ({ builder, gau
<ClaimableRewards builder={builder} gauge={gauge} data={data} />
<LastCycleRewards gauge={gauge} data={data} />
<div>Estimated Rewards</div>
<div>All time rewards</div>
<AllTimeRewards gauge={gauge} data={data} />
<div>All time share</div>
<Popover
disabled={isClaimFunctionReady}
Expand Down
1 change: 1 addition & 0 deletions src/app/collective-rewards/rewards/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './useClaimBuilderRewards'
export * from './useGetBuilderRewards'
export * from './useGetBuilderRewardsClaimedLogs'
export * from './useGetRewardDistributedLogs'
export * from './useGetTokenProjectedReward'
export * from './useGetNotifyRewardLogs'
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useQuery } from '@tanstack/react-query'
import { fetchNotifyRewardLogs } from '@/app/collective-rewards/actions'
import { Address, getAddress, parseEventLogs } from 'viem'
import { GaugeAbi } from '@/lib/abis/v2/GaugeAbi'

export type BuilderRewardsClaimedEventLog = ReturnType<
typeof parseEventLogs<typeof GaugeAbi, true, 'BuilderRewardsClaimed'>
>

export type BuilderClaimedRewardsPerToken = Record<Address, BuilderRewardsClaimedEventLog>

export const useGetBuilderRewardsClaimedLogs = (gauge?: Address) => {
const { data, error, isLoading } = useQuery({
queryFn: async () => {
const { data } = await fetchNotifyRewardLogs(gauge!)

const events = parseEventLogs({
abi: GaugeAbi,
logs: data,
eventName: 'BuilderRewardsClaimed',
})

return events.reduce<BuilderClaimedRewardsPerToken>((acc, log) => {
const rewardToken = getAddress(log.args.rewardToken_)
const existingRewardToken = acc[rewardToken] || []
existingRewardToken.push(log)

acc[rewardToken] = existingRewardToken

return acc
}, {})
},
queryKey: ['builderRewardsClaimedLogs'],
refetchInterval: 30_000,
initialData: {},
enabled: !!gauge,
})

return {
data,
error,
isLoading,
}
}
1 change: 1 addition & 0 deletions src/app/collective-rewards/rewards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './utils'
export * from './MyRewards'
export * from './LastCycleRewards'
export * from './ClaimableRewards'
export * from './AllTimeRewards'

0 comments on commit 8766ed7

Please sign in to comment.