Skip to content

Commit 0f16c7a

Browse files
committed
fix: handle undefined values
1 parent 44b00b3 commit 0f16c7a

File tree

2 files changed

+129
-65
lines changed

2 files changed

+129
-65
lines changed

web/src/pages/Courts/CourtDetails/StakePanel/SimulatorPopup/QuantityToSimulate.tsx

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import React from "react";
22
import styled from "styled-components";
3+
import Skeleton from "react-loading-skeleton";
34

45
import { commify } from "utils/commify";
6+
import { isUndefined } from "utils/index";
57

68
import WithHelpTooltip from "components/WithHelpTooltip";
79

@@ -42,40 +44,62 @@ const StyledMathematicalOperation = styled.p`
4244
`;
4345

4446
interface IQuantityToSimulate {
47+
jurorCurrentEffectiveStake: number | undefined;
48+
jurorCurrentSpecificStake: number | undefined;
4549
isStaking: boolean;
46-
currentEffectiveStake: number;
47-
currentSpecificStake: number;
4850
amountToStake: number;
4951
}
5052

5153
const QuantityToSimulate: React.FC<IQuantityToSimulate> = ({
5254
isStaking,
53-
currentEffectiveStake,
54-
currentSpecificStake,
55+
jurorCurrentEffectiveStake,
56+
jurorCurrentSpecificStake,
5557
amountToStake,
5658
}) => {
59+
const effectiveStakeDisplay = !isUndefined(jurorCurrentEffectiveStake) ? (
60+
`${commify(jurorCurrentEffectiveStake)} PNK`
61+
) : (
62+
<Skeleton width={50} />
63+
);
64+
65+
const amountStakedInThisCourt = !isUndefined(jurorCurrentSpecificStake)
66+
? `${commify(jurorCurrentSpecificStake)} PNK`
67+
: "...";
68+
69+
const amountStakedInSubCourts =
70+
!isUndefined(jurorCurrentEffectiveStake) && !isUndefined(jurorCurrentSpecificStake)
71+
? `${commify(jurorCurrentEffectiveStake - jurorCurrentSpecificStake)} PNK`
72+
: "...";
73+
74+
const finalQuantityValue =
75+
!isUndefined(jurorCurrentEffectiveStake) && !isUndefined(amountToStake)
76+
? isStaking
77+
? jurorCurrentEffectiveStake + amountToStake
78+
: jurorCurrentEffectiveStake - amountToStake
79+
: undefined;
80+
81+
const finalQuantityDisplay = !isUndefined(finalQuantityValue) ? (
82+
`${commify(finalQuantityValue)} PNK`
83+
) : (
84+
<Skeleton width={50} />
85+
);
86+
5787
return (
5888
<Container>
59-
<Quantity>{commify(currentEffectiveStake)} PNK</Quantity>
89+
<Quantity>{effectiveStakeDisplay}</Quantity>
6090
<TextWithTooltipContainer>
6191
<WithHelpTooltip
62-
tooltipMsg={`Current Stake (Sum of): Amount of PNK staked in this court (${commify(
63-
currentSpecificStake
64-
)} PNK); Amount of PNK staked on its sub-courts (${commify(
65-
currentEffectiveStake - currentSpecificStake
66-
)} PNK)`}
92+
tooltipMsg={`Current Stake (Sum of): Amount of PNK staked in this court (${amountStakedInThisCourt}); Amount of PNK staked on its sub-courts (${amountStakedInSubCourts})`}
6793
>
6894
Current Stake
6995
</WithHelpTooltip>
7096
</TextWithTooltipContainer>
7197
<StyledMathematicalOperation>{isStaking ? "+" : "-"}</StyledMathematicalOperation>
7298
<Quantity>{commify(amountToStake)} PNK</Quantity>
7399
<StyledMathematicalOperation>=</StyledMathematicalOperation>
74-
<FinalQuantity>
75-
{isStaking ? commify(currentEffectiveStake + amountToStake) : commify(currentEffectiveStake - amountToStake)}{" "}
76-
PNK
77-
</FinalQuantity>
100+
<FinalQuantity>{finalQuantityDisplay}</FinalQuantity>
78101
</Container>
79102
);
80103
};
104+
81105
export default QuantityToSimulate;

