Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/pages/stakingPage/components/StakingCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,9 @@ export const StakingCard: React.FC<StakingCardProps> = ({
}, [address, tokenAddress]);

const fetchAPR = useCallback(async () => {
if (!address) return;
const calculatedAPR = await calculateAPR(address);
const calculatedAPR = await calculateAPR();
setApr(calculatedAPR);
}, [address]);
}, []);

useEffect(() => {
void fetchBalance();
Expand Down
40 changes: 21 additions & 19 deletions src/web3Config/staking/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down