-
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
7f9d548
commit 65e7d09
Showing
13 changed files
with
196 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { useCycleContext } from '@/app/collective-rewards/metrics/context/CycleContext' | ||
import { | ||
formatMetrics, | ||
getLastCycleRewards, | ||
MetricsCard, | ||
MetricsCardTitle, | ||
MetricsCardWithSpinner, | ||
TokenMetricsCardRow, | ||
useGetNotifyRewardLogs, | ||
useGetRewardsCoinbase, | ||
useGetRewardsERC20, | ||
useGetRewardShares, | ||
useGetTotalPotentialReward, | ||
} from '@/app/collective-rewards/rewards' | ||
import { useHandleErrors } from '@/app/collective-rewards/utils' | ||
import { formatBalanceToHuman } from '@/app/user/Balances/balanceUtils' | ||
import { usePricesContext } from '@/shared/context/PricesContext' | ||
import { FC, useEffect, useState } from 'react' | ||
import { Address } from 'viem' | ||
import { useGetPerTokenRewards } from './hooks/useGetPerTokenRewards' | ||
import { withSpinner } from '@/components/LoadingSpinner/withLoadingSpinner' | ||
|
||
type Token = { | ||
symbol: string | ||
address: Address | ||
} | ||
|
||
type TokenRewardsProps = { | ||
gauge: Address | ||
currency?: string | ||
token: Token & { id: 'rif' | 'rbtc' } | ||
} | ||
|
||
const TokenRewards: FC<TokenRewardsProps> = ({ gauge, token: { id, symbol }, currency = 'USD' }) => { | ||
const { [id]: tokenRewards } = useGetPerTokenRewards() | ||
const [rewards, setRewards] = useState<bigint>(0n) | ||
const [rewardsError, setRewardsError] = useState<Error | null>(null) | ||
const [isRewardsLoading, setIsRewardsLoading] = useState(true) | ||
|
||
useEffect(() => { | ||
if (tokenRewards && symbol in tokenRewards) { | ||
setRewards(tokenRewards.data ?? 0n) | ||
} | ||
if (tokenRewards && tokenRewards.error) { | ||
setRewardsError(tokenRewards.error) | ||
} | ||
if (tokenRewards) { | ||
setIsRewardsLoading(tokenRewards.isLoading) | ||
} | ||
}, [tokenRewards, symbol]) | ||
|
||
const { | ||
data: totalPotentialRewards, | ||
isLoading: totalPotentialRewardsLoading, | ||
error: totalPotentialRewardsError, | ||
} = useGetTotalPotentialReward() | ||
const { | ||
data: rewardShares, | ||
isLoading: rewardSharesLoading, | ||
error: rewardSharesError, | ||
} = useGetRewardShares(gauge) | ||
|
||
const error = rewardsError ?? totalPotentialRewardsError ?? rewardSharesError | ||
useHandleErrors({ error, title: 'Error loading estimated rewards' }) | ||
|
||
const { prices } = usePricesContext() | ||
|
||
const estimatedRewards = | ||
rewards && rewardShares && totalPotentialRewards ? (rewards * rewardShares) / totalPotentialRewards : 0n | ||
const estimatedRewardsInHuman = Number(formatBalanceToHuman(estimatedRewards)) | ||
const price = prices[symbol]?.price ?? 0 | ||
const { amount, fiatAmount } = formatMetrics(estimatedRewardsInHuman, price, symbol, currency) | ||
|
||
return withSpinner(TokenMetricsCardRow)({ | ||
amount, | ||
fiatAmount, | ||
isLoading: isRewardsLoading || totalPotentialRewardsLoading || rewardSharesLoading, | ||
}) | ||
} | ||
|
||
type EstimatedRewardsProps = { | ||
gauge: Address | ||
currency?: string | ||
data: { | ||
[token: string]: Token | ||
} | ||
} | ||
|
||
export const EstimatedRewards: FC<EstimatedRewardsProps> = ({ data: { rif, rbtc }, ...rest }) => { | ||
return ( | ||
<MetricsCard borderless> | ||
<MetricsCardTitle title="Estimated rewards" data-testid="EstimatedRewards" /> | ||
<TokenRewards {...rest} token={{ ...rif, id: 'rif' }} /> | ||
<TokenRewards {...rest} token={{ ...rbtc, id: '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
21 changes: 21 additions & 0 deletions
21
src/app/collective-rewards/rewards/hooks/getRewardShares.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,21 @@ | ||
import { GaugeAbi } from '@/lib/abis/v2/GaugeAbi' | ||
import { AVERAGE_BLOCKTIME } from '@/lib/constants' | ||
import { Address } from 'viem' | ||
import { useReadContract } from 'wagmi' | ||
|
||
export const useGetRewardShares = (gauge: Address) => { | ||
const { data, isLoading, error } = useReadContract({ | ||
address: gauge, | ||
abi: GaugeAbi, | ||
functionName: 'rewardShares', | ||
query: { | ||
refetchInterval: AVERAGE_BLOCKTIME, | ||
}, | ||
}) | ||
|
||
return { | ||
data, | ||
isLoading, | ||
error, | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/app/collective-rewards/rewards/hooks/getTotalPotentialRewards.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,21 @@ | ||
import { BackersManagerAbi } from '@/lib/abis/v2/BackersManagerAbi' | ||
import { AVERAGE_BLOCKTIME } from '@/lib/constants' | ||
import { BackersManagerAddress } from '@/lib/contracts' | ||
import { useReadContract } from 'wagmi' | ||
|
||
export const useGetTotalPotentialReward = () => { | ||
const { data, isLoading, error } = useReadContract({ | ||
address: BackersManagerAddress, | ||
abi: BackersManagerAbi, | ||
functionName: 'totalPotentialReward', | ||
query: { | ||
refetchInterval: AVERAGE_BLOCKTIME, | ||
}, | ||
}) | ||
|
||
return { | ||
data, | ||
isLoading, | ||
error, | ||
} | ||
} |
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
12 changes: 12 additions & 0 deletions
12
src/app/collective-rewards/rewards/hooks/useGetPerTokenRewards.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,12 @@ | ||
import { BackersManagerAbi } from '@/lib/abis/v2/BackersManagerAbi' | ||
import { UseReadContractReturnType } from 'wagmi' | ||
import { useGetRewardsCoinbase } from './useGetRewardsCoinbase' | ||
import { useGetRewardsERC20 } from './useGetRewardsERC20' | ||
|
||
export const useGetPerTokenRewards = (): Record< | ||
'rif' | 'rbtc', | ||
UseReadContractReturnType<typeof BackersManagerAbi, 'rewardsERC20' | 'rewardsCoinbase'> | ||
> => ({ | ||
rif: useGetRewardsERC20(), | ||
rbtc: useGetRewardsCoinbase(), | ||
}) |
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
15 changes: 15 additions & 0 deletions
15
src/app/collective-rewards/rewards/hooks/useGetRewardsCoinbase.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,15 @@ | ||
import { BackersManagerAbi } from '@/lib/abis/v2/BackersManagerAbi' | ||
import { AVERAGE_BLOCKTIME } from '@/lib/constants' | ||
import { BackersManagerAddress } from '@/lib/contracts' | ||
import { useReadContract, UseReadContractReturnType } from 'wagmi' | ||
|
||
export const useGetRewardsCoinbase = () => { | ||
return useReadContract({ | ||
address: BackersManagerAddress, | ||
abi: BackersManagerAbi, | ||
functionName: 'rewardsCoinbase', | ||
query: { | ||
refetchInterval: AVERAGE_BLOCKTIME, | ||
}, | ||
}) | ||
} |
16 changes: 16 additions & 0 deletions
16
src/app/collective-rewards/rewards/hooks/useGetRewardsERC20.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,16 @@ | ||
import { BackersManagerAbi } from '@/lib/abis/v2/BackersManagerAbi' | ||
import { AVERAGE_BLOCKTIME } from '@/lib/constants' | ||
import { BackersManagerAddress } from '@/lib/contracts' | ||
import { ReadContractReturnType } from 'viem/actions' | ||
import { useReadContract, UseReadContractReturnType } from 'wagmi' | ||
|
||
export const useGetRewardsERC20 = () => { | ||
return useReadContract({ | ||
address: BackersManagerAddress, | ||
abi: BackersManagerAbi, | ||
functionName: 'rewardsERC20', | ||
query: { | ||
refetchInterval: AVERAGE_BLOCKTIME, | ||
}, | ||
}) | ||
} |
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
4 changes: 2 additions & 2 deletions
4
...ctive-rewards/utils/getCoinBaseAddress.ts → ...ctive-rewards/utils/getCoinbaseAddress.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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { getAddress, keccak256, toUtf8Bytes, ZeroAddress } from 'ethers' | ||
import { Address } from 'viem' | ||
|
||
export const getCoinBaseAddress = () => { | ||
export const getCoinbaseAddress = () => { | ||
return getAddress(keccak256(toUtf8Bytes('COINBASE_ADDRESS')).slice(26)) as Address | ||
} | ||
|
||
export const resolveCollectiveRewardToken = (rewardToken: Address) => | ||
rewardToken === ZeroAddress ? getCoinBaseAddress() : rewardToken | ||
rewardToken === ZeroAddress ? getCoinbaseAddress() : rewardToken |
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,2 +1,2 @@ | ||
export * from './handleErrors' | ||
export * from './getCoinBaseAddress' | ||
export * from './getCoinbaseAddress' |