web/src/pages/Courts/CourtDetails/StakePanel/SimulatorPopup/index.tsx

Lines changed: 91 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,12 @@ const SimulatorPopup: React.FC<ISimulatorPopup> = ({ amountToStake, isStaking })
139139
const { address } = useAccount();
140140
const { data: stakeData } = useJurorStakeDetailsQuery(address?.toLowerCase() as `0x${string}`);
141141
const courtStakeData = stakeData?.jurorTokensPerCourts?.find(({ court }) => court.id === id);
142-
const currentEffectiveStake = !isUndefined(courtStakeData) ? Number(formatEther(courtStakeData.effectiveStake)) : 0;
143-
const currentSpecificStake = !isUndefined(courtStakeData) ? Number(formatEther(courtStakeData.staked)) : 0;
142+
const jurorCurrentEffectiveStake = !isUndefined(courtStakeData)
143+
? Number(formatEther(courtStakeData.effectiveStake))
144+
: undefined;
145+
const jurorCurrentSpecificStake = !isUndefined(courtStakeData)
146+
? Number(formatEther(courtStakeData.staked))
147+
: undefined;
144148

145149
const timeframedCourtData = useHomePageExtraStats(30);
146150
const { prices: pricesData } = useCoinPrice([CoinIds.ETH, CoinIds.PNK]);
@@ -150,72 +154,108 @@ const SimulatorPopup: React.FC<ISimulatorPopup> = ({ amountToStake, isStaking })
150154
return timeframedCourtData?.data?.courts?.find((c) => c.id === id);
151155
}, [timeframedCourtData, id]);
152156

