Skip to content

Commit

Permalink
feat(wip): implement the formula
Browse files Browse the repository at this point in the history
  • Loading branch information
antomor committed Dec 11, 2024
1 parent 9364fd4 commit e8f11dc
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@/app/collective-rewards/rewards'
import { withSpinner } from '@/components/LoadingSpinner/withLoadingSpinner'
import { useHandleErrors } from '@/app/collective-rewards/utils'
import { useGetTotalAllocation } from './hooks/useGetTotalAllocation'

type TotalAllocationsProps = {
gauges: Address[]
Expand All @@ -25,13 +26,11 @@ export const TotalAllocationsMetrics: FC<TotalAllocationsProps> = ({
currency = 'USD',
}) => {
const { prices } = usePricesContext()
const { data, isLoading, error } = useGaugesGetFunction(gauges, 'totalAllocation')
const { data: totalAllocations, isLoading, error } = useGetTotalAllocation(gauges)
useHandleErrors({ error, title: 'Error loading total allocations' })
const totalAllocationsInHuman = Number(formatOnchainFraction(totalAllocations))

const price = prices[symbol]?.price ?? 0

const totalAllocations = Object.values(data).reduce((acc, allocation) => acc + allocation, 0n)
const totalAllocationsInHuman = Number(formatOnchainFraction(totalAllocations))
const fiatAmount = `= ${currency} ${formatCurrency(totalAllocationsInHuman * price, currency)}`

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { MetricsCard, MetricsCardTitle, TokenMetricsCardRow } from '@/app/collective-rewards/rewards'
import { withSpinner } from '@/components/LoadingSpinner/withLoadingSpinner'
import { useGetABI } from './hooks/useGetABI'

export const ABIMetrics = () => {
const isLoading = false
const abiPct = 0
const { data: abiPct, isLoading } = useGetABI()
return (
<>
<MetricsCard borderless>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useGetRewardsERC20 } from '@/app/collective-rewards/rewards'
import { usePricesContext } from '@/shared/context/PricesContext'
import { useGetTotalAllocation } from '../../../hooks/useGetTotalAllocation'
import { useBuilderContext, useGetGaugesArrayByType } from '@/app/collective-rewards/user'

export const useGetABI = () => {
const { data: cyclePayout, isLoading: isCyclePayoutLoading, error: cyclePayoutError } = useGetRewardsERC20()
const { data: builders } = useBuilderContext()
// FIXME: verify if this filter is correct
// activatedBuilders already include the backer rewards percentage
const activatedBuilders = builders.filter(builder => builder.stateFlags?.activated === false)
// FIXME: for each builder, get votes allocated to that builder and calculate the weighted average
const weightedAverageBuilderRewardsPct = 0n

const { data: gauges } = useGetGaugesArrayByType('active')
const {
data: totalAllocation,
isLoading: isTotalAllocationLoading,
error: totalAllocationError,
} = useGetTotalAllocation(gauges ?? [])
// FIXME: verify if we need to use the cyclePayout in wei or in RIF and if we need to use bigint or Number
const rewardsPerStRIFPerCycle = Number(
((cyclePayout ?? 1n) * weightedAverageBuilderRewardsPct) / totalAllocation,
)
const { prices } = usePricesContext()
const rifPrice = prices.RIF?.price ?? 0
const abi = rifPrice ? Math.pow(1 + rewardsPerStRIFPerCycle / rifPrice, 26) - 1 : 0

const isLoading = isCyclePayoutLoading || isTotalAllocationLoading
const error = cyclePayoutError ?? totalAllocationError
return {
data: abi,
isLoading,
error,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ABIMetrics'
12 changes: 12 additions & 0 deletions src/app/collective-rewards/metrics/hooks/useGetTotalAllocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useGaugesGetFunction } from '../../shared'

export const useGetTotalAllocation = (gauges: any[]) => {
const { data, isLoading, error } = useGaugesGetFunction(gauges, 'totalAllocation')

const totalAllocations = Object.values(data).reduce((acc, allocation) => acc + allocation, 0n)
return {
data: totalAllocations,
isLoading,
error,
}
}

0 comments on commit e8f11dc

Please sign in to comment.