diff --git a/src/pages/stakingPage/components/StakingCard.tsx b/src/pages/stakingPage/components/StakingCard.tsx index d075fa9..1747298 100644 --- a/src/pages/stakingPage/components/StakingCard.tsx +++ b/src/pages/stakingPage/components/StakingCard.tsx @@ -87,10 +87,9 @@ export const StakingCard: React.FC = ({ }, [address, tokenAddress]); const fetchAPR = useCallback(async () => { - if (!address) return; - const calculatedAPR = await calculateAPR(address); + const calculatedAPR = await calculateAPR(); setApr(calculatedAPR); - }, [address]); + }, []); useEffect(() => { void fetchBalance(); diff --git a/src/web3Config/staking/hooks.ts b/src/web3Config/staking/hooks.ts index 53a9068..f66ebef 100644 --- a/src/web3Config/staking/hooks.ts +++ b/src/web3Config/staking/hooks.ts @@ -246,31 +246,33 @@ export const getTotalStaked = async (userAddress: string) => { } }; //Staking APR Formula = (ETH rewards + esGS emissions annualized) / (Total GS + esGS staked) * 100 -export const calculateAPR = async (userAddress: string) => { - // Get ETH reward rate - const ethRewardRate = await getRewardRate(); - - const esGrixRewardRate = await getTokenPerInterval(); +export const calculateAPR = async () => { + // Get total staked amount using totalSupply from the reward tracker + const totalStaked = await readContract(wagmiConfig, { + abi: rewardTrackerAbi, + address: normalizeAddress(stakingContracts.rewardTracker.address), + functionName: 'totalSupply', + args: [], + }); - const totalStaked = await getTotalStaked(userAddress); - if (totalStaked === 0n) { - return 0; - } + if (totalStaked === 0n) return 0; - const SECONDS_PER_YEAR = 365n * 24n * 60n * 60n; + // Get ETH reward rate from rewardDistributor + const ethRewardRate = await getTokenPerInterval(); - // Calculate annualized rewards for both ETH and esGS - const ethRewardsPerYear = ethRewardRate * SECONDS_PER_YEAR; - const esGrixRewardsPerYear = esGrixRewardRate * SECONDS_PER_YEAR; + // Get esGrix emission rate (assuming it's from the reward distributor as well) + const esGrixRewardRate = await getTokenPerInterval(); - // Calculate total rewards (ETH + esGS) - const totalRewardsPerYear = ethRewardsPerYear + esGrixRewardsPerYear; + // Calculate annual rewards (seconds in a year * tokens per interval) + const SECONDS_PER_YEAR = 365n * 24n * 60n * 60n; + const annualEthRewards = ethRewardRate * SECONDS_PER_YEAR; + const annualEsGrixRewards = esGrixRewardRate * SECONDS_PER_YEAR; + const totalAnnualRewards = annualEthRewards + annualEsGrixRewards; - // Calculate APR including both reward types - const aprBips = (totalRewardsPerYear * 10000n) / totalStaked; - const aprPercentage = Number(aprBips) / 100; + // Calculate APR: (totalAnnualRewards / totalStaked) * 100 + const apr = (totalAnnualRewards * 100n) / (totalStaked as bigint); - return aprPercentage; + return Number(apr); }; // Add vesting allowance check export const checkVestingAllowance = async (owner: string, token: string) => {