Skip to content

Commit

Permalink
Merge pull request #1164 from interlay/tom/fix/staking-rewards-edge-case
Browse files Browse the repository at this point in the history
Tom/fix/staking rewards edge case
  • Loading branch information
tomjeatt committed Apr 28, 2023
2 parents 6f97214 + 1622590 commit d59c8bd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/config/relay-chains.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ switch (process.env.REACT_APP_RELAY_CHAIN_NAME) {
CROWDLOAN_LINK = INTERLAY_CROWDLOAN_LINK;
OPEN_GRAPH_IMAGE_FILE_NAME = 'interlay-meta-image.jpg';
STAKE_LOCK_TIME = {
MIN: 0,
MIN: 1,
MAX: 192
};
// TODO: temporary
Expand Down Expand Up @@ -191,7 +191,7 @@ switch (process.env.REACT_APP_RELAY_CHAIN_NAME) {
CROWDLOAN_LINK = KINTSUGI_CROWDLOAN_LINK;
OPEN_GRAPH_IMAGE_FILE_NAME = 'kintsugi-meta-image.jpg';
STAKE_LOCK_TIME = {
MIN: 0,
MIN: 1,
MAX: 96
};
// TODO: temporary
Expand Down
30 changes: 14 additions & 16 deletions src/pages/Staking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const Staking = (): JSX.Element => {
handleSubmit,
watch,
reset,
formState: { errors, isDirty, isValid },
formState: { errors, isValid },
trigger,
setValue
} = useForm<StakingFormData>({
Expand Down Expand Up @@ -197,7 +197,6 @@ const Staking = (): JSX.Element => {
// Estimated governance token Rewards & APY
const monetaryLockingAmount = newMonetaryAmount(lockingAmount, GOVERNANCE_TOKEN, true);
const {
isIdle: estimatedRewardAmountAndAPYIdle,
isLoading: estimatedRewardAmountAndAPYLoading,
data: estimatedRewardAmountAndAPY,
error: estimatedRewardAmountAndAPYError,
Expand All @@ -219,13 +218,6 @@ const Staking = (): JSX.Element => {
);
useErrorHandler(estimatedRewardAmountAndAPYError);

// MEMO: This is being set outside of a useEffect because of
// an race condition. This is a underlying issue with the
// component and can't be easily fixed.
if (isValid || !isDirty) {
estimatedRewardAmountAndAPYRefetch();
}

const {
isIdle: stakedAmountAndEndBlockIdle,
isLoading: stakedAmountAndEndBlockLoading,
Expand Down Expand Up @@ -319,6 +311,13 @@ const Staking = (): JSX.Element => {
}
);

React.useEffect(() => {
if (!isValid) return;
if (!estimatedRewardAmountAndAPYRefetch) return;

estimatedRewardAmountAndAPYRefetch();
}, [isValid, monetaryLockingAmount, blockLockTimeExtension, estimatedRewardAmountAndAPYRefetch]);

React.useEffect(() => {
if (!lockTime) return;
if (!currentBlockNumber) return;
Expand Down Expand Up @@ -583,7 +582,7 @@ const Staking = (): JSX.Element => {

const renderNewVoteGovernanceTokenGainedLabel = () => {
const newTotalStakeAmount = getNewTotalStake();
if (voteGovernanceTokenBalance === undefined || newTotalStakeAmount === undefined) {
if (voteGovernanceTokenBalance === undefined || newTotalStakeAmount === undefined || !isValid) {
return '-';
}

Expand All @@ -595,7 +594,7 @@ const Staking = (): JSX.Element => {
};

const getNewTotalStake = () => {
if (remainingBlockNumbersToUnstake === undefined || stakedAmount === undefined) {
if (remainingBlockNumbersToUnstake === undefined || stakedAmount === undefined || !isValid) {
return undefined;
}

Expand Down Expand Up @@ -633,31 +632,31 @@ const Staking = (): JSX.Element => {

const renderEstimatedAPYLabel = () => {
if (
estimatedRewardAmountAndAPYIdle ||
estimatedRewardAmountAndAPYLoading ||
!projectedRewardAmountAndAPY ||
errors[LOCK_TIME] ||
errors[LOCKING_AMOUNT]
) {
return '-';
}
if (estimatedRewardAmountAndAPY === undefined) {
throw new Error('Something went wrong!');
return formatPercentage(projectedRewardAmountAndAPY.apy.toNumber());
}

return formatPercentage(estimatedRewardAmountAndAPY.apy.toNumber());
};

const renderEstimatedRewardAmountLabel = () => {
if (
estimatedRewardAmountAndAPYIdle ||
estimatedRewardAmountAndAPYLoading ||
!projectedRewardAmountAndAPY ||
errors[LOCK_TIME] ||
errors[LOCKING_AMOUNT]
) {
return '-';
}
if (estimatedRewardAmountAndAPY === undefined) {
throw new Error('Something went wrong!');
return `${displayMonetaryAmount(projectedRewardAmountAndAPY.amount)} ${GOVERNANCE_TOKEN_SYMBOL}`;
}

return `${displayMonetaryAmount(estimatedRewardAmountAndAPY.amount)} ${GOVERNANCE_TOKEN_SYMBOL}`;
Expand Down Expand Up @@ -714,7 +713,6 @@ const Staking = (): JSX.Element => {
claimableRewardAmountLoading ||
projectedRewardAmountAndAPYIdle ||
projectedRewardAmountAndAPYLoading ||
estimatedRewardAmountAndAPYIdle ||
estimatedRewardAmountAndAPYLoading ||
stakedAmountAndEndBlockIdle ||
stakedAmountAndEndBlockLoading;
Expand Down

2 comments on commit d59c8bd

@vercel
Copy link

@vercel vercel bot commented on d59c8bd Apr 28, 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 d59c8bd Apr 28, 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.