Skip to content

fix: write functions to viem #986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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