Skip to content

Commit 23ba21b

Browse files
authored
Merge pull request #948 from kleros/fix(web)/fix-assertions-undefined
refactor: started assertion refactoring
2 parents 3288285 + 1e03469 commit 23ba21b

File tree

6 files changed

+38
-29
lines changed

6 files changed

+38
-29
lines changed

web/src/pages/Cases/CaseDetails/Appeal/Classic/Fund.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ import {
1212
useSelectedOptionContext,
1313
useFundingContext,
1414
} from "hooks/useClassicAppealContext";
15-
import { notUndefined } from "utils/index";
15+
import { isUndefined } from "utils/index";
1616

1717
const Fund: React.FC = () => {
1818
const loserSideCountdown = useLoserSideCountdownContext();
1919
const { fundedChoices, winningChoice } = useFundingContext();
2020
const needFund =
21-
notUndefined([loserSideCountdown, fundedChoices]) &&
22-
(loserSideCountdown! > 0 || (fundedChoices!.length > 0 && !fundedChoices?.includes(winningChoice!)));
21+
(loserSideCountdown ?? 0) > 0 ||
22+
(!isUndefined(fundedChoices) &&
23+
!isUndefined(winningChoice) &&
24+
fundedChoices.length > 0 &&
25+
!fundedChoices.includes(winningChoice));
2326
const { id } = useParams();
2427
const { address, isDisconnected } = useAccount();
2528
const { data: balance } = useBalance({
@@ -33,8 +36,8 @@ const Fund: React.FC = () => {
3336
const [isSending, setIsSending] = useState(false);
3437
const { selectedOption } = useSelectedOptionContext();
3538
const { config: fundAppealConfig } = usePrepareDisputeKitClassicFundAppeal({
36-
enabled: notUndefined([id, selectedOption]),
37-
args: [BigInt(id!), BigInt(selectedOption!)],
39+
enabled: !isUndefined(id) && !isUndefined(selectedOption),
40+
args: [BigInt(id ?? 0), BigInt(selectedOption ?? 0)],
3841
value: parsedAmount,
3942
});
4043
const { writeAsync: fundAppeal } = useDisputeKitClassicFundAppeal(fundAppealConfig);

web/src/pages/Cases/CaseDetails/Appeal/Classic/Options/StageTwo.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ import React, { useEffect } from "react";
22
import styled from "styled-components";
33
import OptionCard from "../../OptionCard";
44
import { useFundingContext, useOptionsContext, useSelectedOptionContext } from "hooks/useClassicAppealContext";
5-
import { notUndefined } from "utils/index";
5+
import { isUndefined } from "utils/index";
66

77
const StageOne: React.FC = () => {
88
const { paidFees, winningChoice, winnerRequiredFunding, fundedChoices } = useFundingContext();
99
const options = useOptionsContext();
1010
const { selectedOption, setSelectedOption } = useSelectedOptionContext();
1111
useEffect(() => {
12-
if (notUndefined(winningChoice)) setSelectedOption(parseInt(winningChoice!));
12+
if (!isUndefined(winningChoice)) setSelectedOption(parseInt(winningChoice));
1313
});
1414
return (
1515
<Container>
16-
{notUndefined([winningChoice, fundedChoices, paidFees]) &&
17-
fundedChoices!.length > 0 &&
18-
!fundedChoices?.includes(winningChoice!) ? (
16+
{!isUndefined(winningChoice) &&
17+
!isUndefined(fundedChoices) &&
18+
!isUndefined(paidFees) &&
19+
fundedChoices.length > 0 &&
20+
!fundedChoices.includes(winningChoice) ? (
1921
<>
2022
<label>Loser deadline has finalized, you can only fund the current winner.</label>
2123
<OptionsContainer>

web/src/pages/Cases/CaseDetails/Evidence/SubmitEvidenceModal.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ const SubmitEvidenceModal: React.FC<{
3333
uploadFormDataToIPFS(formData)
3434
.then(async (res) => {
3535
const response = await res.json();
36-
if (res.status === 200) {
36+
if (res.status === 200 && walletClient) {
3737
const cid = "/ipfs/" + response["cid"];
3838
const { request } = await prepareWriteDisputeKitClassic({
3939
functionName: "submitEvidence",
4040
args: [BigInt(evidenceGroup), cid],
4141
});
42-
await wrapWithToast(walletClient!.writeContract(request)).then(() => {
42+
await wrapWithToast(walletClient.writeContract(request)).then(() => {
4343
setMessage("");
4444
close();
4545
});

web/src/pages/Cases/CaseDetails/Voting/Binary.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { prepareWriteDisputeKitClassic } from "hooks/contracts/generated";
99

1010
const Binary: React.FC<{ arbitrable?: string; voteIDs: string[] }> = ({ arbitrable, voteIDs }) => {
1111
const { id } = useParams();
12-
const parsedDisputeID = BigInt(id!);
12+
const parsedDisputeID = BigInt(id ?? 0);
1313
const parsedVoteIDs = useMemo(() => voteIDs.map((voteID) => BigInt(voteID)), [voteIDs]);
1414
const { data: metaEvidence } = useGetMetaEvidence(id, arbitrable);
1515
const [chosenOption, setChosenOption] = useState(-1);
@@ -44,10 +44,12 @@ const Binary: React.FC<{ arbitrable?: string; voteIDs: string[] }> = ({ arbitrab
4444
functionName: "castVote",
4545
args: [parsedDisputeID, parsedVoteIDs, BigInt(i + 1), 0n, justification],
4646
});
47-
wrapWithToast(walletClient!.writeContract(request)).finally(() => {
48-
setChosenOption(-1);
49-
setIsSending(false);
50-
});
47+
if (walletClient) {
48+
wrapWithToast(walletClient?.writeContract(request)).finally(() => {
49+
setChosenOption(-1);
50+
setIsSending(false);
51+
});
52+
}
5153
}}
5254
/>
5355
))}
@@ -66,10 +68,12 @@ const Binary: React.FC<{ arbitrable?: string; voteIDs: string[] }> = ({ arbitrab
6668
functionName: "castVote",
6769
args: [parsedDisputeID, parsedVoteIDs, 0n, 0n, justification],
6870
});
69-
wrapWithToast(walletClient!.writeContract(request)).finally(() => {
70-
setChosenOption(-1);
71-
setIsSending(false);
72-
});
71+
if (walletClient) {
72+
wrapWithToast(walletClient.writeContract(request)).finally(() => {
73+
setChosenOption(-1);
74+
setIsSending(false);
75+
});
76+
}
7377
}}
7478
/>
7579
</RefuseToArbitrateContainer>

web/src/pages/Courts/CourtDetails/StakePanel/InputDisplay.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const InputDisplay: React.FC<IInputDisplay> = ({ action, isSending, setIsSending
2626
const { id } = useParams();
2727
const { address } = useAccount();
2828
const { data: balance } = usePNKBalance(address);
29-
const parsedBalance = formatEther(balance!);
29+
const parsedBalance = formatEther(balance ?? 0n);
3030
const { data: jurorBalance } = useJurorBalance(address, id);
3131
const parsedStake = formatEther(jurorBalance?.[0] || 0n);
3232
const isStaking = action === ActionType.stake;

web/src/pages/Courts/CourtDetails/StakePanel/StakeWithdrawButton.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from "hooks/contracts/generated";
1414
import { useJurorBalance } from "queries/useJurorBalance";
1515
import { wrapWithToast } from "utils/wrapWithToast";
16-
import { notUndefined } from "utils/index";
16+
import { isUndefined } from "utils/index";
1717

1818
export enum ActionType {
1919
allowance = "allowance",
@@ -33,7 +33,7 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ parsedAmount, action, se
3333
const { id } = useParams();
3434
const { address } = useAccount();
3535
const { data: balance } = usePnkBalanceOf({
36-
enabled: notUndefined(address),
36+
enabled: !isUndefined(address),
3737
args: [address!],
3838
watch: true,
3939
});
@@ -55,12 +55,12 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ parsedAmount, action, se
5555

5656
const klerosCore = getKlerosCore({});
5757
const { config: increaseAllowanceConfig } = usePreparePnkIncreaseAllowance({
58-
enabled: notUndefined([klerosCore, targetStake, allowance]),
58+
enabled: !isUndefined([klerosCore, targetStake, allowance]),
5959
args: [klerosCore?.address, BigInt(targetStake ?? 0) - BigInt(allowance ?? 0)],
6060
});
6161
const { writeAsync: increaseAllowance } = usePnkIncreaseAllowance(increaseAllowanceConfig);
6262
const handleAllowance = () => {
63-
if (notUndefined(increaseAllowance)) {
63+
if (!isUndefined(increaseAllowance)) {
6464
setIsSending(true);
6565
wrapWithToast(increaseAllowance!()).finally(() => {
6666
setIsSending(false);
@@ -69,8 +69,8 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ parsedAmount, action, se
6969
};
7070

7171
const { config: setStakeConfig } = usePrepareKlerosCoreSetStake({
72-
enabled: notUndefined([targetStake, id]),
73-
args: [BigInt(id!), targetStake!],
72+
enabled: !isUndefined(targetStake) && !isUndefined(id),
73+
args: [BigInt(id ?? 0), targetStake],
7474
});
7575
const { writeAsync: setStake } = useKlerosCoreSetStake(setStakeConfig);
7676
const handleStake = () => {
@@ -107,7 +107,7 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ parsedAmount, action, se
107107
<Button
108108
text={text}
109109
isLoading={isSending}
110-
disabled={isSending || parsedAmount == 0n || !notUndefined(targetStake) || checkDisabled()}
110+
disabled={isSending || parsedAmount == 0n || !!isUndefined(targetStake) || checkDisabled()}
111111
onClick={onClick}
112112
/>
113113
);

0 commit comments

Comments
 (0)