From d6ff14b9a6f0863a02b20098ee6c324b5d5cdf91 Mon Sep 17 00:00:00 2001 From: Steven Yi Date: Mon, 19 Aug 2024 16:52:16 -0400 Subject: [PATCH 1/2] fix: adjusted values for EAY calculations and using new stake vs total user stake --- src/components/modals/StakingModal.tsx | 5 +++-- src/hooks/useRewardsInfo.ts | 19 ++++++++++--------- src/pages/Staking/DelegateStakeTable.tsx | 4 ++-- src/utils/rewards.ts | 9 ++++++--- tests/utils/rewards.test.ts | 4 ++-- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/components/modals/StakingModal.tsx b/src/components/modals/StakingModal.tsx index 7cc9f43..910fc25 100644 --- a/src/components/modals/StakingModal.tsx +++ b/src/components/modals/StakingModal.tsx @@ -110,7 +110,8 @@ const StakingModal = ({ tab == 0 ? currentStake + parseFloat(amountToStake) : currentStake - parseFloat(amountToUnstake); - const rewardsInfo = useRewardsInfo(gateway, newTotalStake); + const newStake = tab == 0 ? parseFloat(amountToStake) : -parseFloat(amountToUnstake); + const rewardsInfo = useRewardsInfo(gateway, newStake); const EAY = rewardsInfo && newTotalStake > 0 ? (rewardsInfo.EAY * 100).toLocaleString('en-us', { @@ -356,7 +357,7 @@ const StakingModal = ({ { +const useRewardsInfo = ( + gateway: AoGateway | undefined, + userStake: number +) => { const { data: gateways } = useGateways(); const { data: protocolBalance } = useProtocolBalance(); let res: UserRewards | undefined = undefined; - if ( - gateways && - gateway && - protocolBalance && - protocolBalance > 0 && - userStake > 0 - ) { + if (gateways && gateway && protocolBalance && protocolBalance > 0 && !isNaN(userStake)) { const numGateways = gateways ? Object.keys(gateways).length : 0; const gatewayRewards = calculateGatewayRewards( new mIOToken(protocolBalance).toIO(), numGateways, gateway, ); + + console.log(userStake) + const userRewards = calculateUserRewards( gatewayRewards, - new IOToken(userStake), + new IOToken(Math.abs(userStake)), + userStake < 0 ); res = userRewards; } diff --git a/src/pages/Staking/DelegateStakeTable.tsx b/src/pages/Staking/DelegateStakeTable.tsx index 6a99963..ddcf522 100644 --- a/src/pages/Staking/DelegateStakeTable.tsx +++ b/src/pages/Staking/DelegateStakeTable.tsx @@ -197,7 +197,7 @@ const DelegateStake = () => { id: 'eay', header: () => (
- EAY + Delegate EAY @@ -215,7 +215,7 @@ const DelegateStake = () => {
{row.original.eay < 0 ? 'N/A' - : `${formatWithCommas(row.original.eay)}%`} + : `${formatWithCommas(row.original.eay * 100)}%`}
), }), diff --git a/src/utils/rewards.ts b/src/utils/rewards.ts index 5bab799..2495ba4 100644 --- a/src/utils/rewards.ts +++ b/src/utils/rewards.ts @@ -1,8 +1,8 @@ import { AoGateway, IOToken, mIOToken } from '@ar.io/sdk/web'; const EPOCHS_PER_YEAR = 365; -const EPOCH_DISTRIBUTION_RATIO = 0.0025; // 0.25% -const GATEWAY_REWARDS_RATIO = 0.95; // 95% +const EPOCH_DISTRIBUTION_RATIO = 0.0005; // 0.05% +const GATEWAY_REWARDS_RATIO = 0.9; // 90% // const OBSERVER_REWARDS_RATIO = .05; // 5% export interface GatewayRewards { @@ -53,8 +53,11 @@ export const calculateGatewayRewards = ( export const calculateUserRewards = ( gatewayRewards: GatewayRewards, userDelegatedStake: IOToken, + removingStake = false ): UserRewards => { - const delegatedStake = userDelegatedStake.valueOf(); + const multiplier = removingStake ? -1 : 1; + const delegatedStake = userDelegatedStake.valueOf() * multiplier; + const stakeProportion = delegatedStake / (gatewayRewards.totalDelegatedStake.valueOf() + delegatedStake); diff --git a/tests/utils/rewards.test.ts b/tests/utils/rewards.test.ts index 17c839d..81664c2 100644 --- a/tests/utils/rewards.test.ts +++ b/tests/utils/rewards.test.ts @@ -26,9 +26,9 @@ describe('rewards.ts', () => { expect(result.totalDelegatedStake.valueOf()).toEqual( new IOToken(50000).valueOf(), ); - expect(result.rewardsSharedPerEpoch.valueOf()).toBeCloseTo(197.91, 1); + expect(result.rewardsSharedPerEpoch.valueOf()).toBeCloseTo(37.5, 1); expect(result.EEY).toBeCloseTo(0.004); - expect(result.EAY).toBeCloseTo(1.4447916691); + expect(result.EAY).toBeCloseTo(0.27375); }); }); From 13291cab71eb6aa6cdd002be294ed081bd0dbf44 Mon Sep 17 00:00:00 2001 From: Steven Yi Date: Mon, 19 Aug 2024 16:53:18 -0400 Subject: [PATCH 2/2] chore: remove unnecessary log --- src/hooks/useRewardsInfo.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/hooks/useRewardsInfo.ts b/src/hooks/useRewardsInfo.ts index 1b813c9..ff5528f 100644 --- a/src/hooks/useRewardsInfo.ts +++ b/src/hooks/useRewardsInfo.ts @@ -24,8 +24,6 @@ const useRewardsInfo = ( gateway, ); - console.log(userStake) - const userRewards = calculateUserRewards( gatewayRewards, new IOToken(Math.abs(userStake)),