153-
const effectiveStakeAsNumber = foundCourt ? Number(foundCourt.effectiveStake) / 1e18 : 0;
154-
155-
const currentTreeVotesPerPnk = foundCourt ? foundCourt.treeVotesPerPnk : 0;
156-
const currentTreeDisputesPerPnk = foundCourt ? foundCourt.treeDisputesPerPnk : 0;
157-
const currentTreeExpectedRewardPerPnk = foundCourt ? foundCourt.treeExpectedRewardPerPnk : 0;
158-
159-
const totalVotes = effectiveStakeAsNumber * currentTreeVotesPerPnk;
160-
const totalCases = effectiveStakeAsNumber * currentTreeDisputesPerPnk;
161-
const totalRewards = effectiveStakeAsNumber * currentTreeExpectedRewardPerPnk;
162-
163-
const futureTotalEffectiveStake = Math.max(
164-
isStaking ? effectiveStakeAsNumber + amountToStake : effectiveStakeAsNumber - amountToStake,
165-
0
166-
);
167-
168-
const futureTreeVotesPerPnk = futureTotalEffectiveStake !== 0 ? totalVotes / futureTotalEffectiveStake : 0;
169-
const futureTreeDisputesPerPnk = futureTotalEffectiveStake !== 0 ? totalCases / futureTotalEffectiveStake : 0;
170-
const futureTreeExpectedRewardPerPnk = futureTotalEffectiveStake !== 0 ? totalRewards / futureTotalEffectiveStake : 0;
171-
172-
const futureEffectiveStake = Math.max(
173-
isStaking ? currentEffectiveStake + amountToStake : currentEffectiveStake - amountToStake,
174-
0
175-
);
176-
177-
const currentExpectedVotes = beautifyStatNumber(currentEffectiveStake * currentTreeVotesPerPnk);
178-
const futureExpectedVotes = beautifyStatNumber(futureEffectiveStake * futureTreeVotesPerPnk);
179-
180-
const currentExpectedCases = beautifyStatNumber(currentEffectiveStake * currentTreeDisputesPerPnk);
181-
const futureExpectedCases = beautifyStatNumber(futureEffectiveStake * futureTreeDisputesPerPnk);
157+
const courtCurrentEffectiveStake = foundCourt ? Number(foundCourt.effectiveStake) / 1e18 : undefined;
158+
159+
const currentTreeVotesPerPnk = foundCourt?.treeVotesPerPnk;
160+
const currentTreeDisputesPerPnk = foundCourt?.treeDisputesPerPnk;
161+
const currentTreeExpectedRewardPerPnk = foundCourt?.treeExpectedRewardPerPnk;
162+
163+
const totalVotes =
164+
!isUndefined(courtCurrentEffectiveStake) && !isUndefined(currentTreeVotesPerPnk)
165+
? courtCurrentEffectiveStake * currentTreeVotesPerPnk
166+
: undefined;
167+
const totalCases =
168+
!isUndefined(courtCurrentEffectiveStake) && !isUndefined(currentTreeDisputesPerPnk)
169+
? courtCurrentEffectiveStake * currentTreeDisputesPerPnk
170+
: undefined;
171+
const totalRewards =
172+
!isUndefined(courtCurrentEffectiveStake) && !isUndefined(currentTreeExpectedRewardPerPnk)
173+
? courtCurrentEffectiveStake * currentTreeExpectedRewardPerPnk
174+
: undefined;
175+
176+
const courtFutureEffectiveStake = !isUndefined(courtCurrentEffectiveStake)
177+
? Math.max(isStaking ? courtCurrentEffectiveStake + amountToStake : courtCurrentEffectiveStake - amountToStake, 0)
178+
: undefined;
179+
180+
const futureTreeVotesPerPnk =
181+
!isUndefined(courtFutureEffectiveStake) && !isUndefined(totalVotes)
182+
? totalVotes / courtFutureEffectiveStake
183+
: undefined;
184+
const futureTreeDisputesPerPnk =
185+
!isUndefined(courtFutureEffectiveStake) && !isUndefined(totalCases)
186+
? totalCases / courtFutureEffectiveStake
187+
: undefined;
188+
const futureTreeExpectedRewardPerPnk =
189+
!isUndefined(courtFutureEffectiveStake) && !isUndefined(totalRewards)
190+
? totalRewards / courtFutureEffectiveStake
191+
: undefined;
192+
193+
const jurorFutureEffectiveStake = !isUndefined(jurorCurrentEffectiveStake)
194+
? Math.max(isStaking ? jurorCurrentEffectiveStake + amountToStake : jurorCurrentEffectiveStake - amountToStake, 0)
195+
: undefined;
196+
197+
const currentExpectedVotes =
198+
!isUndefined(jurorCurrentEffectiveStake) && !isUndefined(currentTreeVotesPerPnk)
199+
? beautifyStatNumber(jurorCurrentEffectiveStake * currentTreeVotesPerPnk)
200+
: undefined;
201+
const futureExpectedVotes =
202+
!isUndefined(jurorFutureEffectiveStake) && !isUndefined(futureTreeVotesPerPnk)
203+
? beautifyStatNumber(jurorFutureEffectiveStake * futureTreeVotesPerPnk)
204+
: undefined;
205+
206+
const currentExpectedCases =
207+
!isUndefined(jurorCurrentEffectiveStake) && !isUndefined(currentTreeDisputesPerPnk)
208+
? beautifyStatNumber(jurorCurrentEffectiveStake * currentTreeDisputesPerPnk)
209+
: undefined;
210+
const futureExpectedCases =
211+
!isUndefined(jurorFutureEffectiveStake) && !isUndefined(futureTreeDisputesPerPnk)
212+
? beautifyStatNumber(jurorFutureEffectiveStake * futureTreeDisputesPerPnk)
213+
: undefined;
182214

