-
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.
feat(ABI): annual backers incentives
- Loading branch information
1 parent
97df3b7
commit 214a4d1
Showing
16 changed files
with
233 additions
and
42 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
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
43 changes: 43 additions & 0 deletions
43
src/app/collective-rewards/metrics/components/ABIMetrics/ABIMetrics.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,43 @@ | ||
import { MetricsCard, MetricsCardTitle, TokenMetricsCardRow } from '@/app/collective-rewards/rewards' | ||
import { withSpinner } from '@/components/LoadingSpinner/withLoadingSpinner' | ||
import { useGetABI } from './hooks/useGetABI' | ||
|
||
export const ABIMetrics = () => { | ||
const { data: abiPct, isLoading } = useGetABI() | ||
return ( | ||
<> | ||
<MetricsCard borderless> | ||
<MetricsCardTitle | ||
title="Annual Backers Incentives %" | ||
data-testid="abiPct" | ||
tooltip={{ | ||
text: ( | ||
<span className="font-rootstock-sans text-sm font-normal"> | ||
The Annual Backers Incentives (%) represents an estimate of the annualized percentage of | ||
rewards that backers could receive based on their backing allocations. | ||
<br /> | ||
<br /> | ||
The calculation follows the formula: (1 + Rewards per stRIF per Cycle / RIF price)^26 - 1. | ||
<br /> | ||
<br /> | ||
This estimation is dynamic and may vary based on total rewards and user activity. This data is | ||
for informational purposes only.{' '} | ||
</span> | ||
), | ||
popoverProps: { | ||
size: 'medium', | ||
position: 'left-bottom', | ||
}, | ||
}} | ||
/> | ||
{withSpinner( | ||
TokenMetricsCardRow, | ||
'min-h-0 grow-0', | ||
)({ | ||
amount: `${abiPct.toFixed(0)} %`, | ||
isLoading, | ||
})} | ||
</MetricsCard> | ||
</> | ||
) | ||
} |
119 changes: 119 additions & 0 deletions
119
src/app/collective-rewards/metrics/components/ABIMetrics/hooks/useGetABI.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,119 @@ | ||
import { | ||
useGetBackersRewardPercentage, | ||
useGetRewardsCoinbase, | ||
useGetRewardsERC20, | ||
} from '@/app/collective-rewards/rewards' | ||
import { usePricesContext } from '@/shared/context/PricesContext' | ||
import { useGetBuildersByState } from '@/app/collective-rewards/user' | ||
import { RequiredBuilder } from '@/app/collective-rewards/types' | ||
import { useGaugesGetFunction } from '@/app/collective-rewards/shared' | ||
import { useCycleContext } from '@/app/collective-rewards/metrics' | ||
import { formatEther } from 'viem' | ||
import { useMemo } from 'react' | ||
|
||
export const useGetABI = () => { | ||
const { | ||
data: builders, | ||
isLoading: buildersLoading, | ||
error: buildersError, | ||
} = useGetBuildersByState<RequiredBuilder>({ | ||
activated: true, | ||
communityApproved: true, | ||
kycApproved: true, | ||
revoked: false, | ||
}) | ||
|
||
const { data: rifRewards, isLoading: rifRewardsLoading, error: rifRewardsError } = useGetRewardsERC20() | ||
const { | ||
data: rbtcRewards, | ||
isLoading: rbtcRewardsLoading, | ||
error: rbtcRewardsError, | ||
} = useGetRewardsCoinbase() | ||
|
||
const { | ||
data: { cycleNext }, | ||
isLoading: cycleLoading, | ||
error: cycleError, | ||
} = useCycleContext() | ||
|
||
const gauges = builders.map(({ gauge }) => gauge) | ||
const { | ||
data: totalAllocation, | ||
isLoading: totalAllocationLoading, | ||
error: totalAllocationError, | ||
} = useGaugesGetFunction(gauges, 'totalAllocation') | ||
|
||
const buildersAddress = builders.map(({ address }) => address) | ||
const { | ||
data: backersRewardsPct, | ||
isLoading: backersRewardsPctLoading, | ||
error: backersRewardsPctError, | ||
} = useGetBackersRewardPercentage(buildersAddress, cycleNext.toSeconds()) | ||
|
||
const { prices } = usePricesContext() | ||
|
||
const abi = useMemo(() => { | ||
const sumTotalAllocation = Object.values(totalAllocation).reduce((acc, value) => acc + (value ?? 0n), 0n) | ||
|
||
if (!sumTotalAllocation) { | ||
return 0 | ||
} | ||
|
||
const rifPrice = prices.RIF?.price ?? 0 | ||
const rbtcPrice = prices.RBTC?.price ?? 0 | ||
const rifAmount = Number(formatEther(rifRewards ?? 0n)) | ||
const rbtcAmount = Number(formatEther(rbtcRewards ?? 0n)) | ||
const cyclePayout = rifAmount * rifPrice + rbtcAmount * rbtcPrice | ||
|
||
if (!rifPrice) { | ||
return 0 | ||
} | ||
|
||
const topFiveBuilders = builders | ||
.reduce<Array<{ allocation: bigint; current: number }>>((acc, builder) => { | ||
const allocation = totalAllocation[builder.gauge] | ||
const rewardPct = backersRewardsPct[builder.address] | ||
if (allocation && rewardPct) { | ||
acc.push({ allocation, current: rewardPct.current }) | ||
} | ||
return acc | ||
}, []) | ||
.sort((a, b) => (a.allocation > b.allocation ? -1 : 1)) | ||
.slice(0, 5) | ||
|
||
const weightedAverageBuilderRewardsPct = | ||
topFiveBuilders.reduce( | ||
(acc, { allocation, current }) => | ||
acc + Number(((allocation * 100n) / sumTotalAllocation) * BigInt(current)), | ||
0, | ||
) / 100 | ||
|
||
const totalAllocationInEther = Number(formatEther(sumTotalAllocation)) | ||
const rewardsPerStRIFPerCycle = | ||
(cyclePayout * (weightedAverageBuilderRewardsPct / totalAllocationInEther / rifPrice)) / 100 | ||
|
||
return (Math.pow(1 + rewardsPerStRIFPerCycle, 26) - 1) * 100 | ||
}, [backersRewardsPct, builders, prices, rbtcRewards, rifRewards, totalAllocation]) | ||
|
||
const isLoading = | ||
buildersLoading || | ||
rifRewardsLoading || | ||
rbtcRewardsLoading || | ||
cycleLoading || | ||
totalAllocationLoading || | ||
backersRewardsPctLoading | ||
|
||
const error = | ||
buildersError ?? | ||
rifRewardsError ?? | ||
rbtcRewardsError ?? | ||
totalAllocationError ?? | ||
cycleError ?? | ||
backersRewardsPctError | ||
|
||
return { | ||
data: abi, | ||
isLoading, | ||
error, | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/app/collective-rewards/metrics/components/ABIMetrics/index.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 @@ | ||
export * from './ABIMetrics' |
12 changes: 12 additions & 0 deletions
12
src/app/collective-rewards/metrics/hooks/useGetTotalAllocation.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 { 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, | ||
} | ||
} |
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
Oops, something went wrong.