-
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
e4162cb
commit 05d10d5
Showing
9 changed files
with
156 additions
and
11 deletions.
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
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,87 @@ | ||
import { | ||
formatMetrics, | ||
MetricsCard, | ||
MetricsCardTitle, | ||
MetricsCardWithSpinner, | ||
TokenMetricsCardRow, | ||
useGetBuilderRewards, | ||
useGetBuilderRewardsClaimedLogs, | ||
} from '@/app/collective-rewards/rewards' | ||
import { useHandleErrors } from '@/app/collective-rewards/utils' | ||
import { formatBalanceToHuman } from '@/app/user/Balances/balanceUtils' | ||
import { withSpinner } from '@/components/LoadingSpinner/withLoadingSpinner' | ||
import { usePricesContext } from '@/shared/context/PricesContext' | ||
import { FC } from 'react' | ||
import { Address } from 'viem' | ||
|
||
type Token = { | ||
symbol: string | ||
address: Address | ||
} | ||
|
||
type TokenRewardsMetricsProps = { | ||
gauge: Address | ||
currency?: string | ||
token: { | ||
symbol: string | ||
address: Address | ||
} | ||
} | ||
|
||
const TokenRewardsMetrics: FC<TokenRewardsMetricsProps> = ({ | ||
gauge, | ||
token: { symbol, address }, | ||
currency = 'USD', | ||
}) => { | ||
const { | ||
data: rewardsPerToken, | ||
isLoading: logsLoading, | ||
error: rewardsError, | ||
} = useGetBuilderRewardsClaimedLogs(gauge) | ||
const { | ||
data: claimableRewards, | ||
isLoading: claimableRewardsLoading, | ||
error: claimableRewardsError, | ||
} = useGetBuilderRewards(address, gauge) | ||
|
||
const error = rewardsError ?? claimableRewardsError | ||
useHandleErrors({ error, title: 'Error loading all time rewards' }) | ||
|
||
const { prices } = usePricesContext() | ||
|
||
const totalClaimedRewards = | ||
rewardsPerToken[address]?.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 | ||
|
||
const { amount, fiatAmount } = formatMetrics(totalRewardsInHuman, price, symbol, currency) | ||
|
||
return withSpinner(TokenMetricsCardRow)({ | ||
amount, | ||
fiatAmount, | ||
isLoading: logsLoading || claimableRewardsLoading, | ||
}) | ||
} | ||
|
||
type AllTimeRewardsProps = { | ||
gauge: Address | ||
currency?: string | ||
data: { | ||
[token: string]: Token | ||
} | ||
} | ||
|
||
export const AllTimeRewards: FC<AllTimeRewardsProps> = ({ data: { rif, rbtc }, ...rest }) => { | ||
return ( | ||
<MetricsCard borderless> | ||
<MetricsCardTitle title="All time rewards" data-testid="AllTimeRewards" /> | ||
<TokenRewardsMetrics {...rest} token={rif} /> | ||
<TokenRewardsMetrics {...rest} token={rbtc} /> | ||
</MetricsCard> | ||
) | ||
} |
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
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' |
41 changes: 41 additions & 0 deletions
41
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,41 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { fetchBuilderRewardsClaimed } from '@/app/collective-rewards/actions' | ||
import { Address, getAddress, parseEventLogs } from 'viem' | ||
import { GaugeAbi } from '@/lib/abis/v2/GaugeAbi' | ||
import { AVERAGE_BLOCKTIME } from '@/lib/constants' | ||
|
||
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 fetchBuilderRewardsClaimed(gauge) | ||
|
||
const events = parseEventLogs({ | ||
abi: GaugeAbi, | ||
logs: data, | ||
eventName: 'BuilderRewardsClaimed', | ||
}) | ||
|
||
return events.reduce<BuilderClaimedRewardsPerToken>((acc, log) => { | ||
const rewardToken = getAddress(log.args.rewardToken_) | ||
acc[rewardToken] = [...(acc[rewardToken] || []), log] | ||
|
||
return acc | ||
}, {}) | ||
}, | ||
queryKey: ['builderRewardsClaimedLogs', gauge], | ||
refetchInterval: AVERAGE_BLOCKTIME, | ||
initialData: {}, | ||
}) | ||
|
||
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
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