Skip to content

Commit

Permalink
fix: dynamic reward calculations (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
daywiss authored May 18, 2023
1 parent 7c59fc1 commit dedd410
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
3 changes: 2 additions & 1 deletion components/HowItWorks/HowItWorks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function HowItWorks() {
isDelegate,
delegatorAddress,
isLoading: delegationDataIsLoading,
outstandingRewards,
} = useDelegationContext();
const stakingAddress = isDelegate ? delegatorAddress : userAddress;
const { data: stakedBalance, isLoading: stakedBalanceIsLoading } =
Expand All @@ -32,7 +33,7 @@ export function HowItWorks() {
useUnstakedBalance(stakingAddress);
const { data: stakerDetails, isLoading: stakerDetailsIsLoading } =
useStakerDetails(stakingAddress);
const { outstandingRewards, pendingUnstake } = stakerDetails || {};
const { pendingUnstake } = stakerDetails || {};
const {
data: votingAndStakingDetails,
isLoading: votingAndStakingDetailsIsLoading,
Expand Down
9 changes: 5 additions & 4 deletions components/Panel/ClaimPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ const minimumAmountClaimable = parseEtherSafe(".01");

export function ClaimPanel() {
const { votingWriter } = useContractsContext();
const { isDelegate, delegatorAddress } = useDelegationContext();
const { isDelegate, delegatorAddress, outstandingRewards } =
useDelegationContext();
const { address } = useAccountDetails();
const { withdrawRewardsMutation } = useWithdrawRewards("claim");
const { withdrawAndRestakeMutation } = useWithdrawAndRestake("claim");
const { data: stakerDetails, isLoading: stakerDetailsIsLoading } =
useStakerDetails(isDelegate ? delegatorAddress : address);
const { outstandingRewards } = stakerDetails || {};
const { isLoading: stakerDetailsIsLoading } = useStakerDetails(
isDelegate ? delegatorAddress : address
);
function withdrawRewards() {
if (!outstandingRewards || !votingWriter) return;

Expand Down
59 changes: 56 additions & 3 deletions contexts/DelegationContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getAddress, truncateEthAddress, zeroAddress } from "helpers";
import {
getAddress,
truncateEthAddress,
zeroAddress,
calculateOutstandingRewards,
} from "helpers";
import { BigNumber } from "ethers";
import {
useAcceptReceivedRequestToBeDelegate,
useAccountDetails,
Expand All @@ -9,16 +15,25 @@ import {
useDelegatorSetEventsForDelegator,
useIgnoreReceivedRequestToBeDelegate,
useIgnoredRequestToBeDelegateAddresses,
useInterval,
usePanelContext,
useReceivedRequestsToBeDelegate,
useRewardsCalculationInputs,
useSendRequestToBeDelegate,
useSentRequestsToBeDelegate,
useStakedBalance,
useStakerDetails,
useTerminateRelationshipWithDelegate,
useTerminateRelationshipWithDelegator,
useVoterFromDelegate,
} from "hooks";
import { ReactNode, createContext, useCallback, useMemo } from "react";
import {
ReactNode,
createContext,
useCallback,
useMemo,
useState,
} from "react";
import { DelegationEventT, DelegationStatusT } from "types";
export interface DelegationContextState {
delegationStatus: DelegationStatusT;
Expand Down Expand Up @@ -56,6 +71,7 @@ export interface DelegationContextState {
isAcceptingReceivedRequestToBeDelegate: boolean;
isTerminatingRelationshipWithDelegate: boolean;
isTerminatingRelationshipWithDelegator: boolean;
outstandingRewards: BigNumber;
}

export const defaultDelegationContextState: DelegationContextState = {
Expand Down Expand Up @@ -94,13 +110,17 @@ export const defaultDelegationContextState: DelegationContextState = {
isAcceptingReceivedRequestToBeDelegate: false,
isTerminatingRelationshipWithDelegate: false,
isTerminatingRelationshipWithDelegator: false,
outstandingRewards: BigNumber.from(0),
};

export const DelegationContext = createContext<DelegationContextState>(
defaultDelegationContextState
);

export function DelegationProvider({ children }: { children: ReactNode }) {
const [outstandingRewards, setOutstandingRewards] = useState(
BigNumber.from(0)
);
const { address } = useAccountDetails();
const {
data: receivedRequestsToBeDelegate,
Expand Down Expand Up @@ -148,9 +168,9 @@ export function DelegationProvider({ children }: { children: ReactNode }) {
terminateRelationshipWithDelegatorMutation,
isTerminatingRelationshipWithDelegator,
} = useTerminateRelationshipWithDelegator(address);
const { votingWriter } = useContractsContext();
const { data: stakerDetails } = useStakerDetails(address);
const { delegate } = stakerDetails ?? {};
const { votingWriter } = useContractsContext();
const { closePanel } = usePanelContext();
const pendingReceivedRequestsToBeDelegate =
getPendingReceivedRequestsToBeDelegate();
Expand All @@ -168,6 +188,10 @@ export function DelegationProvider({ children }: { children: ReactNode }) {
const isDelegator = delegationStatus === "delegator";
const delegatorAddress = isDelegate ? getDelegatorAddress() : undefined;
const delegateAddress = getDelegateAddress();
const stakerAddress = delegatorAddress ?? address;
// this makes sure to look up staker details by delegator if you are delegatee
const { data: stakerAddressStakerDetails } = useStakerDetails(stakerAddress);
const { data: stakerAddressStakedBalance } = useStakedBalance(stakerAddress);
const isLoading =
receivedRequestsToBeDelegateLoading ||
sentRequestsToBeDelegateLoading ||
Expand Down Expand Up @@ -388,6 +412,33 @@ export function DelegationProvider({ children }: { children: ReactNode }) {
});
}, [terminateRelationshipWithDelegateMutation, votingWriter]);

const { data: rewardCalculationInputs } = useRewardsCalculationInputs();

useInterval(() => {
updateOutstandingRewards();
}, 100);

function updateOutstandingRewards() {
if (rewardCalculationInputs === undefined) return;
if (stakerAddressStakerDetails === undefined) return;
if (stakerAddressStakedBalance === undefined) return;
const { emissionRate, rewardPerTokenStored, cumulativeStake, updateTime } =
rewardCalculationInputs;
const { outstandingRewards: voterOutstandingRewards, rewardsPaidPerToken } =
stakerAddressStakerDetails;
if (voterOutstandingRewards === undefined) return;
const calculatedOutstandingRewards = calculateOutstandingRewards({
voterOutstandingRewards,
stakedBalance: stakerAddressStakedBalance,
rewardsPaidPerToken,
cumulativeStake,
rewardPerTokenStored,
updateTime,
emissionRate,
});
setOutstandingRewards(calculatedOutstandingRewards);
}

const value = useMemo(
() => ({
delegationStatus,
Expand Down Expand Up @@ -425,6 +476,7 @@ export function DelegationProvider({ children }: { children: ReactNode }) {
isAcceptingReceivedRequestToBeDelegate,
isTerminatingRelationshipWithDelegate,
isTerminatingRelationshipWithDelegator,
outstandingRewards,
}),
[
acceptReceivedRequestToBeDelegate,
Expand Down Expand Up @@ -462,6 +514,7 @@ export function DelegationProvider({ children }: { children: ReactNode }) {
terminateRelationshipWithDelegate,
terminateRelationshipWithDelegator,
voterFromDelegateLoading,
outstandingRewards,
]
);
return (
Expand Down

2 comments on commit dedd410

@vercel
Copy link

@vercel vercel bot commented on dedd410 May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on dedd410 May 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.