Skip to content

Commit e63117c

Browse files
authored
Merge pull request #986 from kleros/fix(web)/write-functions-to-viem
fix: write functions to viem
2 parents c584078 + 0a124e7 commit e63117c

File tree

6 files changed

+47
-26
lines changed

6 files changed

+47
-26
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useState } from "react";
22
import styled from "styled-components";
33
import { useParams } from "react-router-dom";
4-
import { useAccount, useBalance } from "wagmi";
4+
import { useAccount, useBalance, usePublicClient } from "wagmi";
55
import { useDebounce } from "react-use";
66
import { Field, Button } from "@kleros/ui-components-library";
77
import { wrapWithToast } from "utils/wrapWithToast";
@@ -49,6 +49,7 @@ const Fund: React.FC = () => {
4949
address,
5050
watch: true,
5151
});
52+
const publicClient = usePublicClient();
5253

5354
const [amount, setAmount] = useState("");
5455
const [debouncedAmount, setDebouncedAmount] = useState("");
@@ -78,7 +79,7 @@ const Fund: React.FC = () => {
7879
onClick={() => {
7980
if (fundAppeal) {
8081
setIsSending(true);
81-
wrapWithToast(fundAppeal())
82+
wrapWithToast(async () => await fundAppeal().then((response) => response.hash), publicClient)
8283
.then(() => {
8384
setAmount("");
8485
close();

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Modal from "react-modal";
55
import { Textarea, Button } from "@kleros/ui-components-library";
66
import { wrapWithToast, OPTIONS as toastOptions } from "utils/wrapWithToast";
77
import { uploadFormDataToIPFS } from "utils/uploadFormDataToIPFS";
8-
import { useWalletClient } from "wagmi";
8+
import { useWalletClient, usePublicClient } from "wagmi";
99
import { EnsureChain } from "components/EnsureChain";
1010
import { prepareWriteDisputeKitClassic } from "hooks/contracts/generated";
1111

@@ -15,6 +15,7 @@ const SubmitEvidenceModal: React.FC<{
1515
close: () => void;
1616
}> = ({ isOpen, evidenceGroup, close }) => {
1717
const { data: walletClient } = useWalletClient();
18+
const publicClient = usePublicClient();
1819
const [isSending, setIsSending] = useState(false);
1920
const [message, setMessage] = useState("");
2021
return (
@@ -41,10 +42,12 @@ const SubmitEvidenceModal: React.FC<{
4142
functionName: "submitEvidence",
4243
args: [BigInt(evidenceGroup), cid],
4344
});
44-
await wrapWithToast(walletClient.writeContract(request)).then(() => {
45-
setMessage("");
46-
close();
47-
});
45+
await wrapWithToast(async () => await walletClient.writeContract(request), publicClient).then(
46+
() => {
47+
setMessage("");
48+
close();
49+
}
50+
);
4851
}
4952
})
5053
.catch()

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useMemo, useState } from "react";
22
import styled from "styled-components";
33
import { useParams } from "react-router-dom";
4-
import { useWalletClient } from "wagmi";
4+
import { useWalletClient, usePublicClient } from "wagmi";
55
import { Button, Textarea } from "@kleros/ui-components-library";
66
import { prepareWriteDisputeKitClassic } from "hooks/contracts/generated";
77
import { wrapWithToast } from "utils/wrapWithToast";
@@ -54,6 +54,7 @@ const Binary: React.FC<{ arbitrable: `0x${string}`; voteIDs: string[] }> = ({ ar
5454
const [isSending, setIsSending] = useState(false);
5555
const [justification, setJustification] = useState("");
5656
const { data: walletClient } = useWalletClient();
57+
const publicClient = usePublicClient();
5758

5859
const handleVote = async (voteOption: number) => {
5960
setIsSending(true);
@@ -69,7 +70,7 @@ const Binary: React.FC<{ arbitrable: `0x${string}`; voteIDs: string[] }> = ({ ar
6970
],
7071
});
7172
if (walletClient) {
72-
wrapWithToast(walletClient.writeContract(request)).finally(() => {
73+
wrapWithToast(async () => await walletClient.writeContract(request), publicClient).finally(() => {
7374
setChosenOption(-1);
7475
setIsSending(false);
7576
});

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { Field } from "@kleros/ui-components-library";
88

99
import { useParsedAmount } from "hooks/useParsedAmount";
1010
import { usePNKBalance } from "queries/usePNKBalance";
11-
import { useJurorBalance } from "queries/useJurorBalance";
11+
import { useKlerosCoreGetJurorBalance } from "hooks/contracts/generated";
1212
import StakeWithdrawButton, { ActionType } from "./StakeWithdrawButton";
13+
import { isUndefined } from "utils/index";
1314
import { EnsureChain } from "components/EnsureChain";
1415

1516
const StyledField = styled(Field)`
@@ -50,7 +51,11 @@ const InputDisplay: React.FC<IInputDisplay> = ({ action, isSending, setIsSending
5051
const { address } = useAccount();
5152
const { data: balance } = usePNKBalance(address);
5253
const parsedBalance = formatEther(balance ?? 0n);
53-
const { data: jurorBalance } = useJurorBalance(address, id);
54+
const { data: jurorBalance } = useKlerosCoreGetJurorBalance({
55+
enabled: !isUndefined(address),
56+
args: [address, id],
57+
watch: true,
58+
});
5459
const parsedStake = formatEther(jurorBalance?.[0] || 0n);
5560
const isStaking = action === ActionType.stake;
5661

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useMemo } from "react";
22
import { useParams } from "react-router-dom";
3-
import { useAccount } from "wagmi";
3+
import { useAccount, usePublicClient } from "wagmi";
44
import { Button } from "@kleros/ui-components-library";
55
import {
66
getKlerosCore,
@@ -9,8 +9,8 @@ import {
99
usePnkBalanceOf,
1010
usePnkIncreaseAllowance,
1111
usePreparePnkIncreaseAllowance,
12+
useKlerosCoreGetJurorBalance,
1213
} from "hooks/contracts/generated";
13-
import { useJurorBalance } from "queries/useJurorBalance";
1414
import { usePNKAllowance } from "queries/usePNKAllowance";
1515
import { wrapWithToast } from "utils/wrapWithToast";
1616
import { isUndefined } from "utils/index";
@@ -38,34 +38,44 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ parsedAmount, action, se
3838
args: [address!],
3939
watch: true,
4040
});
41-
const { data: jurorBalance } = useJurorBalance(address, id);
41+
const { data: jurorBalance } = useKlerosCoreGetJurorBalance({
42+
enabled: !isUndefined(address),
43+
args: [address ?? "0x", BigInt(id ?? 0)],
44+
watch: true,
45+
});
4246
const { data: allowance } = usePNKAllowance(address);
47+
const publicClient = usePublicClient();
4348

4449
const isStaking = action === ActionType.stake;
45-
const isAllowance = isStaking && allowance && allowance < parsedAmount;
50+
const isAllowance = isStaking && !isUndefined(allowance) && allowance < parsedAmount;
4651

4752
const targetStake = useMemo(() => {
4853
if (jurorBalance) {
49-
if (action === ActionType.stake) {
54+
if (isAllowance) {
55+
return parsedAmount;
56+
} else if (isStaking) {
5057
return jurorBalance[0] + parsedAmount;
5158
} else {
5259
return jurorBalance[0] - parsedAmount;
5360
}
5461
}
55-
}, [action, jurorBalance, parsedAmount]);
62+
return 0n;
63+
}, [jurorBalance, parsedAmount, isAllowance, isStaking]);
5664

5765
const klerosCore = getKlerosCore({});
5866
const { config: increaseAllowanceConfig } = usePreparePnkIncreaseAllowance({
59-
enabled: !isUndefined([klerosCore, targetStake, allowance]),
67+
enabled: isAllowance && !isUndefined(klerosCore) && !isUndefined(targetStake) && !isUndefined(allowance),
6068
args: [klerosCore?.address, BigInt(targetStake ?? 0) - BigInt(allowance ?? 0)],
6169
});
6270
const { writeAsync: increaseAllowance } = usePnkIncreaseAllowance(increaseAllowanceConfig);
6371
const handleAllowance = () => {
6472
if (!isUndefined(increaseAllowance)) {
6573
setIsSending(true);
66-
wrapWithToast(increaseAllowance!()).finally(() => {
67-
setIsSending(false);
68-
});
74+
wrapWithToast(async () => await increaseAllowance().then((response) => response.hash), publicClient).finally(
75+
() => {
76+
setIsSending(false);
77+
}
78+
);
6979
}
7080
};
7181

@@ -77,7 +87,7 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ parsedAmount, action, se
7787
const handleStake = () => {
7888
if (typeof setStake !== "undefined") {
7989
setIsSending(true);
80-
wrapWithToast(setStake())
90+
wrapWithToast(async () => await setStake().then((response) => response.hash), publicClient)
8191
.then(() => {
8292
setAmount("");
8393
})

web/src/utils/wrapWithToast.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ export const OPTIONS = {
1111
theme: "colored" as Theme,
1212
};
1313

14-
export async function wrapWithToast(tx: Promise<any>) {
14+
export async function wrapWithToast(contractWrite: () => Promise<`0x${string}`>, publicClient: any) {
1515
toast.info("Transaction initiated", OPTIONS);
16-
await tx
17-
.then(async (tx) => {
18-
await tx.wait(2);
16+
const hash = await contractWrite();
17+
await publicClient
18+
.waitForTransactionReceipt({ hash, confirmations: 2 })
19+
.then(() => {
1920
toast.success("Transaction mined!", OPTIONS);
2021
})
2122
.catch((error) => {

0 commit comments

Comments
 (0)