-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
622adbc
commit 8766ed7
Showing
5 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
44 changes: 44 additions & 0 deletions
44
src/app/collective-rewards/rewards/hooks/useGetBuilderRewardsClaimedLogs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters