-
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
7f0a332
commit 07da9ed
Showing
12 changed files
with
199 additions
and
8 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
src/app/collective-rewards/rewards/EstimatedRewards.tsx
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,104 @@ | ||
import { useCycleContext } from '@/app/collective-rewards/metrics/context/CycleContext' | ||
import { | ||
formatMetrics, | ||
getLastCycleRewards, | ||
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' | ||
|
||
type Token = { | ||
symbol: string | ||
address: Address | ||
} | ||
|
||
type RewardsTokenMetricsProps = { | ||
gauge: Address | ||
currency?: string | ||
token: { | ||
symbol: string | ||
address: Address | ||
} | ||
rewards?: bigint | ||
setState: (state: { isLoading: boolean }) => void | ||
} | ||
|
||
const RewardsTokenMetrics: FC<RewardsTokenMetricsProps> = ({ | ||
gauge, | ||
rewards = 0n, | ||
token: { symbol, address }, | ||
currency = 'USD', | ||
setState, | ||
}) => { | ||
const { | ||
data: totalPotentialRewards, | ||
isLoading: totalPotentialRewardsLoading, | ||
error: totalPotentialRewardsError, | ||
} = useGetTotalPotentialReward() | ||
const { | ||
data: rewardShares, | ||
isLoading: rewardSharesLoading, | ||
error: rewardSharesError, | ||
} = useGetRewardShares(gauge) | ||
|
||
const error = totalPotentialRewardsError ?? rewardSharesError | ||
useHandleErrors([{ error: error, title: 'Error loading estimated rewards' }]) | ||
|
||
const { prices } = usePricesContext() | ||
|
||
const estimatedRewards = | ||
!!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) | ||
|
||
useEffect(() => { | ||
setState({ isLoading: totalPotentialRewardsLoading || rewardSharesLoading }) | ||
}, [totalPotentialRewardsLoading, rewardSharesLoading, setState]) | ||
|
||
return <TokenMetricsCardRow amount={amount} fiatAmount={fiatAmount} /> | ||
} | ||
|
||
type EstimatedRewardsProps = { | ||
gauge: Address | ||
currency?: string | ||
data: { | ||
[token: string]: Token | ||
} | ||
} | ||
|
||
export const EstimatedRewards: FC<EstimatedRewardsProps> = ({ data: { rif, rbtc }, ...rest }) => { | ||
const { data: rifRewards, isLoading: rifRewardsLoading, error: rifRewardsError } = useGetRewardsERC20() | ||
const { | ||
data: rbtcRewards, | ||
isLoading: rbtcRewardsLoading, | ||
error: rbtcRewardsError, | ||
} = useGetRewardsCoinbase() | ||
|
||
const error = rifRewardsError ?? rbtcRewardsError | ||
useHandleErrors([{ error: error, title: 'Error loading estimated rewards' }]) | ||
|
||
const [{ isLoading: isLoadingRif }, setRifState] = useState({ isLoading: false }) | ||
const [{ isLoading: isLoadingRbtc }, setRbtcState] = useState({ isLoading: false }) | ||
|
||
return ( | ||
<MetricsCardWithSpinner | ||
isLoading={isLoadingRif || isLoadingRbtc || rifRewardsLoading || rbtcRewardsLoading} | ||
borderless | ||
> | ||
<MetricsCardTitle title="Estimated rewards" data-testid="EstimatedRewards" /> | ||
<RewardsTokenMetrics {...rest} token={rif} rewards={rifRewards} setState={() => setRifState} /> | ||
<RewardsTokenMetrics {...rest} token={rbtc} rewards={rbtcRewards} setState={() => setRbtcState} /> | ||
</MetricsCardWithSpinner> | ||
) | ||
} |
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 { 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: 30_000, | ||
enabled: !!gauge, | ||
}, | ||
}) | ||
|
||
return { | ||
data, | ||
isLoading, | ||
error, | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
import { BackersManagerAbi } from '@/lib/abis/v2/BackersManagerAbi' | ||
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: 30_000, | ||
}, | ||
}) | ||
|
||
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
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
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
import { BackersManagerAbi } from '@/lib/abis/v2/BackersManagerAbi' | ||
import { BackersManagerAddress } from '@/lib/contracts' | ||
import { useReadContract } from 'wagmi' | ||
|
||
export const useGetRewardsCoinbase = () => { | ||
const { data, isLoading, error } = useReadContract({ | ||
address: BackersManagerAddress, | ||
abi: BackersManagerAbi, | ||
functionName: 'rewardsCoinbase', | ||
query: { | ||
refetchInterval: 30_000, | ||
}, | ||
}) | ||
|
||
return { | ||
data, | ||
isLoading, | ||
error, | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
import { BackersManagerAbi } from '@/lib/abis/v2/BackersManagerAbi' | ||
import { BackersManagerAddress } from '@/lib/contracts' | ||
import { useReadContract } from 'wagmi' | ||
|
||
export const useGetRewardsERC20 = () => { | ||
const { data, isLoading, error } = useReadContract({ | ||
address: BackersManagerAddress, | ||
abi: BackersManagerAbi, | ||
functionName: 'rewardsERC20', | ||
query: { | ||
refetchInterval: 30_000, | ||
}, | ||
}) | ||
|
||
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
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' |