Skip to content

Commit

Permalink
Merge pull request #986 from kleros/fix(web)/write-functions-to-viem
Browse files Browse the repository at this point in the history
fix: write functions to viem
  • Loading branch information
alcercu authored Jun 28, 2023
2 parents c584078 + 0a124e7 commit e63117c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 26 deletions.
5 changes: 3 additions & 2 deletions web/src/pages/Cases/CaseDetails/Appeal/Classic/Fund.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from "react";
import styled from "styled-components";
import { useParams } from "react-router-dom";
import { useAccount, useBalance } from "wagmi";
import { useAccount, useBalance, usePublicClient } from "wagmi";
import { useDebounce } from "react-use";
import { Field, Button } from "@kleros/ui-components-library";
import { wrapWithToast } from "utils/wrapWithToast";
Expand Down Expand Up @@ -49,6 +49,7 @@ const Fund: React.FC = () => {
address,
watch: true,
});
const publicClient = usePublicClient();

const [amount, setAmount] = useState("");
const [debouncedAmount, setDebouncedAmount] = useState("");
Expand Down Expand Up @@ -78,7 +79,7 @@ const Fund: React.FC = () => {
onClick={() => {
if (fundAppeal) {
setIsSending(true);
wrapWithToast(fundAppeal())
wrapWithToast(async () => await fundAppeal().then((response) => response.hash), publicClient)
.then(() => {
setAmount("");
close();
Expand Down
13 changes: 8 additions & 5 deletions web/src/pages/Cases/CaseDetails/Evidence/SubmitEvidenceModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Modal from "react-modal";
import { Textarea, Button } from "@kleros/ui-components-library";
import { wrapWithToast, OPTIONS as toastOptions } from "utils/wrapWithToast";
import { uploadFormDataToIPFS } from "utils/uploadFormDataToIPFS";
import { useWalletClient } from "wagmi";
import { useWalletClient, usePublicClient } from "wagmi";
import { EnsureChain } from "components/EnsureChain";
import { prepareWriteDisputeKitClassic } from "hooks/contracts/generated";

Expand All @@ -15,6 +15,7 @@ const SubmitEvidenceModal: React.FC<{
close: () => void;
}> = ({ isOpen, evidenceGroup, close }) => {
const { data: walletClient } = useWalletClient();
const publicClient = usePublicClient();
const [isSending, setIsSending] = useState(false);
const [message, setMessage] = useState("");
return (
Expand All @@ -41,10 +42,12 @@ const SubmitEvidenceModal: React.FC<{
functionName: "submitEvidence",
args: [BigInt(evidenceGroup), cid],
});
await wrapWithToast(walletClient.writeContract(request)).then(() => {
setMessage("");
close();
});
await wrapWithToast(async () => await walletClient.writeContract(request), publicClient).then(
() => {
setMessage("");
close();
}
);
}
})
.catch()
Expand Down
5 changes: 3 additions & 2 deletions web/src/pages/Cases/CaseDetails/Voting/Binary.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useMemo, useState } from "react";
import styled from "styled-components";
import { useParams } from "react-router-dom";
import { useWalletClient } from "wagmi";
import { useWalletClient, usePublicClient } from "wagmi";
import { Button, Textarea } from "@kleros/ui-components-library";
import { prepareWriteDisputeKitClassic } from "hooks/contracts/generated";
import { wrapWithToast } from "utils/wrapWithToast";
Expand Down Expand Up @@ -54,6 +54,7 @@ const Binary: React.FC<{ arbitrable: `0x${string}`; voteIDs: string[] }> = ({ ar
const [isSending, setIsSending] = useState(false);
const [justification, setJustification] = useState("");
const { data: walletClient } = useWalletClient();
const publicClient = usePublicClient();

const handleVote = async (voteOption: number) => {
setIsSending(true);
Expand All @@ -69,7 +70,7 @@ const Binary: React.FC<{ arbitrable: `0x${string}`; voteIDs: string[] }> = ({ ar
],
});
if (walletClient) {
wrapWithToast(walletClient.writeContract(request)).finally(() => {
wrapWithToast(async () => await walletClient.writeContract(request), publicClient).finally(() => {
setChosenOption(-1);
setIsSending(false);
});
Expand Down
9 changes: 7 additions & 2 deletions web/src/pages/Courts/CourtDetails/StakePanel/InputDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import { Field } from "@kleros/ui-components-library";

import { useParsedAmount } from "hooks/useParsedAmount";
import { usePNKBalance } from "queries/usePNKBalance";
import { useJurorBalance } from "queries/useJurorBalance";
import { useKlerosCoreGetJurorBalance } from "hooks/contracts/generated";
import StakeWithdrawButton, { ActionType } from "./StakeWithdrawButton";
import { isUndefined } from "utils/index";
import { EnsureChain } from "components/EnsureChain";

const StyledField = styled(Field)`
Expand Down Expand Up @@ -50,7 +51,11 @@ const InputDisplay: React.FC<IInputDisplay> = ({ action, isSending, setIsSending
const { address } = useAccount();
const { data: balance } = usePNKBalance(address);
const parsedBalance = formatEther(balance ?? 0n);
const { data: jurorBalance } = useJurorBalance(address, id);
const { data: jurorBalance } = useKlerosCoreGetJurorBalance({
enabled: !isUndefined(address),
args: [address, id],
watch: true,
});
const parsedStake = formatEther(jurorBalance?.[0] || 0n);
const isStaking = action === ActionType.stake;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useMemo } from "react";
import { useParams } from "react-router-dom";
import { useAccount } from "wagmi";
import { useAccount, usePublicClient } from "wagmi";
import { Button } from "@kleros/ui-components-library";
import {
getKlerosCore,
Expand All @@ -9,8 +9,8 @@ import {
usePnkBalanceOf,
usePnkIncreaseAllowance,
usePreparePnkIncreaseAllowance,
useKlerosCoreGetJurorBalance,
} from "hooks/contracts/generated";
import { useJurorBalance } from "queries/useJurorBalance";
import { usePNKAllowance } from "queries/usePNKAllowance";
import { wrapWithToast } from "utils/wrapWithToast";
import { isUndefined } from "utils/index";
Expand Down Expand Up @@ -38,34 +38,44 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ parsedAmount, action, se
args: [address!],
watch: true,
});
const { data: jurorBalance } = useJurorBalance(address, id);
const { data: jurorBalance } = useKlerosCoreGetJurorBalance({
enabled: !isUndefined(address),
args: [address ?? "0x", BigInt(id ?? 0)],
watch: true,
});
const { data: allowance } = usePNKAllowance(address);
const publicClient = usePublicClient();

const isStaking = action === ActionType.stake;
const isAllowance = isStaking && allowance && allowance < parsedAmount;
const isAllowance = isStaking && !isUndefined(allowance) && allowance < parsedAmount;

const targetStake = useMemo(() => {
if (jurorBalance) {
if (action === ActionType.stake) {
if (isAllowance) {
return parsedAmount;
} else if (isStaking) {
return jurorBalance[0] + parsedAmount;
} else {
return jurorBalance[0] - parsedAmount;
}
}
}, [action, jurorBalance, parsedAmount]);
return 0n;
}, [jurorBalance, parsedAmount, isAllowance, isStaking]);

const klerosCore = getKlerosCore({});
const { config: increaseAllowanceConfig } = usePreparePnkIncreaseAllowance({
enabled: !isUndefined([klerosCore, targetStake, allowance]),
enabled: isAllowance && !isUndefined(klerosCore) && !isUndefined(targetStake) && !isUndefined(allowance),
args: [klerosCore?.address, BigInt(targetStake ?? 0) - BigInt(allowance ?? 0)],
});
const { writeAsync: increaseAllowance } = usePnkIncreaseAllowance(increaseAllowanceConfig);
const handleAllowance = () => {
if (!isUndefined(increaseAllowance)) {
setIsSending(true);
wrapWithToast(increaseAllowance!()).finally(() => {
setIsSending(false);
});
wrapWithToast(async () => await increaseAllowance().then((response) => response.hash), publicClient).finally(
() => {
setIsSending(false);
}
);
}
};

Expand All @@ -77,7 +87,7 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({ parsedAmount, action, se
const handleStake = () => {
if (typeof setStake !== "undefined") {
setIsSending(true);
wrapWithToast(setStake())
wrapWithToast(async () => await setStake().then((response) => response.hash), publicClient)
.then(() => {
setAmount("");
})
Expand Down
9 changes: 5 additions & 4 deletions web/src/utils/wrapWithToast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ export const OPTIONS = {
theme: "colored" as Theme,
};

export async function wrapWithToast(tx: Promise<any>) {
export async function wrapWithToast(contractWrite: () => Promise<`0x${string}`>, publicClient: any) {
toast.info("Transaction initiated", OPTIONS);
await tx
.then(async (tx) => {
await tx.wait(2);
const hash = await contractWrite();
await publicClient
.waitForTransactionReceipt({ hash, confirmations: 2 })
.then(() => {
toast.success("Transaction mined!", OPTIONS);
})
.catch((error) => {
Expand Down

0 comments on commit e63117c

Please sign in to comment.