Skip to content

Commit edc8301

Browse files
authored
Merge pull request #913 from kleros/refactor(web)/ethers-to-viem-marinos-files
Refactor(web)/ethers to viem marinos files
2 parents a2a9294 + 4f3abc5 commit edc8301

File tree

15 files changed

+195
-290
lines changed

15 files changed

+195
-290
lines changed

web/src/hooks/useParsedAmount.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useMemo } from "react";
22
import { parseUnits } from "viem";
33

4-
export function useParsedAmount(amount: string): bigint {
5-
return useMemo(() => (amount === "" ? 0n : parseUnits(`${parseInt(amount)}`, 18)), [amount]);
4+
export function useParsedAmount(amount: `${number}`): bigint {
5+
return useMemo(() => parseUnits(amount, 18), [amount]);
66
}

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

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import { useParams } from "react-router-dom";
44
import { useAccount, useBalance } from "wagmi";
55
import { useDebounce } from "react-use";
66
import { Field, Button } from "@kleros/ui-components-library";
7-
import {
8-
usePrepareDisputeKitClassicFundAppeal,
9-
useDisputeKitClassicFundAppeal,
10-
} from "hooks/contracts/generated";
7+
import { usePrepareDisputeKitClassicFundAppeal, useDisputeKitClassicFundAppeal } from "hooks/contracts/generated";
118
import { wrapWithToast } from "utils/wrapWithToast";
129
import { useParsedAmount } from "hooks/useParsedAmount";
1310
import {
@@ -16,15 +13,13 @@ import {
1613
useFundingContext,
1714
} from "hooks/useClassicAppealContext";
1815
import { notUndefined } from "utils/index";
19-
import { BigNumber } from "ethers";
2016

2117
const Fund: React.FC = () => {
2218
const loserSideCountdown = useLoserSideCountdownContext();
2319
const { fundedChoices, winningChoice } = useFundingContext();
2420
const needFund =
2521
notUndefined([loserSideCountdown, fundedChoices]) &&
26-
(loserSideCountdown! > 0 ||
27-
(fundedChoices!.length > 0 && !fundedChoices?.includes(winningChoice!)));
22+
(loserSideCountdown! > 0 || (fundedChoices!.length > 0 && !fundedChoices?.includes(winningChoice!)));
2823
const { id } = useParams();
2924
const { address, isDisconnected } = useAccount();
3025
const { data: balance } = useBalance({
@@ -39,13 +34,10 @@ const Fund: React.FC = () => {
3934
const { selectedOption } = useSelectedOptionContext();
4035
const { config: fundAppealConfig } = usePrepareDisputeKitClassicFundAppeal({
4136
enabled: notUndefined([id, selectedOption]),
42-
args: [BigNumber.from(id), BigNumber.from(selectedOption)],
43-
overrides: {
44-
value: parsedAmount,
45-
},
37+
args: [BigInt(id!), BigInt(selectedOption!)],
38+
value: parsedAmount,
4639
});
47-
const { writeAsync: fundAppeal } =
48-
useDisputeKitClassicFundAppeal(fundAppealConfig);
40+
const { writeAsync: fundAppeal } = useDisputeKitClassicFundAppeal(fundAppealConfig);
4941
return needFund ? (
5042
<div>
5143
<label>How much ETH do you want to contribute?</label>
@@ -59,12 +51,7 @@ const Fund: React.FC = () => {
5951
placeholder="Amount to fund"
6052
/>
6153
<StyledButton
62-
disabled={
63-
isDisconnected ||
64-
isSending ||
65-
!balance ||
66-
parsedAmount.gt(balance.value)
67-
}
54+
disabled={isDisconnected || isSending || !balance || parsedAmount > balance.value}
6855
text={isDisconnected ? "Connect to Fund" : "Fund"}
6956
onClick={() => {
7057
if (fundAppeal) {

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from "react";
22
import styled from "styled-components";
3-
import { BigNumber } from "ethers";
43
import StageExplainer from "../StageExplainer";
54
import OptionCard from "../../OptionCard";
65
import {
@@ -11,12 +10,7 @@ import {
1110
} from "hooks/useClassicAppealContext";
1211

1312
const StageOne: React.FC = () => {
14-
const {
15-
paidFees,
16-
winningChoice,
17-
loserRequiredFunding,
18-
winnerRequiredFunding,
19-
} = useFundingContext();
13+
const { paidFees, winningChoice, loserRequiredFunding, winnerRequiredFunding } = useFundingContext();
2014
const options = useOptionsContext();
2115
const loserSideCountdown = useLoserSideCountdownContext();
2216
const { selectedOption, setSelectedOption } = useSelectedOptionContext();
@@ -34,14 +28,8 @@ const StageOne: React.FC = () => {
3428
text={answer}
3529
selected={i === selectedOption}
3630
winner={i.toString() === winningChoice}
37-
funding={
38-
paidFees[i] ? BigNumber.from(paidFees[i]) : BigNumber.from(0)
39-
}
40-
required={
41-
i.toString() === winningChoice
42-
? winnerRequiredFunding
43-
: loserRequiredFunding
44-
}
31+
funding={paidFees[i] ? BigInt(paidFees[i]) : 0n}
32+
required={i.toString() === winningChoice ? winnerRequiredFunding : loserRequiredFunding}
4533
onClick={() => setSelectedOption(i)}
4634
/>
4735
))}

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

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,37 @@
11
import React, { useEffect } from "react";
22
import styled from "styled-components";
3-
import { BigNumber } from "ethers";
43
import OptionCard from "../../OptionCard";
5-
import {
6-
useFundingContext,
7-
useOptionsContext,
8-
useSelectedOptionContext,
9-
} from "hooks/useClassicAppealContext";
4+
import { useFundingContext, useOptionsContext, useSelectedOptionContext } from "hooks/useClassicAppealContext";
105
import { notUndefined } from "utils/index";
116

127
const StageOne: React.FC = () => {
13-
const { paidFees, winningChoice, winnerRequiredFunding, fundedChoices } =
14-
useFundingContext();
8+
const { paidFees, winningChoice, winnerRequiredFunding, fundedChoices } = useFundingContext();
159
const options = useOptionsContext();
1610
const { selectedOption, setSelectedOption } = useSelectedOptionContext();
1711
useEffect(() => {
18-
if (notUndefined(winningChoice))
19-
setSelectedOption(parseInt(winningChoice!));
12+
if (notUndefined(winningChoice)) setSelectedOption(parseInt(winningChoice!));
2013
});
2114
return (
2215
<Container>
2316
{notUndefined([winningChoice, fundedChoices, paidFees]) &&
2417
fundedChoices!.length > 0 &&
2518
!fundedChoices?.includes(winningChoice!) ? (
2619
<>
27-
<label>
28-
Loser deadline has finalized, you can only fund the current winner.
29-
</label>
20+
<label>Loser deadline has finalized, you can only fund the current winner.</label>
3021
<OptionsContainer>
3122
<OptionCard
3223
text={options![winningChoice!]}
3324
selected={winningChoice === selectedOption}
3425
winner={true}
35-
funding={
36-
paidFees![winningChoice!]
37-
? BigNumber.from(paidFees![winningChoice!])
38-
: BigNumber.from(0)
39-
}
26+
funding={paidFees![winningChoice!] ? BigInt(paidFees![winningChoice!]) : 0n}
4027
required={winnerRequiredFunding!}
4128
canBeSelected={false}
4229
onClick={() => setSelectedOption(parseInt(winningChoice!, 10))}
4330
/>
4431
</OptionsContainer>
4532
</>
4633
) : (
47-
<label>
48-
No losing option has been funded in time, winner is maintained.
49-
</label>
34+
<label>No losing option has been funded in time, winner is maintained.</label>
5035
)}
5136
</Container>
5237
);

web/src/pages/Cases/CaseDetails/Appeal/OptionCard.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import styled from "styled-components";
33
import { useMeasure } from "react-use";
44
import { Card, Radio, LinearProgress } from "@kleros/ui-components-library";
55
import Gavel from "svgs/icons/gavel.svg";
6-
import { BigNumber, utils } from "ethers";
6+
import { formatEther } from "viem";
77

88
interface IOptionCard extends React.HTMLAttributes<HTMLDivElement> {
99
text: string;
10-
funding: BigNumber;
11-
required: BigNumber;
10+
funding: bigint;
11+
required: bigint;
1212
winner?: boolean;
1313
selected?: boolean;
1414
canBeSelected?: boolean;
@@ -38,18 +38,12 @@ const OptionCard: React.FC<IOptionCard> = ({
3838
</TopContainer>
3939
<LabelContainer>
4040
<label>
41-
{funding.gte(required)
41+
{funding >= required
4242
? "Fully funded!"
43-
: utils.formatEther(funding) +
44-
" out of " +
45-
utils.formatEther(required) +
46-
"ETH required"}
43+
: formatEther(funding) + " out of " + formatEther(required) + "ETH required"}
4744
</label>
4845
</LabelContainer>
49-
<LinearProgress
50-
progress={funding.mul(100).div(required).toNumber()}
51-
width={width}
52-
/>
46+
<LinearProgress progress={Number((funding * 100n) / required)} width={width} />
5347
</StyledCard>
5448
);
5549
};

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

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
import React, { useState } from "react";
22
import styled from "styled-components";
3-
import { BigNumber } from "ethers";
4-
import { useSigner } from "wagmi";
53
import { toast } from "react-toastify";
64
import Modal from "react-modal";
75
import { Textarea, Button } from "@kleros/ui-components-library";
8-
import { useDisputeKitClassic } from "hooks/contracts/generated";
96
import { wrapWithToast, OPTIONS as toastOptions } from "utils/wrapWithToast";
107
import { uploadFormDataToIPFS } from "utils/uploadFormDataToIPFS";
8+
import { useWalletClient } from "wagmi";
9+
import { prepareWriteDisputeKitClassic } from "hooks/contracts/generated";
1110

1211
const SubmitEvidenceModal: React.FC<{
1312
isOpen: boolean;
1413
evidenceGroup: string;
1514
close: () => void;
1615
}> = ({ isOpen, evidenceGroup, close }) => {
17-
const { data: signer } = useSigner();
18-
const disputeKit = useDisputeKitClassic({
19-
signerOrProvider: signer,
20-
});
16+
const { data: walletClient } = useWalletClient();
2117
const [isSending, setIsSending] = useState(false);
2218
const [message, setMessage] = useState("");
2319
return (
@@ -31,24 +27,26 @@ const SubmitEvidenceModal: React.FC<{
3127
isLoading={isSending}
3228
disabled={isSending}
3329
onClick={() => {
34-
if (disputeKit) {
35-
setIsSending(true);
36-
const formData = constructEvidence(message);
37-
toast.info("Uploading to IPFS", toastOptions);
38-
uploadFormDataToIPFS(formData)
39-
.then(async (res) => {
40-
const response = await res.json();
41-
if (res.status === 200) {
42-
const cid = "/ipfs/" + response["cid"];
43-
await wrapWithToast(disputeKit.submitEvidence(BigNumber.from(evidenceGroup), cid)).then(() => {
44-
setMessage("");
45-
close();
46-
});
47-
}
48-
})
49-
.catch()
50-
.finally(() => setIsSending(false));
51-
}
30+
setIsSending(true);
31+
const formData = constructEvidence(message);
32+
toast.info("Uploading to IPFS", toastOptions);
33+
uploadFormDataToIPFS(formData)
34+
.then(async (res) => {
35+
const response = await res.json();
36+
if (res.status === 200) {
37+
const cid = "/ipfs/" + response["cid"];
38+
const { request } = await prepareWriteDisputeKitClassic({
39+
functionName: "submitEvidence",
40+
args: [BigInt(evidenceGroup), cid],
41+
});
42+
await wrapWithToast(walletClient!.writeContract(request)).then(() => {
43+
setMessage("");
44+
close();
45+
});
46+
}
47+
})
48+
.catch()
49+
.finally(() => setIsSending(false));
5250
}}
5351
/>
5452
</ButtonArea>

0 commit comments

Comments
 (0)