From cbcc72071bfa9da1f6c5e14d5236b283e87e236e Mon Sep 17 00:00:00 2001 From: Thomas Jeatt Date: Thu, 27 Apr 2023 15:57:31 +0100 Subject: [PATCH 1/6] refactor: disable second rewards api call and set initial value from existing rewards --- src/pages/Staking/index.tsx | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/pages/Staking/index.tsx b/src/pages/Staking/index.tsx index c95bbb604b..0d340f7c7d 100644 --- a/src/pages/Staking/index.tsx +++ b/src/pages/Staking/index.tsx @@ -124,7 +124,7 @@ const Staking = (): JSX.Element => { handleSubmit, watch, reset, - formState: { errors, isDirty, isValid }, + formState: { errors, isValid }, trigger, setValue } = useForm({ @@ -197,11 +197,10 @@ 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, - refetch: estimatedRewardAmountAndAPYRefetch + error: estimatedRewardAmountAndAPYError + // refetch: estimatedRewardAmountAndAPYRefetch } = useQuery( [ GENERIC_FETCHER, @@ -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, @@ -633,15 +625,15 @@ 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()); @@ -649,15 +641,15 @@ const Staking = (): JSX.Element => { 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}`; @@ -714,7 +706,6 @@ const Staking = (): JSX.Element => { claimableRewardAmountLoading || projectedRewardAmountAndAPYIdle || projectedRewardAmountAndAPYLoading || - estimatedRewardAmountAndAPYIdle || estimatedRewardAmountAndAPYLoading || stakedAmountAndEndBlockIdle || stakedAmountAndEndBlockLoading; From 20defe18f4d99519f5e1b7ab77f47939eea5492a Mon Sep 17 00:00:00 2001 From: Thomas Jeatt Date: Thu, 27 Apr 2023 16:02:33 +0100 Subject: [PATCH 2/6] fix: only estimate reward amount when form is valid --- src/pages/Staking/index.tsx | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/pages/Staking/index.tsx b/src/pages/Staking/index.tsx index 0d340f7c7d..1faf33722e 100644 --- a/src/pages/Staking/index.tsx +++ b/src/pages/Staking/index.tsx @@ -199,8 +199,8 @@ const Staking = (): JSX.Element => { const { isLoading: estimatedRewardAmountAndAPYLoading, data: estimatedRewardAmountAndAPY, - error: estimatedRewardAmountAndAPYError - // refetch: estimatedRewardAmountAndAPYRefetch + error: estimatedRewardAmountAndAPYError, + refetch: estimatedRewardAmountAndAPYRefetch } = useQuery( [ GENERIC_FETCHER, @@ -311,6 +311,13 @@ const Staking = (): JSX.Element => { } ); + React.useEffect(() => { + if (!isValid) return; + if (!estimatedRewardAmountAndAPYRefetch) return; + + estimatedRewardAmountAndAPYRefetch(); + }, [isValid, estimatedRewardAmountAndAPYRefetch]); + React.useEffect(() => { if (!lockTime) return; if (!currentBlockNumber) return; From 118674db81b7add5601002be5079e732ea1b84ab Mon Sep 17 00:00:00 2001 From: Thomas Jeatt Date: Thu, 27 Apr 2023 16:13:13 +0100 Subject: [PATCH 3/6] fix: don't show new vKint gained when form state is invalid --- src/pages/Staking/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/Staking/index.tsx b/src/pages/Staking/index.tsx index 1faf33722e..33aa8dd1b5 100644 --- a/src/pages/Staking/index.tsx +++ b/src/pages/Staking/index.tsx @@ -582,7 +582,7 @@ const Staking = (): JSX.Element => { const renderNewVoteGovernanceTokenGainedLabel = () => { const newTotalStakeAmount = getNewTotalStake(); - if (voteGovernanceTokenBalance === undefined || newTotalStakeAmount === undefined) { + if (voteGovernanceTokenBalance === undefined || newTotalStakeAmount === undefined || !isValid) { return '-'; } From 038fcb1ad7e76e3cbd66e5346734641fc793b555 Mon Sep 17 00:00:00 2001 From: Thomas Jeatt Date: Thu, 27 Apr 2023 16:20:38 +0100 Subject: [PATCH 4/6] fix: revert config change which introduced validation regression --- src/config/relay-chains.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config/relay-chains.tsx b/src/config/relay-chains.tsx index 948553f231..d358c50be6 100644 --- a/src/config/relay-chains.tsx +++ b/src/config/relay-chains.tsx @@ -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 @@ -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 From 4be2add4186b0aba59d90c7514f98fcbccddb5aa Mon Sep 17 00:00:00 2001 From: Thomas Jeatt Date: Fri, 28 Apr 2023 09:11:57 +0100 Subject: [PATCH 5/6] fix: only calculate newTotalStake if form is valid --- src/pages/Staking/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/Staking/index.tsx b/src/pages/Staking/index.tsx index 33aa8dd1b5..fb1f2fea3f 100644 --- a/src/pages/Staking/index.tsx +++ b/src/pages/Staking/index.tsx @@ -594,7 +594,7 @@ const Staking = (): JSX.Element => { }; const getNewTotalStake = () => { - if (remainingBlockNumbersToUnstake === undefined || stakedAmount === undefined) { + if (remainingBlockNumbersToUnstake === undefined || stakedAmount === undefined || !isValid) { return undefined; } From 1622590b18c8b2db0b622f7e31917544aa2fcaaf Mon Sep 17 00:00:00 2001 From: Thomas Jeatt Date: Fri, 28 Apr 2023 09:46:48 +0100 Subject: [PATCH 6/6] fix: correct dependency array for estimating stake rewards --- src/pages/Staking/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/Staking/index.tsx b/src/pages/Staking/index.tsx index fb1f2fea3f..bff794e201 100644 --- a/src/pages/Staking/index.tsx +++ b/src/pages/Staking/index.tsx @@ -316,7 +316,7 @@ const Staking = (): JSX.Element => { if (!estimatedRewardAmountAndAPYRefetch) return; estimatedRewardAmountAndAPYRefetch(); - }, [isValid, estimatedRewardAmountAndAPYRefetch]); + }, [isValid, monetaryLockingAmount, blockLockTimeExtension, estimatedRewardAmountAndAPYRefetch]); React.useEffect(() => { if (!lockTime) return;