-
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 * refactor: pr comments --------- Co-authored-by: Francisco Tobar <[email protected]>
- Loading branch information
1 parent
6d83922
commit 3d6855f
Showing
15 changed files
with
230 additions
and
38 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> | ||
</> | ||
) | ||
} |
120 changes: 120 additions & 0 deletions
120
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,120 @@ | ||
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, parseUnits } 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: bigint }>>((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) | ||
|
||
// We use the multiplication with the current backer rewards % to avoid losing precision | ||
// Thats why we don't need to multiply by 100 | ||
const weightedAverageBuilderRewardsPct = topFiveBuilders.reduce( | ||
(acc, { allocation, current }) => acc + (allocation * current) / sumTotalAllocation, | ||
0n, | ||
) | ||
const weightedAverageBuilderRewardsPctInEther = Number(formatEther(weightedAverageBuilderRewardsPct)) | ||
|
||
const totalAllocationInEther = Number(formatEther(sumTotalAllocation)) | ||
const rewardsPerStRIFPerCycle = | ||
cyclePayout * (weightedAverageBuilderRewardsPctInEther / totalAllocationInEther) | ||
|
||
return (Math.pow(1 + rewardsPerStRIFPerCycle / rifPrice, 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.