Skip to content

Commit 9152499

Browse files
committed
fix: write functions to viem
1 parent d77cdcf commit 9152499

File tree

6 files changed

+37
-20
lines changed

6 files changed

+37
-20
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(fundAppeal, publicClient)
8283
.then(() => {
8384
setAmount("");
8485
close();

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

Lines changed: 3 additions & 2 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,7 +42,7 @@ const SubmitEvidenceModal: React.FC<{
4142
functionName: "submitEvidence",
4243
args: [BigInt(evidenceGroup), cid],
4344
});
44-
await wrapWithToast(walletClient.writeContract(request)).then(() => {
45+
await wrapWithToast(() => walletClient.writeContract(request), publicClient).then(() => {
4546
setMessage("");
4647
close();
4748
});

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(() => 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: 15 additions & 8 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,15 +38,22 @@ 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, id],
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;
@@ -56,14 +63,14 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ parsedAmount, action, se
5663

5764
const klerosCore = getKlerosCore({});
5865
const { config: increaseAllowanceConfig } = usePreparePnkIncreaseAllowance({
59-
enabled: !isUndefined([klerosCore, targetStake, allowance]),
66+
enabled: !isUndefined(klerosCore) && !isUndefined(targetStake) && !isUndefined(allowance),
6067
args: [klerosCore?.address, BigInt(targetStake ?? 0) - BigInt(allowance ?? 0)],
6168
});
6269
const { writeAsync: increaseAllowance } = usePnkIncreaseAllowance(increaseAllowanceConfig);
6370
const handleAllowance = () => {
6471
if (!isUndefined(increaseAllowance)) {
6572
setIsSending(true);
66-
wrapWithToast(increaseAllowance!()).finally(() => {
73+
wrapWithToast(increaseAllowance, publicClient).finally(() => {
6774
setIsSending(false);
6875
});
6976
}
@@ -77,7 +84,7 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ parsedAmount, action, se
7784
const handleStake = () => {
7885
if (typeof setStake !== "undefined") {
7986
setIsSending(true);
80-
wrapWithToast(setStake())
87+
wrapWithToast(setStake, publicClient)
8188
.then(() => {
8289
setAmount("");
8390
})

web/src/utils/wrapWithToast.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { toast, ToastPosition, Theme } from "react-toastify";
2+
import { WriteContractResult } from "wagmi/dist/actions";
23

34
export const OPTIONS = {
45
position: "top-center" as ToastPosition,
@@ -11,11 +12,12 @@ export const OPTIONS = {
1112
theme: "colored" as Theme,
1213
};
1314

14-
export async function wrapWithToast(tx: Promise<any>) {
15+
export async function wrapWithToast(contractWrite: () => Promise<WriteContractResult>, publicClient: any) {
1516
toast.info("Transaction initiated", OPTIONS);
16-
await tx
17-
.then(async (tx) => {
18-
await tx.wait(2);
17+
const { hash } = await contractWrite();
18+
await publicClient
19+
.waitForTransactionReceipt({ hash, confirmations: 2 })
20+
.then(() => {
1921
toast.success("Transaction mined!", OPTIONS);
2022
})
2123
.catch((error) => {

0 commit comments

Comments
 (0)