183215
const currentDrawingOdds =
184-
effectiveStakeAsNumber && calculateJurorOdds(currentEffectiveStake, effectiveStakeAsNumber);
216+
!isUndefined(jurorCurrentEffectiveStake) && !isUndefined(courtCurrentEffectiveStake)
217+
? calculateJurorOdds(jurorCurrentEffectiveStake, courtCurrentEffectiveStake)
218+
: undefined;
185219
const futureDrawingOdds =
186-
effectiveStakeAsNumber && calculateJurorOdds(futureEffectiveStake, futureTotalEffectiveStake);
187-
188-
const currentExpectedRewardsUSD = formatUSD(
189-
currentEffectiveStake * currentTreeExpectedRewardPerPnk * (ethPriceUSD || 0)
190-
);
191-
const futureExpectedRewardsUSD = formatUSD(
192-
futureEffectiveStake * futureTreeExpectedRewardPerPnk * (ethPriceUSD || 0)
193-
);
220+
!isUndefined(jurorFutureEffectiveStake) && !isUndefined(courtFutureEffectiveStake)
221+
? calculateJurorOdds(jurorFutureEffectiveStake, courtFutureEffectiveStake)
222+
: undefined;
223+
224+
const currentExpectedRewardsUSD =
225+
!isUndefined(jurorCurrentEffectiveStake) &&
226+
!isUndefined(currentTreeExpectedRewardPerPnk) &&
227+
!isUndefined(ethPriceUSD)
228+
? formatUSD(jurorCurrentEffectiveStake * currentTreeExpectedRewardPerPnk * ethPriceUSD)
229+
: undefined;
230+
const futureExpectedRewardsUSD =
231+
!isUndefined(jurorFutureEffectiveStake) && !isUndefined(futureTreeExpectedRewardPerPnk) && !isUndefined(ethPriceUSD)
232+
? formatUSD(jurorFutureEffectiveStake * futureTreeExpectedRewardPerPnk * ethPriceUSD)
233+
: undefined;
194234

195235
const simulatorItems = [
196236
{
197237
title: "Votes",
198238
icon: <GavelIcon />,
199-
currentValue: `${currentExpectedVotes}`,
200-
futureValue: `${futureExpectedVotes}`,
239+
currentValue: currentExpectedVotes,
240+
futureValue: futureExpectedVotes,
201241
},
202242
{
203243
title: "Cases",
204244
icon: <LawBalanceIcon />,
205-
currentValue: `${currentExpectedCases}`,
206-
futureValue: `${futureExpectedCases}`,
245+
currentValue: currentExpectedCases,
246+
futureValue: futureExpectedCases,
207247
},
208248
{
209249
title: "Drawing Odds",
210250
icon: <DiceIcon />,
211-
currentValue: `${currentDrawingOdds}`,
212-
futureValue: `${futureDrawingOdds}`,
251+
currentValue: currentDrawingOdds,
252+
futureValue: futureDrawingOdds,
213253
},
214254
{
215255
title: "Rewards",
216256
icon: <DollarIcon />,
217-
currentValue: `${currentExpectedRewardsUSD}`,
218-
futureValue: `${futureExpectedRewardsUSD}`,
257+
currentValue: currentExpectedRewardsUSD,
258+
futureValue: futureExpectedRewardsUSD,
219259
tooltipMsg:
220260
"Estimated rewards in USD, assuming 100% coherent voting. If other jurors vote incoherently, additional rewards in the form of PNK tokens may be earned beyond this estimate.",
221261
},
@@ -225,7 +265,7 @@ const SimulatorPopup: React.FC<ISimulatorPopup> = ({ amountToStake, isStaking })
225265
<Container>
226266
<Header />
227267
<Divider />
228-
<QuantityToSimulate {...{ currentEffectiveStake, currentSpecificStake, isStaking, amountToStake }} />
268+
<QuantityToSimulate {...{ jurorCurrentEffectiveStake, jurorCurrentSpecificStake, isStaking, amountToStake }} />
229269
<ItemsContainer>
230270
{simulatorItems.map((item, index) => (
231271
<SimulatorItem key={index}>
@@ -241,14 +281,14 @@ const SimulatorPopup: React.FC<ISimulatorPopup> = ({ amountToStake, isStaking })
241281
</LeftContent>
242282
<RightContent>
243283
<StyledCurrentValue>
244-
{!item.currentValue.includes("undefined") ? item.currentValue : <Skeleton width={32} />}
284+
{!isUndefined(item.currentValue) ? item.currentValue : <Skeleton width={32} />}
245285
</StyledCurrentValue>
246286
<StyledArrowRightIcon {...{ isStaking }} />
247287
<StyledFutureValue>
248288
{!amountToStake || amountToStake === 0 ? "Enter amount" : null}
249289
{!isUndefined(amountToStake) &&
250290
amountToStake > 0 &&
251-
(!item.futureValue.includes("undefined") ? item.futureValue : <Skeleton width={32} />)}
291+
(!isUndefined(item.futureValue) ? item.futureValue : <Skeleton width={32} />)}
252292
</StyledFutureValue>
253293
</RightContent>
254294
</SimulatorItem>

0 commit comments

Comments
 (0)